Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a loop that subtracts 1 from each element in lowerScores. If the element w

ID: 3674165 • Letter: W

Question

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}.

#include <iostream>
using namespace std;

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

lowerScores[0] = 5;
lowerScores[1] = 0;
lowerScores[2] = 2;
lowerScores[3] = -3;

/* Your solution goes here */

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

return 0;
}

Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}.

Note: These activities may test code with different test values. This activity will perform two tests, the first with a 4-element array (newScores = {10, 20, 30, 40}), the second with a 1-element array (newScores = {199}). See How to Use zyBooks.

Also note: If the submitted code tries to access an invalid array element, such as newScores[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message.

#include <iostream>
using namespace std;

int main() {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i = 0;

oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;

/* Your solution goes here */

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

return 0;
}

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:

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 = 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

1.

#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int lowerScores[SCORES_SIZE];
int i = 0;
lowerScores[0] = 5;
lowerScores[1] = 0;
lowerScores[2] = 2;
lowerScores[3] = -3;
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
       lowerScores[i] = lowerScores[i] - 1; // subtracting 1
       if(lowerScores[i] < 0)// if it is negative
           lowerScores[i] = 0;
}

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

/*

Output:

4 0 1 0

*/

2.

#include <iostream>
using namespace std;
int main() {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i = 0;
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;
/* Your solution goes here */
// putting all elements of oldScores(from second element) into newScores
for (i = 1; i < SCORES_SIZE; ++i) {
newScores[i-1] = oldScores[i];
}
//putting first element of oldScores at last of newScores
newScores[SCORES_SIZE-1] = oldScores[0];


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

/*

Output:

20 30 40 10

*/

3.

#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-1; ++i) {
   // adding
bonusScores[i] = bonusScores[i] + bonusScores[i+1];
}

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

/*

Output:

30 50 70 40

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote