| Author |
Comparing the sums of rows and columns in a 2D array
|
Lonnie Wood
Greenhorn
Joined: Feb 16, 2011
Posts: 15
|
|
Here's the code I have for summing the totals of my columns and my rows.
I'm trying to figure out how to say if row one does not equal row two. I'm just not getting the syntax here.
If anyone could help I'd appreciate it.
Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Lonnie Wood wrote:
I'm trying to figure out how to say if row one does not equal row two. I'm just not getting the syntax here.
Well, you are not saving the row totals (or the column totals) anywhere. You calculate a row total, print it, and then calculate the next row using the same variables.
You can't compare one row total to another because your program never have two row totals (at the same time) to allow for any comparison -- much less, for a specific comparison.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Lonnie Wood
Greenhorn
Joined: Feb 16, 2011
Posts: 15
|
|
Henry,
I had tried about twenty different comparison strings but nothing worked.
I'm calculating the totals, but how do I store the value is a better question I guess. How can I say that columnTotal for column0 = columnTotal0 and columnTotal for column1 = columnTotal1?
Hence the reference to a syntax problem.
Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Lonnie Wood wrote:
I had tried about twenty different comparison strings but nothing worked.
I'm calculating the totals, but how do I store the value is a better question I guess. How can I say that columnTotal for column0 = columnTotal0 and columnTotal for column1 = columnTotal1?
Have you tried creating an array for the column totals? Then you can say "columnTotals[col]" as the total for a particular column.
Henry
|
 |
Lonnie Wood
Greenhorn
Joined: Feb 16, 2011
Posts: 15
|
|
Henry,
No I haven't but that makes sense. Let me try that out and let you know.
Thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
I take it you have a "rectangular" array, ie all the rows are the same length. Otherwise, if a row is longer than the first row, you will omit its last element and get a wrong answer. If a row is shorter, you will get the far less serious result of an Exception.
|
 |
 |
|
|
subject: Comparing the sums of rows and columns in a 2D array
|
|
|