5.9. Formulating Algorithms: Sentinel-Controlled Repetition
Let us generalize Section 5.8's class-average problem. Consider the following problem:
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 application's execution. The application must process an arbitrary number of grades. How can it 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." This is called sentinel-controlled repetition . The user enters grades until all legitimate grades have been entered. The user then types the sentinel value to indicate that no more grades will be entered. Sentinel-controlled repetition is often called indefinite repetition because the number of repetitions is not known by the application before the loop begins executing.
Clearly, a sentinel value must be chosen that cannot be confused with an acceptable input value. Grades on a quiz are nonnegative integers, so 1 is an acceptable sentinel value for this problem. Thus, a run of the class-average application might process a stream of inputs such as 95, 96, 75, 74, 89 and 1. The application 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.
Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement: The Top and First Refinement
We approach the class-average application with a technique called top-down, stepwise refinement , which is essential to the development of well-structured applications. We begin with a pseudocode representation of the top a single statement that conveys the overall function of the application:
The top is, in effect, a complete representation of an application. Unfortunately, the top rarely conveys sufficient detail from which to write a C# application. So we now begin the refinement process. We divide the top into a series of smaller tasks and list these in the order in which they will be performed. This results in the following first refinement :
This refinement uses only the sequence structurethe steps listed should execute in order, one after the other.
Proceeding to the Second Refinement
The preceding Software Engineering Observation is often all you need for the first refinement in the top-down process. To proceed to the next level of refinement, the second refinement , we specify individual variables. In this example, we need a running total of the numbers, a count of how many numbers have been processed , a variable to receive the value of each grade as it is input by the user and a variable to hold the calculated average. The pseudocode statement
can be refined as follows :
Only the variables total and counter need to be initialized before they are used. The variables average and grade (for the calculated average and the user input, respectively) need not be initialized, because their values will be replaced as they are calculated or input.
The pseudocode statement
requires a repetition structure (i.e., a loop) that successively inputs each grade. We do not know in advance how many grades are to be processed, so we will use sentinel-controlled repetition. The user enters grades one at a time. After entering the last grade, the user enters the sentinel value. The application tests for the sentinel value after each grade is input and terminates the loop when the user enters the sentinel value. The second refinement of the preceding pseudocode statement is then
In pseudocode, we do not use braces around the statements that form the body of the while structure. We simply indent the statements under the while to show that they belong to the while . Again, pseudocode is only an informal application-development aid.
The pseudocode statement
can be refined as follows:
We are careful here to test for the possibility of division by zeroa logic error that, if undetected, would cause the application to fail or produce invalid output. The complete second refinement of the pseudocode for the class-average problem is shown in Fig. 5.8.
Figure 5.8. Class-average problem pseudocode algorithm with sentinel-controlled repetition.
In Fig. 5.5 and Fig. 5.8, we included some completely blank lines and indentation in the pseudocode to make it more readable. The blank lines separate the pseudocode algorithms into their various phases and set off control statements, and the indentation emphasizes the bodies of the control statements.
The pseudocode algorithm in Fig. 5.8 solves the more general class-averaging problem. This algorithm was developed after only two refinements. Sometimes more refinements are necessary.
Implementing Sentinel-Controlled Repetition in Class GradeBook
Figure 5.9 shows the C# class GradeBook containing method DetermineClassAverage that implements the pseudocode algorithm of Fig. 5.8. Although each grade is an integer, the averaging calculation is likely to produce a number with a decimal pointin other words, a real number or floating-point number. The type int cannot represent such a number, so this class uses type double to do so.
Figure 5.9. GradeBook class that solves the class-average problem using sentinel-controlled repetition.
(This item is displayed on pages 192 - 193 in the print version)
In this example, we see that control statements may be stacked on top of one another (in sequence) just as a child stacks building blocks. The while statement (lines 5462) is followed in sequence by an if ... else statement (lines 6677). Much of the code in this application is identical to the code in Fig. 5.6, so we concentrate on the new features and issues.
Line 42 declares double variable average . This variable allows us to store the calculated class average as a floating-point number. Line 46 initializes gradeCounter to , because no grades have been entered yet. Remember that this application uses sentinel-controlled repetition to input the grades from the user. To keep an accurate record of the number of grades entered, the application increments gradeCounter only when the user inputs a valid grade value.
Program Logic for Sentinel-Controlled Repetition vs. Counter-Controlled Repetition
Compare the program logic for sentinel-controlled repetition in this application with that for counter-controlled repetition in Fig. 5.6. In counter-controlled repetition, each repetition of the while statement (e.g., lines 5056 of Fig. 5.6) reads a value from the user, for the specified number of repetitions. In sentinel-controlled repetition, the application reads the first value (lines 5051 of Fig. 5.9) before reaching the while . This value determines whether the application's flow of control should enter the body of the while . If the condition of the while is false, the user entered the sentinel value, so the body of the while does not execute (because no grades were entered). If, on the other hand, the condition is true, the body begins execution, and the loop adds the grade value to the total (line 56) and adds 1 to gradeCounter (line 57). Then lines 6061 in the loop's body input the next value from the user. Next, program control reaches the closing right brace of the body at line 62, so execution continues with the test of the while 's condition (line 54). The condition uses the most recent grade input by the user to determine whether the loop's body should execute again. Note that the value of variable grade is always input from the user immediately before the application tests the while condition. This allows the application to determine whether the value just input is the sentinel value before the application processes that value (i.e., adds it to the total ). If the sentinel value is input, the loop terminates; the application does not add 1 to the total .
After the loop terminates, the if ... else statement at lines 6677 executes. The condition at line 66 determines whether any grades were input. If none were input, the else part (lines 7677) of the if ... else statement executes and displays the message "No grades were entered" , and the method returns control to the calling method.
Notice the while statement's block in Fig. 5.9 (lines 5562). Without the braces, the loop would consider its body to be only the first statement, which adds the grade to the total . The last three statements in the block would fall outside the loop's body, causing the computer to interpret the code incorrectly as follows:
while ( grade != -1 ) total = total + grade; // add grade to total gradeCounter = gradeCounter + 1 ; // increment counter // prompt for input and read next grade from user Console.Write( "Enter grade or -1 to quit: " ); grade = Convert.ToInt32( Console.ReadLine() );
The preceding code would cause an infinite loop in the application if the user did not enter the sentinel -1 at line 51 (before the while statement).
Explicitly and Implicitly Converting Between Simple Types
If at least one grade was entered, line 69 of Fig. 5.9 calculates the average of the grades. Recall from Fig. 5.6 that integer division yields an integer result. Even though variable average is declared as a double (line 42), the calculation
average = total / gradeCounter;
loses the fractional part of the quotient before the result of the division is assigned to average . This occurs because total and gradeCounter are both integers, and integer division yields an integer result. To perform a floating-point calculation with integer values, we must temporarily treat these values as floating-point numbers for use in the calculation. C# provides the unary cast operator to accomplish this task. Line 69 uses the ( double ) cast operatora unary operatorto create a temporary floating-point copy of its operand total (which appears to the right of the operator). Using a cast operator in this manner is called explicit conversion . The value stored in total is still an integer.
The calculation now consists of a floating-point value (the temporary double version of total ) divided by the integer gradeCounter . C# knows how to evaluate only arithmetic expressions in which the operands' types are identical. To ensure that the operands are of the same type, C# performs an operation called promotion (or implicit conversion ) on selected operands. For example, in an expression containing values of the types int and double , the int values are promoted to double values for use in the expression. In this example, the value of gradeCounter is promoted to type double , then floating-point division is performed and the result of the calculation is assigned to average . As long as the (double) cast operator is applied to any variable in the calculation, the calculation will yield a double result. Later in this chapter, we discuss all the simple types. You will learn more about the promotion rules in Section 7.7.
Cast operators are available for all simple types. (We'll discuss cast operators for reference types in Chapter 11.) The cast operator is formed by placing parentheses around the name of a type. This operator is a unary operator (i.e., an operator that takes only one operand). In Chapter 3, we studied the binary arithmetic operators. C# also supports unary versions of the plus ( + ) and minus () operators, so you can write expressions like +5 or -7 . Cast operators associate from right to left and have the same precedence as other unary operators, such as unary + and unary - . This precedence is one level higher than that of the multiplicative operators * , / and % . (See the operator precedence chart in Appendix A.) We indicate the cast operator with the notation ( type ) in our precedence charts , to indicate that any type name can be used to form a cast operator.
Line 74 outputs the class average using Console 's WriteLine method. In this example, we decided that we'd like to display the class average rounded to the nearest hundredth and output the average with exactly two digits to the right of the decimal point. The format specifier F in WriteLine 's format item (line 74) indicates that variable average 's value should be displayed as a real number. The number after the format specifier F represents the number of decimal places (in this case, 2 ) that should be output to the right of the decimal point in the floating-point numberalso known as the number's precision . Any floating point value output with F2 will be rounded to the hundredths positionfor example, 123.457 would be rounded to 123.46, and 27.333 would be rounded to 27.33. In this application, the three grades entered during the sample execution of class GradeBookTest (Fig. 5.10) total 263, which yields the average 87.66666.... The format item rounds the average to the hundredths position, and the average is displayed as 87.67 .
Figure 5.10. Create GradeBook object and invoke its DetermineClassAverage method.
Section 5.9. Formulating Algorithms: Sentinel-Controlled Repetition
Macroeconomic Essentials. Understanding Economics in the News 2000
suppose that in 1995 the price index (base year 1992) was 120
suppose that in 1995 the price index (base year 1992) was 120