Write a for loop that sets each array element in bonusScores to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: If bonusScores = [10, 20, 30, 40], then after the the loop bonusScores = [30, 50, 70, 40] The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last element remains the same.

Respuesta :

Answer:

The C program is commented in the explanation

Explanation:

I am going to write a C program, a function that receives an array and the array length.

I can update each position with a for loop. This loop is going to end at the next to lastPosition(so, i < n-1);

void sum(int * bonusScores, int n){

int i;

for(i = 0; i < n-1; i++){

bonusScores[i] = bonusScores[i] + bonusScores[i+1]

}

}

This should do it :)

Answer:

#Python

1. bonusScores = [10, 20, 30, 40]

2. print("Before",bonusScores)

3. for i in range(len(bonusScores)-1):

4.     bonusScores[i] = bonusScores[i] + bonusScores[i+1]

5. print("After",bonusScores)

Explanation:

  1. On the first line, we define our array bonusScores
  2. On line two we print how this array looks like before making any changes
  3. On line 3 we create a for loop to iterate over the length of the array omitting the last element (len(bonusScores)-1)
  4. On line four we sum the current element with the next one
  5. On line five we print the final result
Ver imagen mateolara11