Algorithm Tracing Technique
An algorithm trace is a method for hand simulating the
execution of your code in order to manually verify that it works
correctly before you compile it. It is also known as a
"desk check."
Step 1: Number each executable
statement in your pseudocode or source code.
1 public Hand getPair(List cards)
2 {
3 // SET result to a new Hand
4 // SET previous to first card in cards
5 // SET current to second card in cards
6 // WHILE current not last card AND result is empty LOOP
7 // IF current card rank EQUALS previous card rank THEN
8 // ADD current card to result
9 // ADD previous card to result
10 // END IF
11 // increment current
12 // END LOOP
13 // RETURN result
14 }
Step 2: Draw a table with each variable in the program shown
at the top of a column. Draw a column for Statement number on the left
and a column for Output (if appropriate) on the right. It's also
helpful to draw a
separate
column for each boolean expression.
stmt previous current result condition1 condition2
Step 3: Starting with statement 1, simulate the action the
computer
will take when it executes each statement. Draw one statement per row.
In the appropriate column, write the value that is assigned to the
variable
(or boolean expression).
cards 2C 3D 5S 5D JH
stmt previous current result condition1 condition2
3 new Hand
4 2C
5 3D
6 T
7 F
11 5S
6 T
7 F
11 5C
6 T
7 F
11 JH
6 F
13 Return empty hand
Step 4: When the trace is
complete, check that the output produced matches the expected output.
In this case, the expected output was "5C 5S" so the algorithm
is defective.