• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Rancher
Posts: 1049
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Marshal
Posts: 76450
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1049
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I meant continue working on Java - not necessarily this particular one. )))
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic