Answer:
The program in cpp is given below.
#include <iostream>
using namespace std;
int main()
{
//variables declared to hold sum, count and average of movie ratings
int sum=0;
int count=0;
int rating;
double avg;
//audience is prompted to enter ratings
do{
std::cout << "Enter the rating (0 to 4): ";
cin>>rating;
if(rating>4)
std::cout << "Invalid rating. Please enter correct rating again." << std::endl;
if(rating>=0 && rating<=4)
{
sum=sum+rating;
count++;
}
if(rating<0)
break;
}while(rating>=0);
//average rating is computed
avg=sum/count;
std::cout << "The average rating for the movie is " << avg << std::endl;
return 0;
}
OUTPUT
Enter the rating (0 to 4): 1
Enter the rating (0 to 4): 2
Enter the rating (0 to 4): 0
Enter the rating (0 to 4): 5
Invalid rating. Please enter correct rating again.
Enter the rating (0 to 4): 1
Enter the rating (0 to 4): -1
The average rating for the movie is 1
Explanation:
1. Variables are declared to hold the rating, count of rating, sum of all ratings and average of all the user entered ratings.
2. The variables for sum and count are initialized to 0.
3. Inside do-while loop, user is prompted to enter the rating for the movie.
4. In case the user enters a rating which is out of range, the user is prompted again to enter a rating anywhere from 0 to 4.
5. The loop executes until a negative number is entered.
6. Inside the loop, the running total sum of all the valid ratings is computed and the count variable is incremented by 1 for each valid rating.
7. Outside the loop, the average is computed by dividing the total rating by count of ratings.
8. The average rating is then displayed to the console.
9. The program is written in cpp due to simplicity since all the code is written inside main().