Question

In: Computer Science

Write a loop that sets each array element to the sum of itself and the next...

Write a loop that sets each array element 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:

Initial scores:        10, 20, 30, 40
Scores after the loop: 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.

#include <iostream>
using namespace std;

int main() {
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i;

for (i = 0; i < SCORES_SIZE; ++i) {
cin >> bonusScores[i];
}

/* Your solution goes here */

for (i = 0; i < SCORES_SIZE; ++i) {
cout << bonusScores[i] << " ";
}
cout << endl;

return 0;
}

Solutions

Expert Solution

Solution:

Look at the code for better understanding.............

Screenshot of the code:

Output:

Code to copy:

#include <iostream>
using namespace std;
int main() {
        const int SCORES_SIZE = 4;
        int bonusScores[SCORES_SIZE];
        int i;

        for (i = 0; i < SCORES_SIZE; ++i) {
                cin >> bonusScores[i];
        }

        /* Your solution goes here */
        for(i=0;i<SCORES_SIZE-1;i++)
        {
                bonusScores[i] += bonusScores[i+1];
        }
        for (i = 0; i < SCORES_SIZE; ++i) {
        cout << bonusScores[i] << " ";
        }
        cout << endl;

        return 0;
}

I hope this would help.............:-))


Related Solutions

Write a loop that subtracts 1 from each element in lowerScores. If the element was already...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. please bold the solution import java.util.Scanner; public class StudentScores { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int SCORES_SIZE = 4; int[] lowerScores = new int[SCORES_SIZE]; int i; for (i = 0; i < lowerScores.length; ++i) {...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Assume an MxN array has been filled, write a function to calculate the sum of each...
Assume an MxN array has been filled, write a function to calculate the sum of each column which should be stored into a third 1D array of size N. code in C++
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT