• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to get the sum of all numbers in a row in 2D array, with row being selected by user?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little confused here, especially since I just learned about 2D arrays and am still trying to get a grasp on the concepts. I'm a little confused by how you return all the values in a row to add up and display the sum if the row is entered by the user.



Plus if this is the wrong way to post code, I apologize, first time poster here.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You should put [code=java] at the start of the code block and [/code] at the end. Don't worry: I sorted it out for you, and you can see how much better it looks I also got rid of the excess blank lines.

Who has taught you about 2D arrays, because they are mistaken. There ain't no such thing in Java®. What you have written is an array of arrays. Your row 1 is an array in its own right and you can iterate that and summate its members. You are writing row in the loop (line 32-33) when you should write column. What is going wrong? You ought to tell us the full details.

You will have to learn about printf. Rather than printing \n\n (you should only use \n if somebody says they want the LF character, even though you see \n in many books), you can write
System.out.printf("%n%n");
And for the elements of the array you can write
System.out.printf("%2d ", myArray[...][...]);
You can read about printf in the Java® Tutorials; use ctrl‑F‑formatting and it comes up at least twice. The %2d bit means you take up to two spaces to print a decimal integer, followed by one space (see where the quote marks are).

Look at the loop in line 32... and work out how many times it will run. Look at what values you are using in the loop header. Also work out which row you will get if the user enters 1.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its easiest to extract the row from the matrix with something like:

int[] rowPicked = matrix[userPick];

careful though, you might want to adjust the index the user gives you, (userPick -1).
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also worth reminding yourself all about the for statement in the Java® Tutorials.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Who has taught you about 2D arrays, because they are mistaken. There ain't no such thing in Java®. What you have written is an array of arrays.



What's the difference? I could have sworn we've called this type of object a 2D array before in this forum.
 
Seth Lamb
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well another term is multidimensional array, but both terms were used by my teacher so I assumed that it was what the array was called, plus the term "2D array" has been used elsewhere on the Internet.

The output I'm getting is:

5 9 87 74 12 7
8 1 12 44 65 99
9 2 33 10 11 64
7 5 45 50 97 82

Please enter the row you would like to add: 2
9
2
33
10
Sum for row [2] is 54.

(I'm aware that I need to subtract 1 from the userpick to get the right row since the indexing starts at zero and not one, but I need to get the output working first). It seems to be adding up the first 4 numbers and then just stops, so I am indeed very confused by what is going on exactly.

And that is the first time I have heard of the printf statement. This is a Grade 12 programming course so it's nothing super in-depth or anything; we seem to be ignoring a lot of the less important commands and methods since we jumped from methods to one dimensional arrays to now this and array lists.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:

Campbell Ritchie wrote:
Who has taught you about 2D arrays, because they are mistaken. There ain't no such thing in Java®. What you have written is an array of arrays.



What's the difference? I could have sworn we've called this type of object a 2D array before in this forum.



And we will again. In fact, I would argue that Java does have 2D arrays, but only for purposes of creating them, which you can do with a single invocation of new:

If Java didn't know how to handle multidimensional arrays directly, I think the compiler would see that line as an attempt to index the 11th element of an array with only 5 elements in it.

Post-compilation, I suspect that the byte-code is an array of arrays, which you can also create individually, with the benefit(?) that not every array must have the same length as every other array in the array of arrays:



This can be confusing, as this code exemplifies:

Here's the output:


So, I'd say it has support for bone fide 2D (and nD) arrays, but also implements all multidimensional arrays as arrays of arrays.
 
Sheriff
Posts: 9021
656
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seth Lamb,

You're not taking seriously already given advises. Campbell Ritchie already pointed you out, where mistake happens: lines 32-33.
Beside that, you choose not good enough variable names, so you get confused.

Exact mistake appears in line 32. What is the length of matrix.length?
Hint: line 21.
 
Seth Lamb
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to get program working properly (output is correct and everything and I did change the variable name to better reflect its purpose), but like I said, the concept of matrices is still a bit difficult for me. I still don't fully understand what matrix.length and matrix[0].length are counting though, how can matrix[0].length be returning all the numbers in that specific row?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
matrix.length is just the number of arrays in the matrix (rows in your square). Matrix[0].length is just the number of elements in row 0 (the first one).
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for delay. I thought I had replied last night and now I find half of my reply not actually posted.

Lots of people use loose terminology on the web; the Java® Tutorials say that a multidimensional array is an array of arrays. It also says that arrays of arrays are different from 2D arrays in C and Fortran. If you use the same name then people will think they mean the same thing.
Java® does not support 2D arrays; C does. In C you may find that all the elements of all the “rows” follow one another in memory. So in a C array like… it is possible to have the numbers in memory like this:-In a Java® ordinary (=1D) array…you would get the same sequence of numbers in memory (probably plus a 9 for length of array), but for a Java® array of arrays you would get something like this instead… where the 3 means it is a 3‑element array and the 3 hex numbers are pointers whereby the constituent arrays can be found. (They might be pointers to pointers, which you call handles. On a 64‑bit machine the pointers may go up in 8s rather than in 4s.)

There are two differences in practice:
  • 1: In some C implementations it is possible to go over the end of an array. It may be possible to write myArray[3] in C and get the program to print 4. You cannot go over the end of one of the constituent arrays in Java® because you will suffer an exception. Exactly what Stephens Miller showed earlier.
  • 2: The elements of an array of arrays may be different lengths. The elements of a 2D array are usually always the same length. SM asks whether that is an advantage; I think it is an advantage, even if you use it only rarely.
  • If you write the first code line in Java® you can reassign part of the array:
     
    Stevens Miller
    Bartender
    Posts: 1464
    32
    Netbeans IDE C++ Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:
    Java® does not support 2D arrays;


    Hmmm... Horstmann & Cornell see it my way: Java's support for multidimensional arrays is indistinguishable from 2D arrays (for multidimensional arrays of two dimensions; likewise nD arrays).

    C does.


    But you can only tell the difference because C permits pointer arithmetic. If you don't use that, your C code could as well be Java code.

    In C you may find that all the elements of all the “rows” follow one another in memory.


    But you may also find they don't, if your array variable is a pointer to a pointer. Indexing that would give an array of pointers, which, in C, would be an array of arrays. Come to think of it, none of those arrays must have the same size as any other, though you'd have to track the sizes externally, but that would only take a simple data structure.

    So in a C array like… it is possible to have the numbers in memory like this:-In a Java® ordinary (=1D) array…you would get the same sequence of numbers in memory (probably plus a 9 for length of array), but for a Java® array of arrays you would get something like this instead… where the 3 means it is a 3‑element array and the 3 hex numbers are pointers whereby the constituent arrays can be found. (They might be pointers to pointers, which you call handles. On a 64‑bit machine the pointers may go up in 8s rather than in 4s.)


    Java hides all that, which is a virtue/drawback that is hotly defended/derided by a lot of people. Regardless, you could have an array of pointers in C, like I said.

    There are two differences in practice:

  • 1: In some C implementations it is possible to go over the end of an array. It may be possible to write myArray[3] in C and get the program to print 4. You cannot go over the end of one of the constituent arrays in Java® because you will suffer an exception. Exactly what Stephens

  • Stevens!

    Miller showed earlier.

  • 2: The elements of an array of arrays may be different lengths. The elements of a 2D array are usually always the same length. SM asks whether that is an advantage; I think it is an advantage, even if you use it only rarely.
  • If you write the first code line in Java® you can reassign part of the array:


    I'm sure this discussion is extremely helpful to our Greenhorn OP .
     
    Campbell Ritchie
    Marshal
    Posts: 80874
    506
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stevens Miller wrote: . . .
    But you can only tell the difference because C permits pointer arithmetic. If you don't use that, your C code could as well be Java code.

    If you create the 2D array I showed earlier in C and then write
    printf("%d\n", array[3]);
    it may print 4. That appears not to be strictly defined in C because the behaviour seems to vary from implementation to implementation.

    . . . Come to think of it, none of those arrays must have the same size as any other, though you'd have to track the sizes externally, but that would only take a simple data structure. . . .

    But we are now moving out of the realm of simple arrays. You are either creating an array of arrays specifically or another data structure.

    Java hides all that, which is a virtue/drawback that is hotly defended/derided by a lot of people. . . .

    It is of course a virtue

    I'm sure this discussion is extremely helpful to our Greenhorn OP .

    I haven't got the time to go through this thread at the moment, but if you can work out how to split it, feel free to do so
     
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I won't go into the kind of detail that Campbell does, which probably deserves a cow, but here's something simpler to consider:

    This is not your normal multidimensional array.
     
    Campbell Ritchie
    Marshal
    Posts: 80874
    506
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stevens Miller wrote: . . . Stevens! . . .

    Sorry for the mistake.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic