Trace table
Trace tables are used to allow programmers to trace the value of variables as each line of code is executed. The values of the variables are displayed in a table and assist the programmer in identifying any potential errors.
Example
This algorithm is designed to count the number of potential astronauts who qualify for training based upon the height requirements of a space agency. Astronauts need to be between 1.57 and 1.91 metres tall inclusive of both heights.
Line
1. SET total TO 0
2. SET all_heights TO [1.87, 1.48, 1.57, 1.91, 2.01]
3. FOR EACH height FROM all_heights DO
4. IF height 藘 1.57 AND height 鈮 1.91 THEN
5. 聽 聽 SET total TO total + 1
6. 聽 聽 END IF
7. END FOR EACH
There is a deliberate error on line 4 of this reference language example. The symbol >
has been used instead of 鈮
before the number 1.57.
This will allow us to look at a trace table to identify the point at which the program failed to return an expected value. Three values from the all_heights array should be accepted as they fall within the desired range. The values that should trigger an increment to the total are 1.87, 1.57 and 1.91.
You will notice that after the first iteration of the loop the program will return to line 3. Lines 3 to 7 are repeated for each height in the 1-D array all_heights. The trace table shows a complete run through of the values stored by each variable during the execution of the algorithm.
Line | total | all_heights | height |
1 | 0 | ||
2 | (1.87, 1.48, 1.57, 1.91, 2.01) | ||
3 | 1.87 | ||
5 | 1 | ||
3 | 1.48 | ||
5 | 1 | ||
3 | 1.57 | ||
5 | 1 | ||
3 | 1.91 | ||
5 | 2 | ||
3 | 2.01 | ||
5 | 2 |
Line | 1 |
---|---|
total | 0 |
all_heights | |
height |
Line | 2 |
---|---|
total | |
all_heights | (1.87, 1.48, 1.57, 1.91, 2.01) |
height |
Line | 3 |
---|---|
total | |
all_heights | |
height | 1.87 |
Line | 5 |
---|---|
total | 1 |
all_heights | |
height |
Line | 3 |
---|---|
total | |
all_heights | |
height | 1.48 |
Line | 5 |
---|---|
total | 1 |
all_heights | |
height |
Line | 3 |
---|---|
total | |
all_heights | |
height | 1.57 |
Line | 5 |
---|---|
total | 1 |
all_heights | |
height |
Line | 3 |
---|---|
total | |
all_heights | |
height | 1.91 |
Line | 5 |
---|---|
total | 2 |
all_heights | |
height |
Line | 3 |
---|---|
total | |
all_heights | |
height | 2.01 |
Line | 5 |
---|---|
total | 2 |
all_heights | |
height |
The highlighted row on the trace table shows where the program failed to return the value that the programmer would have expected. The height of 1.57 is within the desired range.
However, the programmer鈥檚 logic error on line 4 of the algorithm has caused the program to exclude 1.57 as a value within range. As the program is designed to only add 1 to the total if the value of height is greater than 1.57, the total remains at 1 instead of 2.
Without using a trace table the algorithm would return the value 2 for total but the programmer would not be able to clearly identify the problem in the code. This exemplifies the purpose and usefulness of a trace table to track the value of variables.