• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Summing arrays

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created two different array to test, I want to take a number that a user inputs and multiply it up to the same number that was inputted, so if I enter a 1 it should go 1 * 1, 1*2 and so forth, then add those results to the array and sum the array. One works but I wanted to try to shorten the code.

The working one works fine:


This one on the other hand take the last number and multiplies the last number in the array by two:


My question is why does the first one work, but the second one doesn't? Also I add println and seen something that looked like a multidimensional array, why is this?  
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody on this website, I think Fred Rosenberger, has a jokey signature reading a follows, which you can find from Martin Fowler's site:-

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.

I am afraid you have given us a good example of how poorly‑chosen names can confuse people, particularly the original writer of the code.
You started off with a method called getXXX(), though is isn't a get method at all, then you showed us a for‑each loop where the value in the loop is called index, even though it isn't the index. When writing for‑each loops, most people use a one‑letter name for the variable in the loop: for (int i : myArray) ... By the way, pronounce that, “for each int i in myArray ...”
Then you tried an ordinary for loop with the loop variable index, which this time does in fact represent an index. That is why you are getting the wrong result. Again, most people would use a one‑letter name: for (int i = 0; i < myArray.length; i++) ...
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wes James wrote:. . . I add println and seen something that looked like a multidimensional array, why is this?  

Don't know, but print statements are usually very useful for helping solve such problems. Please explain more, showing details of what you expected to be printed, and what you actually saw.
 
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To put it in another way: You got exactly what you told the computer to give you. However, because you used poorly chosen names, you confused yourself and thought you were asking for / getting something else.

You use the name index in both versions but if you look more closely, you're using it to represent different things, as Campbell has already explained. In one version, it actually represents an index into the array, while in another, it represents a value. Unfortunately, the version where the semantics are incorrect (the one where index actually represents the array element value) is the one that works, which leads to even more confusion.

Something like the code below expresses what the code is doing correctly:

By the way, another way to do this is:

 
Wes James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While this code is equivalent to a napkin doodle at 2 am in a bar, I still learned a valuable lesson as to why naming convention is so important.  

Don't know, but print statements are usually very useful for helping solve such problems. Please explain more, showing details of what you expected to be printed, and what you actually saw.



I expected it to sum the numbers from the array and print the total, but what i got was this below.




 
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reinforce what has already been said:
Is the variable index really an index?
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wes James wrote:. . . what i got was this below.

That looks to me as if you were printing out the array whilst populating it. You have three lines each showing the state of the array after you have filled one element with index × 2. Change the print statement to...and it should become obvious what is happening.
 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

As been mentioned from other aspect, try to be very precise on what you are saying and what you are doing. For instance the subject of the topic is "Summing arrays". Really you can't sum an array. What you actually can do, is to sum elements of an array if they appear to be numbers in some sense.
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This one on the other hand take the last number and multiplies the last number in the array by two:


The method's name above also perhaps requires some tweak.

Read out loud what you described the method supposed to be doing, and read also out loud method's name. Do they communicate the same idea behind? Not to me personally. While reading the description I could imagine the implementation, but looking to method's name I don't even understand what that means.

"Get numbers for sums..." - very weird, isn't it? Or "Get numbers for sum s..." ? Not clearer either.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!