Page 74 - Introduction to Programming with Java: A Problem Solving Approach
P. 74

                40 Chapter 2 Algorithms and Design
 Often, you should spend time just thinking about a problem’s solution before writing any- thing down. And you should think first about the solution at a high level, without worrying about s. With that said, we encourage you to set the book aside now and think about the steps needed in
the Bowling Score algorithm.
Are you done thinking? If so, compare your thoughts to this high-level description:
Read in scores repeatedly and find the sum of all the scores.
Then, when 􏰁1 is entered, divide the sum by the number of scores entered.
There are two details in this high-level description that you now need to address. First, you need to think about how to find the sum of all the scores. Before asking for any input, and before any looping, assign an initial value of zero to a totalScore variable. In other words, initialize it to zero. Then, in the same loop which repeatedly asks the user for the next score, right after inputting that score, add it to the totalScore variable to accumulate the scores as they come in. This way, after all the scores are in, the totalScore variable will already contain the sum of all scores.
The sum of all scores is useful because the goal is to determine the average score, and to compute an average you need the sum. But to compute an average you also need the total number of items, and that’s not known ahead of time. How can you keep track of the number of scores entered so far? Initialize and ac- cumulate a count variable while you initialize and update the totalScore variable. Note that just one loop does all three activities (inputting, updating totalScore, and updating count). We chose 􏰁1 as a sentinel value for a Bowling Score algorithm because it’s a value that would never be a valid bowling-score entry. But any negative number would work as the sentinel value.
Figure 2.12 illustrates the algorithm solution for this problem. Note how the prompt messages say “(􏰁1 to quit).” That is necessary becauAsepwaithgoutoit, thPe uDseFr wouEldn’thknaownhocwetorquit. In general, always pro- vide enough prompting information so that the user knows what to do next and knows how to quit.
 Mull it over.
all the detail
   set totalScore to 0
set count to 0
print “Enter score (􏰁1 to quit): ” input score
while score is not equal to 􏰁1
set totalScore to totalScore + score setcounttocount+ 1
print “Enter score (􏰁1 to quit): ”
input score
set avg to totalScore / count print “Average score is ” avg
Figure 2.12 Bowling Score algorithm using a sentinel-value loop
What would you expect to happen if the user enters 􏰁1 as the very first input? That causes the loop body to be skipped, and the count variable never gets updated from its original initialized value, zero. When the set average statement attempts to calculate the average score, it divides totalScore by count. Since count is zero, it divides by zero. As you may recall from your math courses, division by zero cre- ates problems. If an algorithm divides by zero, the result is undefined. If a Java program divides by zero, the computer prints a cryptic error message and then immediately shuts down the program. Since the Bowling














































































   72   73   74   75   76