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

                Score algorithm allows for the possibility of division by zero, it is not very robust. To be robust, it should behave in a way that a typical user would consider to be both sensible and courteous, even when the input is unreasonable. To make it more robust, replace the last two statements in Figure 2.12’s algorithm with an if statement like this:
if count is not equal to 0
set avg to totalScore / count print “Average score is ” avg
else
print “No entries were made.”
Using this if statement enables the program to tell the user why a normal output was not produced, and it avoids the problems inherent with division by zero.
2.10 Nested Looping
In the preceding two sections, we presented algorithms where each algorithm contained one loop. As you pro- ceed through the book and as you proceed through your programming career, you’ll find that most programs contain more than one loop. If a program has loops that are independent (i.e., the first loop ends before the second loop begins), then the program’s flow should be reasonably straightforward. On the other hand, if a program has a loop inside a loop, then the program’s flow can be harder to understand. In this section, we’ll try to make you comfortable with a nested loop, which is the formal term for an inner loop that’s inside an outer loop. Apago PDF Enhancer
Suppose you’re asked to write an algorithm that plays multiple games of “Find the largest number.” In each game, the user enters a series of nonnegative numbers. When the user enters a negative number, the algorithm prints the largest number in the series and asks the user if he/she wants to play another game.
2.10 NestedLooping 41
     Before writing anything down, you should think about a very important question:
What types of loops should be used? You’ll need an outer loop that continues as long
as the user says that he/she wants to play another game. What type of loop should that
be—counter loop, user-query loop, or sentinel value loop? You’ll need an inner loop that
plays one game by reading in numbers until a negative number is input. What type of
loop should that be—counter loop, user-query loop, or sentinel value loop? Have you attempted to answer the questions? If so, read on. If not, stop and think.
The outer loop should be a user-query loop. The inner loop should be a sentinel value loop, where the sentinel value is any negative number. Now look at the algorithm for this problem in Figure 2.13. Note that the algorithm does indeed use a user-query outer loop—at the bottom of the loop, the user is prompted to continue, and at the top of the loop, the response is checked. Note that the algorithm does indeed use a sen- tinel value inner loop—the loop terminates when the user enters a negative number.
The inner loop’s logic is nontrivial and deserves special attention. Before examining the code itself, think about the goal and the solution at a high level. The goal is to read in a series of numbers where the last number is negative and then print the largest number. Suppose the input sequence is 7, 6, 8, 3, 4, 􏰁99. After each new number is entered, the algorithm should ask the question: Is the new number
bigger than the previous biggest number? If the new number is bigger, the new number
is the new “champion,” that is, the new biggest number. Note that the preceding question
started with the word “if.” That’s a good indication that you can implement that logic with an if statement. Find the if statement in Figure 2.13’s inner loop and verify that it implements the aforementioned logic.
 Think about what type of loops should be used.
                  How would a human do it?
  











































































   73   74   75   76   77