Answer:
The c++ program for the given scenario is as below.
#include <iostream>
using namespace std;
int main() {
int n = 0;
double sum, avg = 0;
cout << "Enter the GPA for all the courses you are enrolled in " << endl;
do
{ n = n+1;
cin >> sum;
avg = avg + sum;
}while( sum != 0 );
n = n-1;
avg = avg/n;
cout << "The CGPA is " << avg << endl;
return 0;
}
OUTPUT
Enter the GPA for all the courses you are enrolled in
1
2
3
4
0
The CGPA is 2.5
Step-by-step explanation:
The program is designed to take user input. There is no validation to test the user input as this is not mentioned in the question.
The program works as explained below.
1- The program initializes variables to keep count of GPA scores entered in an integer variable, to hold the sum of all GPA scores in a double variable and to hold the average of all the GPA scores in a double variable.
2- The user input is taken inside the do-while loop for GPA scores. Every time a score is entered, the value of n is incremented.
n = n+1;
3- The score is stored in variable sum. This score is assigned to variable avg.
cin >> sum;
avg = avg + sum;
4- Every new score is stored in sum and added to avg.
5- The loop continues till user enters 0.
while( sum != 0 );
6- When the loop is terminated, the value of n is decremented.
n = n-1;
7- The sum of all scores is stored in variable avg. This sum is divided by n and this value is assigned to variable avg itself.
avg = avg/n;
8- This value is called the CGPA and is displayed on the screen.
9- The program ends with a return statement.
return 0;