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.

Respuesta :

Answer: The program in c++ language for the given scenario is given below.

#include <iostream>

using namespace std;

int main() {    

int sumExtra = 0, len;

int testGrades[len];

// this input determines the length of the array

cout<<"Enter the number of grades to be entered : ";

cin>>len;

 

for(int j=0; j<len; j++)

{

    cout<<endl<<"Enter grade "<<j+1;

    cin >> testGrades[j];

}

 

for(int i=0; i<len; i++)

{

    if(testGrades[i] > 100)

        sumExtra =  sumExtra + ( testGrades[i] - 100 );

    else

        continue;

}

cout<<endl<<"Sum of extra credit = " << sumExtra;

return 0;

}

Explanation:

Header files are imported for input/ output alongwith the namespace.

#include <iostream>

using namespace std;

All variables and array is declared of type integer.

int sumExtra = 0, len;

int testGrades[len];

User is prompted for the number of grades to be entered. This number is stored in variable len, which determines the length of the array, testGrades.

testGrades[len];

Using for loop, grades are entered for each index.

for(int j=0; j<len; j++)

{

     cout<<endl<<"Enter grade "<<j+1;

     cin >> testGrades[j];

}

Using another for loop and if-else statement, if the grade > 100, extra credit is added and sum is stored in the variable, sumExtra.

for(int i=0; i<len; i++)

{

     if(testGrades[i] > 100)

          sumExtra =  sumExtra + ( testGrades[i] - 100 );

     else

          continue;

}

This is followed by displaying the message with sum of the extra credit.

The main() function has the return type int. Hence, integer 0 is returned at the end of the program.

In the above, endl keyword is used to insert break line. The following message will be displayed on the new line. This is another alternative for the newline character, \n.

The given program does not implements a class since the logic is simple. Also, the question does not specify that the class and object should be used.

RELAXING NOICE
Relax