Algorithm Design and Problem Solving · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 11% of your exam marks.
The most marks-dense topic. Both papers test pseudocode writing and trace tables every sitting.
A trace table is a table used to record the value of each variable at every step of an algorithm, so that the algorithm can be hand-executed and checked for correctness.
Trace tables are essential for finding logic errors, where a program runs without crashing but produces the wrong output. By stepping through the algorithm with specific test data, the programmer can spot the exact step where the variables go wrong.
OUTPUT statement is reached.Example — Trace the following algorithm with input data 4, 3, 7, 1, 8, 3, 6, 9, 12, 10 (ten numbers, one at a time). The algorithm finds the largest number entered.
Count ← 0
Highest ← 0
INPUT Number
Highest ← Number
Count ← 1
REPEAT
INPUT Number
IF Number > Highest THEN
Highest ← Number
ENDIF
Count ← Count + 1
UNTIL Count = 10
OUTPUT Highest
The trace table:
| Count | Highest | Number | Output |
|---|---|---|---|
| 0 | 0 | (none yet) | |
| 1 | 4 | 4 | |
| 2 | 4 | 3 | |
| 3 | 7 | 7 | |
| 4 | 7 | 1 | |
| 5 | 8 | 8 | |
| 6 | 8 | 3 | |
| 7 | 8 | 6 | |
| 8 | 9 | 9 | |
| 9 | 12 | 12 | |
| 10 | 12 | 10 | 12 |
After ten inputs, the algorithm outputs 12 (the largest number entered). The trace table makes it easy to see that Highest was updated each time a larger value arrived, and that the loop ran exactly ten times.
Highest does not update when it should, the trace table reveals the exact step where the bug is.