15.8k views
3 votes
Write a program that prompts the user to enter seven test scores and then prints the average test score. (Assume that the test scores are decimal numbers.)

1 Answer

5 votes

Answer:

The cpp program for the scenario is given below.

#include <iostream>

using namespace std;

int main() {

// array to store seven test scores

double scores[7];

// variable to store sum of all test scores

double sum=0;

// variable to store average of all test scores

double avg_score;

// user is prompted to enter seven test scores

cout<<"Enter the test scores ";

// user input is taken inside for loop

// for loop executes seven times to take input for seven test scores

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

{

cin>>scores[j];

sum = sum + scores[j];

}

// average of test scores is calculated and assigned

avg_score = sum/7;

cout<<"The average of all entered test scores are "<< avg_score <<endl;

return 0;

}

OUTPUT

Enter the test scores 90

88

76

65

56.89

77.11

68

The average of all entered test scores are 74.4286

Step-by-step explanation:

The program execution is explained below.

1. An array, scores, is declared of size seven to store seven user-entered test scores.

2. Variables, sum and avg_score, are declared to store the sum and average of all the seven test score values. The variable sum is initialized to 0.

3. User is prompted to enter the test scores using cout keyword.

4. Inside for loop, all seven test scores are stored inside array, scores, using cin keyword.

5. Inside the same for loop, the values in the array, scores, are added and assigned to the variable sum.

6. Outside the for loop, the average score is calculated and assigned to the variable avg_score.

7. At the end, the average score is displayed. New line is inserted using endl after the average score is displayed.

8. In order to store decimal values, all variables and the array are declared with double datatype.

9. The main() has return type of integer and thus, ends with a return statement.

10. The iostream is included to enable the use of keywords like cin, cout, endl and others.

User Wes Turner
by
4.8k points