Chapter 4 Control Structures: Part 1 109 (Web hosting ratings) used

Chapter 4 Control Structures: Part 1 109 used in a calculation. Recall that using uninitialized variables in calculations results in compilation errors. Line 20 indicates that the while structure should continue as long as the value of gradeCounter is less than or equal to 10. Lines 23 and 26 correspond to the pseudocode statement Input the next grade. The statement on line 23 displays the prompt Enter integergrade: on the screen. The statement on line 26 reads the information entered by the user, converts it to an int and stores the value in gradeValue. Next, line 29 updates the totalwith the new gradeValue by adding gradeValue to the previous value of total and assigning the result to total. The program is now ready to increment the variable gradeCounter to indicate that a grade has been processed. Line 32 adds 1 to gradeCounter, so the condition in the while structure eventually will become false and terminate the loop. Line 36 assigns the results of the average calculation to variable average. Line 39 displays a message containing the string “Classaverageis ” followed by the value of variable average. The averaging calculation produces an integer result. Actually, the sum of the grade- point values in this example is 794, which, when divided by 10, yields 79.4. Such numbers with a decimal point are called floating-point numbers; we discuss floating-point numbers in the next section. 4.9 Formulating Algorithms with Top-Down, StepwiseRefinement: Case Study 2 (Sentinel-Controlled Repetition) Let us generalize the class-average problem. Consider the following problem: Develop a class-averaging program that processes an arbitrary number of grades each time the program executes. 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 are to be 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 end of data entry. The user inputs all grades and then types the sentinel value to indicate that the last grade has been entered. Sentinel-controlled repetition often is called indefinite repetition because the number of repetitions is not known before the loop begins executing. The sentinel value cannot be confused with an acceptable input value. Grades on a quiz are normally nonnegative integers, thus -1 is an acceptable sentinel value for this problem. 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. The sentinel value, -1, should not enter into the averaging calculation. Common Programming Error 4.5 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, a problem known as an infinite loop. We approach the class-average program with top-down, stepwise refinement, a technique essential to the development of well-structured algorithms. We begin with a pseudocode representation of the top:
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Leave a Reply