C++ Proagram The first element is 30 or 10 + 20, the second element is 50 or 20
ID: 3866544 • Letter: C
Question
C++ Proagram
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.
Follow this code
#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
cout << bonusScores[i] << " ";
}
cout << endl;
return 0;
}
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.
Follow this code
#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
cout << bonusScores[i] << " ";
}
cout << endl;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0,j=0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
/* Your solution goes here */
i=0;
while(i<SCORES_SIZE-1)
{
bonusScores[i]=bonusScores[i]+bonusScores[i+1];
i++;
}
for (i = 0; i < SCORES_SIZE; ++i)
{
cout << bonusScores[i] << " ";
}
cout << endl;
return 0;
}
Output:
30 50 70 40
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.