26.0k views
0 votes
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.#include using namespace std;int main() {const int NUM_VALS = 4;int testGrades[NUM_VALS];int i = 0;int sumExtra = -9999; // Assign sumExtra with 0 before your for looptestGrades[0] = 101;testGrades[1] = 83;testGrades[2] = 107;testGrades[3] = 90;/* Your solution goes here */cout << "sumExtra: " << sumExtra << endl;return 0;}

User Zombie
by
4.4k points

1 Answer

7 votes

Answer:

for (i=0;i<NUM_VALS;++i) {

if (testGrades[i] > 100) {

sumExtra += testGrades[i] - 100; } }

Step-by-step explanation:

The complete program is as follows:

#include<iostream>

using namespace std;

int main()

{const int NUM_VALS = 4;

int testGrades[NUM_VALS];

int i = 0;

int sumExtra = 0;

// Assign sumExtra with 0 before your for loop

testGrades[0] = 101;

testGrades[1] = 83;

testGrades[2] = 107;

testGrades[3] = 90;

for (i=0;i<NUM_VALS;++i) {

if (testGrades[i] > 100) {

sumExtra += testGrades[i] - 100; } }

cout << "sumExtra is: " << sumExtra << endl;

return 0;}

Lets see how the program works:

The value of NUM_VALS is 4 which is fixed throughout the program as const is used with the statement. testGrades is an array of size 4 as the value of NUM_VALS is 4. i is initialized to 0 and it is used as an index variable. The elements of testGrades[] array are 101 , 83, 107 and 90. The loop starts from 0 and continues to iterate until the value of i becomes less than NUM_VALS i.e. 4.

In the first iteration the value of i = 0 and it is positioned at element at 0-th index of testGrades array i.e. 101. Then the if statement checks if the element at i-th index ( 0 index) is greater than 100. This is true as 101 > 100. So sumExtra += testGrades[i] - 100; statement is executed in which 100 is subtracted from that element and the result is added to the sumExtra. sumExtra is initialized to 0 so the value of sumExtra becomes: 0+ 101 - 100= 1 So the value of sumExtra = 1

In the second iteration the value of i = 1 and it is positioned at element at 1st index of testGrades array i.e. 83. Then the if statement checks if the element at i-th index ( 1 index) is greater than 100. This is false as 83 < 100. So the value of sumExtra = 1

In the third iteration the value of i = 2 and it is positioned at element at 2nd index of testGrades array i.e. 107. Then the if statement checks if the element at i-th index ( 2 index) is greater than 100. This is true as 107 > 100. So sumExtra += testGrades[i] - 100; statement is executed in which 100 is subtracted from that element and the result is added to the sumExtra. sumExtra is 1 so the value of sumExtra becomes: 1+ 107 - 100= 8 So the value of sumExtra = 8

In fourth iteration the value of i = 3 and it is positioned at element at 3rd index of testGrades array i.e. 90. Then the if statement checks if the element at i-th index ( 3 index) is greater than 100. This is false as 90 < 100. So the value of sumExtra remains 8.

Finally the loop breaks as the value of i becomes 4. So the output is 8.

The program along with the output is attached as the screen shot.

Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra-example-1
User Michel Vorwieger
by
4.6k points