Page 77 - Introduction to Programming with Java: A Problem Solving Approach
P. 77
between those contacts. After the programmer pulled the moth out—“debugged” the computer program—the computer gave the right answer. When you’re tracing an algorithm or program to find software bugs, you may sometimes feel like one of these old-timers crawling around inside the CPU, looking for moths.
Short-Form Tracing
We present two tracing forms—a short form, described in this subsection, and a long form, described in the next subsection. The short-form tracing procedure is commonly used in industry and in classrooms. It works well in a dynamic environment, where you can move back and forth between pseudocode (or Java code, later) and a trace listing, and fill information in as you go. You may see your teacher go through this dynamic operation on a whiteboard. For example, here’s an algorithm that prints the Happy-birthday song:
input name
set count to 0
while count is less than 2
Apago PDF Enhancer
print “Happy birthday to you.”
set count to count 1
print “Happy birthday, dear ” name “.” print “Happy birthday to you.”
Here’s what the short-form trace looks like after the trace is complete:
input name count Arjun Arjun 0
1
2
output
What is your name?
Happy birthday to you. Happy birthday to you. Happy birthday, dear Arjun. Happy birthday to you.
2.11 Tracing 43
print “What is your name? “
The above trace listing has four columns—input, name, count, and output. The input column shows hypo- thetical input for the algorithm. The output column shows what the algorithm produces when the algorithm runs with the given input. The name and count columns show the values stored in the name and count variables. In this example, we started with the input value “Arjun.” Then we stepped through the code, one line at a time. In stepping through the code, we added values under the name, count, and output columns, and we crossed out old count values as they were overwritten by new count values. Figure 2.14 describes the general procedure.