0 like 0 dislike
56 views
in Computer Science by (1.0m points)
Describe the sentinal control loop and give example Describe the sentinal control loop and give example

1 Answer

0 like 0 dislike
by (1.0m points)

In the previous class average example, the problem statement specified the number of students, so the number of grades (10) was known in advance. In this example, no indication is given of how many grades the user will enter during the program's execution. The program must process an arbitrary number of grades. How can the program determine when to stop the input of grades? How will it know when to calculate and print the class average?

One way to solve this problem is to use a special value called a sentinel value (also called a signal value, a dummy value or a flag value) to indicate "end of data entry." The user types grades in until all legitimate grades have been entered. The user then types the sentinel value to indicate that the last grade has been entered.

Clearly, the sentinel value must be chosen so that it cannot be confused with an acceptable input value. Grades on a quiz are normally nonnegative integers, so –1 is an acceptable sentinel value for this problem. Thus, a run of the class average program might process a stream of inputs such as 95, 96, 75, 74, 89 and –1. The program would then compute and print the class average for the grades 95, 96, 75, 74 and 89. Since –1 is the sentinel value, it should not enter into the averaging calculation.

Implementing Sentinel-Controlled Repetition in Class GradeBook

Figures 4.9 and 4.10 show the C++ class GradeBook containing member function deter-mineClassAverage that implements the class average algorithm with sentinel-controlled repetition. Although each grade entered is an integer, the averaging calculation is likely to produce a number with a decimal point. The type int cannot represent such a number, so this class must use another type to do so. C++ provides several data types for storing floating-point numbers, including float and double. The primary difference between these types is that, compared to float variables, double variables can typically store numbers with larger magnitude and finer detail (i.e., more digits to the right of the decimal point—also known as the number's precision). This program introduces a special operator called a cast operator to force the averaging calculation to produce a floating-point numeric result. These features are explained in detail as we discuss the program.

Related questions

0 like 0 dislike
1 answer 57 views
0 like 0 dislike
1 answer 155 views
asked Feb 9, 2019 in Biology by danish (1.0m points)
0 like 0 dislike
0 answers 52 views
0 like 0 dislike
1 answer 34 views
0 like 0 dislike
1 answer 51 views
0 like 0 dislike
1 answer 39 views
0 like 0 dislike
0 answers 55 views
0 like 0 dislike
0 answers 46 views
0 like 0 dislike
1 answer 70 views
asked Feb 11, 2019 in Computer Science by danish (1.0m points)
0 like 0 dislike
0 answers 49 views
Welcome to Free Homework Help, where you can ask questions and receive answers from other members of the community. Anybody can ask a question. Anybody can answer. The best answers are voted up and rise to the top. Join them; it only takes a minute: School, College, University, Academy Free Homework Help

19.4k questions

18.3k answers

8.7k comments

6.2k users

Free Hit Counters
...