Results 1 to 8 of 8
- 04-19-2011, 09:24 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
how do you add up integers in an array?
seems like a simple question but for some reason it's avoiding me :/
say in a situation where you are given the array like
public int addArray ( int[] array ) {
and in the array was something like "addArray({1,2,3});" but it was not known.
how would you return 6?
many thanks for your replies :)
- 04-19-2011, 09:41 AM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
You need a variable that will keep the total.
Then you need a for..loop that will go through the array and add the value held to the total.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 04-19-2011, 09:42 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-19-2011, 09:53 AM #4
but if the signature of the method is addArray ( int[] array ) you will able to pass only ints. anyway.
@shazakala: assuming the programmer pass only ints array then inside the methods you could use a for-loop with (int i=0; i < array.length; i) to iterate thorugh the array and sum the numbers and return the sum value.Last edited by j2me64; 04-19-2011 at 09:56 AM.
- 04-19-2011, 09:58 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
thanks j2me64 :D solved
- 04-19-2011, 10:11 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-19-2011, 10:13 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
thats not the point, it was an example, all you need is the gist, and j2 had it and helped XD
- 04-19-2011, 10:32 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Printing repeating integers in a given array?
By baumboards in forum New To JavaReplies: 7Last Post: 01-24-2011, 07:08 PM -
Conversion of an arraylist to a 2D array of integers
By yanipao in forum New To JavaReplies: 7Last Post: 02-04-2010, 11:09 AM -
file input: array of integers
By hannes in forum New To JavaReplies: 8Last Post: 01-27-2010, 03:44 PM -
Error if array contains duplicate integers
By lithium002 in forum New To JavaReplies: 4Last Post: 12-05-2009, 08:58 AM -
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks