• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Java problem: 2d-Arrays

 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to understand this question, can you explain it in pseudo code or better please: Create a two dimensional array of integers called rows[][] with five rows and four Columns. Modify the array so that the last element in each row is equal to the sum of all the elements in the row minus the last element. Initialize rows[][] with data any way you choose and print out the modified array.
 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm trying to understand this question


The description of the program seems clear to me.  What problem(s) are you having with it?
Given this two dim array  show what the expected result is:  {{1,2}, {3,4}}
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response, I'm confused on how to write the logic for the "Modify the..." down.

I know that to begin this, I must define my rows and columns

int [][] rows = new int [5][4];

I just don't know how to proceed from here.

Norm Radder wrote:

I'm trying to understand this question


The description of the program seems clear to me.  What problem(s) are you having with it?
Given this two dim array  show what the expected result is:  {{1,2}, {3,4}}

 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I know I'm going to be making use of "for loops"

Adebiyi Itunuayo wrote:Thanks for your response, I'm confused on how to write the logic for the "Modify the..." down.

I know that to begin this, I must define my rows and columns

int [][] rows = new int [5][4];

I just don't know how to proceed from here.

Norm Radder wrote:

I'm trying to understand this question


The description of the program seems clear to me.  What problem(s) are you having with it?
Given this two dim array  show what the expected result is:  {{1,2}, {3,4}}

 
Matthew Bendford
Master Rancher
Posts: 245
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note: There's no such thing as multi-dimensional arrays. There're only arrays of arrays. One of the key differences is that the lower orders can have different lengths. There's nothing that would enforce that the lower order always has to be the same length. The following is fine: {{1,2,3,4},{a,b,c,d,e,f,g,h},{null}}. You see: The first array has a length of 4, the second one has a length of 8 and the third one only has a length of just one.
 
Norm Radder
Rancher
Posts: 4764
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First can you post an example of a two dim array
and then do the process on it manually as described in the question
and post the resulting array
to show that you understand what the program is supposed to do.

Once we agree on what the result should be, then we can work on designing the code to do it.
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a 2d array which prints :
246
135
int combination[][] = { {2,4,6}, {1,3,5} };
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
System.out.println(combination[i][j]);
}
}

Norm Radder wrote:First can you post an example of a two dim array
and then do the process on it manually as described in the question
and post the resulting array
to show that you understand what the program is supposed to do.

Once we agree on what the result should be, then we can work on designing the code to do it.

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is a 2d array which prints :
246
135
int combination[][] = { {2,4,6}, {1,3,5} };


Given that array, can you manually create the array for the solution?

We'll work on the design and coding when  we agree on what the program is supposed to do.

Some corrections for your code to use the array's length attribute:

This code should work with different sizes for the array.  
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I've made some corrections you suggested and also the logic for finding the sum of the array elements but I still can't see what it means to "modify the last element"d

int combination[][] = { {2,4,6}, {1,3,5} };
int sum = 0;
for (int i = 0; i<combination.length; i++) { >
for (int j = 0; j<combination[i].length; j++) { >
sum = sum + combination[i][j];
//System.out.println(combination[i][j]);
}
}
System.out.println(sum);


Thank you for your time again, I appreciate you.

Norm Radder wrote:

Here is a 2d array which prints :
246
135
int combination[][] = { {2,4,6}, {1,3,5} };


Given that array, can you manually create the array for the solution?

We'll work on the design and coding when  we agree on what the program is supposed to do.

Some corrections for your code to use the array's length attribute:

This code should work with different sizes for the array.  

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what it means to "modify the last element


If this is the array:  1,2,3  then 3 is the last element.

I assume what is meant by modifying  an array element is  to assign it a value:
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a 2d array would it be:
combination[0][2] and combination[1][2]
For both rows then?

int combination[][] = { {2,4,6}, {1,3,5} };
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
System.out.println(combination[i][j]);
}
}


Norm Radder wrote:

what it means to "modify the last element


If this is the array:  1,2,3  then 3 is the last element.

I assume what is meant by modifying  an array element is  to assign it a value:

 
Norm Radder
Rancher
Posts: 4764
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the contents of the array for the desired solution so we agree on what the solution is?

or a 2d array would it be:
combination[0][2] and combination[1][2]


That only works for an array with two rows that are 3 elements long.  You need a solution that works for arrays of different sizes.

You can use the array's .length attribute to get to the last element.  Since the index of the last element is the array's length - 1,
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please bear with me, I'm putting all my energy into getting this right.

Alright so:
//Create an array with row = 5 and column = 4
int[][] rows = new int[5][4];
//Modify the array so that the last element in each row. Assuming the contents of the array is rows[][] = { {2,4,6,8,10}, {1,3,5,7,9} }
is equal to the sum of all the elements in the row minus the last element.

int lastElement1 = rows.length - 1;
in lastElement2 = rows[0].length - 1


Please am I on the right path?

Norm Radder wrote:Can you post the contents of the array for the desired solution so we agree on what the solution is?

or a 2d array would it be:
combination[0][2] and combination[1][2]


That only works for an array with two rows that are 3 elements long.  You need a solution that works for arrays of different sizes.

You can use the array's .length attribute to get to the last element.  Since the index of the last element is the array's length - 1,

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That would be the index of the last row in the rows two dim array.  I don't know what that would be used for.



There should not be a 0 used as an index here.  The code should use the variable that is the index for that row.  The code uses i to index the rows in the array and j to index the columns in a row.
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


//Create an array with row = 5 and column = 4
int[][] rows = new int[5][4];
//Modify the array so that the last element in each row. Assuming the contents of the array is rows[][] = { {2,4,6,8,10}, {1,3,5,7} }
is equal to the sum of all the elements in the row minus the last element.

int lastElement1 = rows.length - 1;
in lastElement2 = rows[j].length - 1

Would this be correct sir?

Norm Radder wrote:
That would be the index of the last row in the rows two dim array.  I don't know what that would be used for.



There should not be a 0 used as an index here.  The code should use the variable that is the index for that row.  The code uses i to index the rows in the array and j to index the columns in a row.

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will work on a row at a time so there will only be one variable that will have the location of the last element in the current row being worked on.  
You can not easily have a variable for each row because the number of rows may not be known.
The code should be written so it works with arrays of different sizes.

Please wrap your code in code tags:  Select the code and press the Code button.

Given this array:
What will be the final contents of that array after the program runs?
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate your help a lot and I apologise if this is taking your time as I feel I'm in my worst condition to understand such obvious things you are seeing. Perhaps it's because I've been awake overnight and studying for hours. I think I've reached that burnout stage and I don't want a direct answer to the code problem. Can we jump on this later sir when I'm refreshed and you're available to respond? So things don't drag on for too long.

Thank you once again sir.

Norm Radder wrote:The code will work on a row at a time so there will only be one variable that will have the location of the last element in the current row being worked on.  
You can not easily have a variable for each row because the number of rows may not be known.
The code should be written so it works with arrays of different sizes.

Please wrap your code in code tags:  Select the code and press the Code button.

Given this array:
What will be the final contents of that array after the program runs?

 
Norm Radder
Rancher
Posts: 4764
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, some one will be here when you come back.
 
Les Morgan
Rancher
Posts: 1032
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adebiyi,
this: "last element in each row is equal to the sum of all the elements in the row minus the last element".
seems to me to be a confusing way of saying: sum the elements of each row to k-1 and put the result in element k.
Les

Adebiyi Itunuayo wrote:I'm trying to understand this question, can you explain it in pseudo code or better please: Create a two dimensional array of integers called rows[][] with five rows and four Columns. Modify the array so that the last element in each row is equal to the sum of all the elements in the row minus the last element. Initialize rows[][] with data any way you choose and print out the modified array.

 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day, thank you for responding to my question.
I'll try this.

I've been learning more about arrays since I posted the question.

Les Morgan wrote:Adebiyi,
this: "last element in each row is equal to the sum of all the elements in the row minus the last element".
seems to me to be a confusing way of saying: sum the elements of each row to k-1 and put the result in element k.
Les

Adebiyi Itunuayo wrote:I'm trying to understand this question, can you explain it in pseudo code or better please: Create a two dimensional array of integers called rows[][] with five rows and four Columns. Modify the array so that the last element in each row is equal to the sum of all the elements in the row minus the last element. Initialize rows[][] with data any way you choose and print out the modified array.

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have any specific questions about arrays?
 
Piet Souris
Saloon Keeper
Posts: 4993
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or to start with a more base case, before going into 2D:

suppose you have an array [3, -2, 5, 4, 130]. The question is: what would the last element (130) become if it had to be equal to the sum of 3, -2, 5 and 4? Can you think of a method that, given such an array, would return an array with the last element being equal to the sum of the other elements?
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I thought this thread had become obsolete after my last comment until I saw an unread email notification from this thread. I'd have to bookmark this thread I guess.

You've been of immense help sir. I just have this issue with interpreting coding word problems, sometimes I'm seeing the solution clearly, sometimes not. I mean I approach it with an algorithmic eye with intent to break things down step by step, or statement by statement. I've been reading the book "How to think like a programmer" which has been helpful so far. What surefire tip do you have for me to solve this problem?

Does it have to do with a gap in some concepts of programming or software engineering principles generally?

Thank you as I await your positive and uplifting response.

Norm Radder wrote:Do you have any specific questions about arrays?

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you stuck on finishing the program?  Do you have any specific questions?
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply sir. It means a lot.

I think I'd do something like

Piet Souris wrote:Or to start with a more base case, before going into 2D:

suppose you have an array [3, -2, 5, 4, 130]. The question is: what would the last element (130) become if it had to be equal to the sum of 3, -2, 5 and 4? Can you think of a method that, given such an array, would return an array with the last element being equal to the sum of the other elements?

Staff note (Ron McLeod) :

I modified your post to fix the code tags

 
Norm Radder
Rancher
Posts: 4764
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think I'd do something like


What happens when you compile and execute the code?  Copy and paste here any error messages you need help with.
Do you get the correct answer?  
Have you done the exercise manually to compute the answer?  What value did you get when doing it manually?

Note:  Please be sure to wrap all posted code in code tags:  Select the code and press the Code button.
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tweaked the code and came up with this:



Output:



Norm Radder wrote:

I think I'd do something like


What happens when you compile and execute the code?  Copy and paste here any error messages you need help with.
Do you get the correct answer?  
Have you done the exercise manually to compute the answer?  What value did you get when doing it manually?

Note:  Please be sure to wrap all posted code in code tags:  Select the code and press the Code button.

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it compile and execute and give the correct results?

Can you explain the logic of the code?
Why does it have an if statement inside of the for loop?
What is the purpose of the code on line 18?
What is the purpose of the code on lines 15 and 16?

Can the for loop itself control which  of the items in the array are added to the sum?
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just realised the logic wasn't what I expected. I'm reviewing it again. 🤔

Norm Radder wrote:Does it compile and execute and give the correct results?

Can you explain the logic of the code?
Why does it have an if statement inside of the for loop?
What is the purpose of the code on line 18?

Can the for loop itself control which  of the items in the array are added to the sum?

 
Norm Radder
Rancher
Posts: 4764
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first part of the assignment: sum the elements is done by the for loop and the statement on line 14.
The rest of the contents of the for loop make no sense and should be removed.
Start with code that sums the array's contents minus the last element
When that works, store that sum in the last element of the array,
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:




Norm Radder wrote:The first part of the assignment: sum the elements is done by the for loop and the statement on line 14.
The rest of the contents of the for loop make no sense and should be removed.
Start with code that sums the array's contents minus the last element
When that works, store that sum in the last element of the array,

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why add in the last number if the code is going to subtract it.  For example:
if the array contains: 1,2,3,4  why add in the 4
Sum=1+2+3+4  
vs just adding the first numbers and not the last one
sum=1+2+3

The if test inside the for loop makes the code in the loop more complicated.
If could be simplified to just:

Then store the sum into the last element after exiting the loop:
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could this be what you mean sir?

If so, the sum variable's final value is altered to become 10. Say I intend to use the sum value of  140, do I need to store it another variable to use elsewhere?



Output:







Norm Radder wrote:Why add in the last number if the code is going to subtract it.  For example:
if the array contains: 1,2,3,4  why add in the 4
Sum=1+2+3+4  
vs just adding the first numbers and not the last one
sum=1+2+3

The if test inside the for loop makes the code in the loop more complicated.
If could be simplified to just:

Then store the sum into the last element after exiting the loop:

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the for loop's terminating condition is changed so that it only looks at the elements up to but not including the last element,
then the value of the last element does not need to be subtracted from sum.
The statements on lines 16,17and 18 can be removed.
The logic becomes:
Sum the numbers up to but not including the last element
Assign sum to last element of the array
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh I see what you mean. That makes sense.
So the code becomes:



Output:




Norm Radder wrote:If the for loop's terminating condition is changed so that it only looks at the elements up to but not including the last element,
then the value of the last element does not need to be subtracted from sum.
The statements on lines 16,17and 18 can be removed.
The logic becomes:
Sum the numbers up to but not including the last element
Assign sum to last element of the array

 
Norm Radder
Rancher
Posts: 4764
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that looks better.  
lastNum can be removed since it is not used.
Now the code needs to be put into a method that is passed the int array to work on.
The original program was supposed to work on a two-dim array.
Since a two dim array is an array of arrays, it is easy to extract a one dim array from it which can be passed to the method that you have created above.

Here is how to extract a one dim array from a two dim array:
 
Piet Souris
Saloon Keeper
Posts: 4993
186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Norm indicates, we are almost there, but not quite, yet.

So, can you complete this method:
 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the "enhanced for loop" if I'm correct, where the variable before the colon(, in this case "oneDim" corresponds to each element of the array as the iteration occurs. No?

Norm Radder wrote:

Here is how to extract a one dim array from a two dim array:

 
Adebiyi Itunuayo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply. I noticed the line of code:


Why is it like that, I mean written with the square brackets together with a method.

I was taught that it should be a "Data Type (int, String, etc" together with method name if it were to return a value.
Like so


Is that valid also?
Does it have to do with the fact that you're declaring an array parameter?


Piet Souris wrote:As Norm indicates, we are almost there, but not quite, yet.

So, can you complete this method:

 
Campbell Ritchie
Marshal
Posts: 75866
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adebiyi Itunuayo wrote:. . . Why is it like that, I mean written with the square brackets together with a method. . . .

Because the datatypes you are taking as a parameter and what you are returning as a return type are both arrays of ints: int[]
By the way, in your first post youi said the array was called rows[][]. that isn't quite correct. It turns that the datatype was int[][], and the array was called rows, without [].
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic