Page 80 - Introduction to Programming with Java: A Problem Solving Approach
P. 80
46 Chapter 2 Algorithms and Design
that begin with N through Z are assigned to Herr House. Using the trace setup provided in Figure 2.16, try to either complete the trace or get to a point in the trace where you’ve identified a problem.
Have you finished working on the trace? If so, compare your answer to this:
line#
lastName
output
1
Enter last name (q to quit):
2
Ponce
7
Ponce is assigned to Herr House.
7
Ponce is assigned to Herr House.
7
Ponce is assigned to Herr House.
The trace points out a problem—the algorithm repeatedly prints Ponce’s dorm assignment, but no one else’s. There appears to be an infinite loop. Can you identify the bug? The trace shows that lastName gets the first input value, Ponce, but it never gets any other input values. Referring back to Figure 2.16, you can see that the algorithm prompts for the last name above the loop, but not inside the loop. Therefore, the first input value is read in, but no others. The solution is to add another last name prompt inside the while loop, at its bottom. Here is the corrected algorithm:
print “Enter last name (q to quit): ” input lastName
while lastName is not equal to q
if lastName’s first character is between A and M
print lastName “is aAssipgneadgtooChestPnuDt HFall.”Enhancer
else
print lastName “is assigned to Herr House.”
print “Enter last name (q to quit): ” input lastName
We encourage you to trace the corrected algorithm on your own, and you’ll find that all four freshmen are assigned to appropriate dorms. Yeah!
Software Development Tools
Most software development tools temporarily label each line of code with a line number to help identify the locations of programming errors. Those line numbers are not actually part of the code, but when they are available, you can use them as identifiers in the line# column of a long-form trace. Many software de- velopment tools also include a debugger that enables you to step through a program one line at a time as it executes. The debugger enables you to look at variable values as you go. Our tracing procedure emulates a debugger’s step-by-step type of evaluation. Experience with the tracing used in this book will make it easier for you to understand what an automated debugger is telling you.
2.12 Other Pseudocode Formats and Applications
Pseudocode comes in many different varieties. In this section, we describe several pseudocode variations and the inherent differences between them.