-1

I am given an array of name 'array[]' with an unknown amount of elements. I am asked to return the sum of all of the values in the array, and I tried to write a for loop covering all of the values in the array and adding them together. I think that I am overthinking this, and I should be able to do this in one command but I can't remember.

Here is what I am given:

int arraySum(int[] array)

I tried:

for(int i = 0; i <= array.length; i++){ 
    int sum = array[i]; 
} 
return sum;
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Inezda1
  • 21
  • 1
  • 2
  • 4

6 Answers6

4

Use Java 8 Stream api which gives sum in one line .

Arrays.stream(array).sum();
Rai
  • 126
  • 5
3

You have the problem here:

int sum = array[i]; 

because you are creating a new variable sum each time the loop make an iteration. You should create your sum variable before using it on the loop:

int arraySum( int[] array) {
   int sum = 0;
   for(int i = 0; i <= array.length; i++){ 
       sum = sum + array[i]; 
   } 

   return sum;
}

Look that you have to initialize it to zero.

Also, remember that the arrays starts at the position 0. Because of that, you should use

i < array.length

instead of

i <= array.length

in your loop condition.

I expect it will be helpful for you!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
2

This should do the trick:

int arraySum( int[] array) {
    int sum = 0;
    for (int i : array) {
        sum += i;
    }
    return sum;
}
1

You need to declare (and initialize sum). Usually, a sum starts at 0. Then, you might use a for-each loop - you can read it like for-each value in the array, do something with the value - like add it to sum. Finally, return the sum. Like,

int arraySum(int[] array) {
    int sum = 0;
    for (int value : array) {
        sum += value;
    }
    return sum;
}

Or, using an index variable like

int arraySum(int[] array) {
    int sum = 0;
    for (int index = 0; index < array.length; index++) {
        sum += array[index];
    }
    return sum;
}

Or, you could use the new Java 8+ stream api with Arrays.stream(int[]) and IntStream.sum() like

int arraySum(int[] array) {
    return Arrays.stream(array).sum();
}

And, as pointed out by @PaulBoddington, you could also use IntStream.of(int...) and IntStream.sum() like

int arraySum(int[] array) {
    return IntStream.of(array).sum();
}
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The scope of sum is within the for loop. You should declare it outside for loop to use it.

Also, the addition of sum isn't correct as well as the for loop condition.

The updated code should be something similar to following

int sum = 0;    
for(int i = 0; i < array.length; i++){ 
        sum+ = array[i]; 
 } 
 return sum;
Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
0

Declare variable sum outside the loop, else it will re initiate every time. Also, use += operator to add itself.

int sum = 0; 
for(int i = 0; i <= array.length; i++){ 
   sum += array[i]; 
 } 
return sum;
cw fei
  • 1,534
  • 1
  • 15
  • 33