166 Control Structures: Part 1 Chapter 4 In (Web hosting service)

166 Control Structures: Part 1 Chapter 4 In the first class-average example, the number of grades (10) was known in advance. In this example, no indication is given of how many grades the user will input. 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 the 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. Sentinel-controlled repetition is often called indefinite repetition, because the number of repetitions is not known before the loop begins executing. Clearly, the sentinel value must be chosen so that it cannot be confused with an acceptable input value. Because grades on a quiz are normally nonnegative integers, 1 is an acceptable sentinel value for this problem. Thus, an execution of the class-average program might process a stream of inputs such as 95, 96, 75, 74, 89 and 1. In this case, the program would compute and print the class average for the grades 95, 96, 75, 74 and 89. ( 1 is the sentinel value, so it should not enter into the averaging calculation.) Common Programming Error 4.7 Choosing a sentinel value that is also a legitimate data value results in a logic error and may prevent a sentinel-controlled loop from terminating properly. We approach the class-average program with a technique called top-down, stepwise refinement, a method that is essential to the development of well-structured algorithms. We begin with a pseudocode representation of the top: Determine the class average for the quiz The top is a single statement that conveys the overall function of the program. As such, the top is, in effect, a complete representation of a program. Unfortunately, the top rarely conveys a sufficient amount of detail from which to write the Java algorithm. So we now begin the refinement process. We divide the top into a series of smaller tasks and list these tasks in the order in which they need to be performed. This procedure results in the following first refinement: Initialize variables Input, sum up and count the quiz grades Calculate and print the class average This pseudocode uses only the sequence structure the steps listed occur in order, one after the other. Software Engineering Observation 4.4 Each refinement, as well as the top itself, is a complete specification of the algorithm; only the level of detail varies. To proceed to the next level of refinement (i.e., the second refinement), we commit to specific variables. We need a running total of the grades, a count of how many grades have been processed, a variable to receive the value of each grade as it is input and a variable to store the calculated average. The pseudocode statement Initialize variables Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply