Modify an array's elements using other elements 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 program to this question can be given as:

Program:

#include<stdio.h>  //header file.

int main()

//main function

{

  int i;  //define variables.

   int bonusScores[]={10,20,30,40};  //define array.

   printf("Values:");  //message

  for (i = 0; i < 4; ++i)  //loop for print values.

{

printf("%d ", bonusScores[i]);

}

for (i = 0; i <4; i++)   //loop for convert values

{

      if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))  //check array elements

       {

          bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);   //add values

      }

      else

{           bonusScores[i] = bonusScores[i];

     }

   }

   printf("\nReturn values:");  //message

   for (i = 0; i < 4; ++i) //loop for print values

  {

      printf("%d ", bonusScores[i]);      //print values

  }

  printf("\n");

return 0;

}  

Output:

Values:10 20 30 40  

Return values:30 50 70 40  

Explanation:

In the above c++ programming code firstly we define the header file. Then we define the main function in this function we define variable that name is given in the question that is i. Variable i is used in the loop for a print array. Then we define and initialize an array. The array name is and elements are define in the question that is bonusScores[]={10,20,30,40}. Then we use the loop for print array elements. We use the second time in this loop we check array elements by using the condition statements in if block we add the elements and in else we print last value. At the last, we use the loop for print array.

ACCESS MORE
EDU ACCESS