• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Adding All the Elements of a Method

 
Jenny Flair
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am attempting to find what would be described as the best method for the following array named arr to correctly add all the elements together.

7 8 0 1
2 4 7 9
6 5 3 2

Would this be a suitable method and why? I am still trying to understand how for-loops work so any suggestions would be helpful. What am I looking for to ensure that all the elements are correctly added together.
I know a storage variable will be needed, which is sum.  





 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Close.
This is the length of zero'ith row only. What if other rows are a different length?
 
Piet Souris
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jenny,

Have you tested your method? If so, what was the result? Have you also tested the code for 2D arrays that are not square?
 
Jenny Flair
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, I have wondered if the arrays were of a varying length

I have also tried this method



The problem with the first one is that it won't compile, which is fine for now as I am trying to figure out which/what method would be best to approach this issue.

The potential problem with this method I see is that it is printing re twice, which doesn't exactly tell me if its printing out the column. The numbers that appear are 3 rows with 4 columns from the first post.
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jenny Flair wrote:

This is soooo... close. Change one character (like I indicated) and you're done.

[Edit] Just noticed your use of r & c are backwards.
 
Campbell Ritchie
Marshal
Posts: 65366
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your suggestion with re won't work reliably because it requires the array to be “square” like a square matrix.

Please have a look at our formatting suggestions. You should always wrap the contents of a loop in {}; you can get nasty errors if you omit the {} and change the code later.
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!