Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do we access totals of columns in 2D arrays to calculate their difference?  RSS feed

 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, rancheros!
Can't figure out how to do it.
After using nested for loops, I have totals of all columns.
total += qS[row][col];
}
then I SOP it
then I need to find out the difference between adjacent total columns. How do I access the elements of this new 1D array?
If you need the code - I can post it (but it would be an obvious solution to a homework, and I do not want to violate)
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what you posted you don't have an array, but just a plain variable, but if you have a 1D array you address the column with the same column index you used to address your 2D array.
 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is not an array - then what do you call that list of numbers it outputs as total?

**************************************************
Total
qS 150 190 220 250
*******************************************************

And how do you access them to calculate the difference - it does not recognize total outside the loop - requesting create a local variable total, or field etc?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I call it is an int, that you are summing the individual columns separately and printing then using a non-linefeed call. When col is 0 you calculate the sum of column 0 all the way up until you get to qS[0].length-1. Your variable total is not an array, it is an int: See where you do a total = 0? then do you see where you do a total += qS[row][col]? You are summing the individual columns and by clever placement of the print and sum functions you are making it appear to be an array, but it is not.

If you have any doubt as to what I am saying, look to where your closing curly brace if for your first "for" statement--you are not showing it, but it makes your print inside the column loop in your double for loops. So turn it into an array and use the col variable to address it.

Lena Sergeenko wrote:If it is not an array - then what do you call that list of numbers it outputs as total?

**************************************************
Total
qS 150 190 220 250
*******************************************************

And how do you access them to calculate the difference - it does not recognize total outside the loop - requesting create a local variable total, or field etc?
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:. . . look to where your closing curly brace . . .
You can get no end of confusion if you get braces in the wrong places. I think the solution to that problem is to write the code backwards, as shown in that link. Put the {} first and then put the code inside the {}.
 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys! I wanted to know if it was possible at all - will continue beating my head at the wall! Probably will ask for more guidance later - LOL!
(LOL - riming was unintentional))))
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were told exactly how to solve your problem, all that was not done was code provided.
Lena Sergeenko wrote:
... I wanted to know if it was possible at all - will continue beating my head at the wall!
 
Lena Sergeenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I meant continue working on Java - not necessarily this particular one. )))
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!