This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding Values in arrays.

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On one of my lab week tutorials we had to do this, add the values of and array, the outcome would be 4950.

I wrote my program that works, and gives the value, 4950



Is there an easiar way to achieve this?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you didn't need the if statement before the System.out at the end since this would be checked in every loop. You could have just put it after the loop completed.

Also, the array itself was unnecessary since you defined "anArray[i]=i" at the beginning of every loop. You could have removed the array completely and just done "add = add + i" or "add += i" for short.

Finally, you could have used mathematics to determine that the sum of integers from 1 to N is defined to be: (n^2 + n)/2 although the proof of this normally requires induction.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, note that you get 4950 since you are really summing from 0 to 99, not 1..100. If you apply the formula to 99 it works out to be exactly 4950. Keep in mind this formula is a shortcut and may not be what your teacher wanted. Its not often such short cuts exist but when they do its great to be able to use them.
[ November 08, 2005: Message edited by: Scott Selikoff ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips, i didnt fully understand what you mean by remove array, i presume you mean the "anArray[i]+anArray[i]"?



Hows that?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think you misunderstand. For the first part where I said to remove the array, I assumed your goal was the target value of 4950, not the array so you could do:

But the second piece of advise (n^2+n)/2 there is no loop needed, its one line of code really:

For the equation no loop is neccessary, its extremely fast with O(1) running time compared to O(n) for the loop.
[ November 08, 2005: Message edited by: Scott Selikoff ]
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may take a look at java.reflect.Array
Api is at
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Array.html

But it don't provide you a sum operation.

Alternatively you can create a collection by implementing collection and give it a sum method. The custom collection could be a collection of primitives or non-primitives. toArray method can be implemented to convert the Collection to an Array if needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic