Write a program that will total up all of the values in an array. Try using a foreach loop since you don't need to know the position of any item.
- Complete the
total()
method so that it returns the sum of all of the items in the array passed to it. - Create some arrays in your main method to test your method.
- Don't forget to Commit and Push to save your code.
Sample Data | Sample Output |
---|---|
[-99,1,2,3,4,5,6,7,8,9,10,12345] | 12301 |
[10,9,8,7,6,5,4,3,2,1,-99] | -44 |
[10,20,30,40,50,-11818,40,30,20,10] | -11568 |
[32767] | 32767 |
[255,255] | 510 |
[9,10,-88,100,-555,1000] | 476 |
[10,10,10,11,456] | 497 |
[-111,1,2,3,9,11,20,30] | -35 |
[9,8,7,6,5,4,3,2,0,-2,-989] | -947 |
[12,15,18,21,23,1000] | 1089 |
[250,19,17,15,13,11,10,9,6,3,2,1,-455] | -99 |
[9,10,-8,10000,-5000,1000] | 6011 |