231k views
2 votes
Write a sentinel-controlled while loop that accumulates a set of integer test scores input by the user until negative 99 is entered.

1 Answer

6 votes

Answer:

Here is the sentinel-controlled while loop:

#include <iostream> //to use input output functions

using namespace std; // to identify objects like cin cout

int main(){ // start of main() function body

int test_score; // declare an integer variable for test scores

//prompts user to enter test scores and type-99 to stop entering scores

cout << "Enter the test scores (enter -99 to stop): ";

cin >> test_score; // reads test scores from user

while (test_score != -99){ // while loop keeps executing until user enters -99

cin >> test_score; } } // keeps taking and reading test scores from user

Step-by-step explanation:

while loop in the above chunk of code keeps taking input scores from user until -99 is entered. Sentinel-controlled loops keep repeating until a sentinel value is entered. This sentinel value indicates the end of the data entry such as here the sentinel value is -99 which stops the while loop from iterating and taking the test score input from user.

The complete question is that the code should then report how many scores were entered and the average of these scores. Do not count the end sentinel -99 as a score.

So the program that takes input scores and computes the number of scores entered and average of these scores is given below.

#include <iostream> // to use input output functions

using namespace std; // to identify objects like cin cout

int main(){ //start of main function body

double sum = 0.0; // declares sum variable to hold the sum of test scores

int test_score,count =0;

/* declares test_scores variable to hold the test scores entered by user and count variable to count how many test scores input by user */

cout << "Enter the test scores (or -99 to stop: ";

//prompts user to enter test scores and type-99 to stop entering scores

cin >> test_score; // reads test scores from user

while (test_score != -99){ // while loop keeps executing until user enters -99

count++; /* increments count variable each time a test cores is input by user to count the number of times user entered test scores */

sum = sum + test_score; // adds the test scores

cin >> test_score;} // reads test scores from user

if (count == 0) // if user enters no test score displays the following message

cout << "No score entered by the user" << endl;

else //if user enters test scores

//displays the numbers of times test scores are entered by user

cout<<"The number of test scores entered: "<<count;

/* displays average of test scores by dividing the sum of input test scores with the total number of input test scores */

cout << "\\ The average of " << count << " test scores: " <<sum / count << endl;}

The program along with its output is attached.

Write a sentinel-controlled while loop that accumulates a set of integer test scores-example-1
User Isaura
by
4.7k points