Edexcel International Advanced Level Computer Science, point 5.1.2
Definition and function
A trace table is a table that records the value of each variable (and any output) as an algorithm is carried out, one step at a time. It is "running the program by hand".
Function: it lets you predict what an algorithm does, check that it is correct, and find logic errors (5.1.1), without running it on a computer.
How to complete one
- Make a column for every variable (and one for output, and sometimes one for conditions).
- Follow the code line by line, in the order the computer would (including loops and branches).
- Add a new row each time a variable changes (or each time round a loop).
- Copy unchanged values down, or leave them blank, consistently.
- Write each output in the output column when it happens.
- Stop when the algorithm ends.
Take care with:
- Conditions (is
x > 3true now?). - Loop tests (the test is made before a
whileloop's body, and after ado ... until's). - The order of assignments:
a = bthenb = aisn't a swap. %(remainder) and//(whole-number division):17 % 5 = 2and17 // 5 = 3.
Example 1: a while loop
x = 5
total = 0
while x > 0:
total = total + x
x = x - 2
print(total)
| Step | x |
total |
x > 0? |
Output |
|---|---|---|---|---|
| Start | 5 | 0 | ||
| Test | 5 | 0 | true | |
| Body | 3 | 5 | ||
| Test | 3 | 5 | true | |
| Body | 1 | 8 | ||
| Test | 1 | 8 | true | |
| Body | -1 | 9 | ||
| Test | -1 | 9 | false | |
| End | 9 |
The loop stops because x is , which fails x > 0, and 9 is output.
Example 2: a for loop with selection
count = 0
for i in range(1, 6):
if i % 2 == 0:
count = count + i
print(count)
i |
i % 2 == 0? |
count |
|---|---|---|
| (start) | 0 | |
| 1 | false | 0 |
| 2 | true | 2 |
| 3 | false | 2 |
| 4 | true | 6 |
| 5 | false | 6 |
| Output: 6 |
Example 3: % and // (digit sum)
n = 472
s = 0
while n > 0:
s = s + n % 10 # last digit
n = n // 10 # remove last digit
print(s)
n |
n % 10 |
s |
|---|---|---|
| 472 | 2 | 2 |
| 47 | 7 | 9 |
| 4 | 4 | 13 |
| 0 | 13 |
Example 4: finding an error with a trace
This algorithm should count how many numbers in a list are above the average.
nums = [2, 4, 9]
total = 0
for n in nums:
total = total + n
avg = total / len(nums)
count = 0
for n in nums:
if n > avg:
count = count + 1
print(count)
n |
total |
avg |
n > avg? |
count |
|---|---|---|---|---|
| 2 | 2 | |||
| 4 | 6 | |||
| 9 | 15 | |||
| 5.0 | 0 | |||
| 2 | 5.0 | false | 0 | |
| 4 | 5.0 | false | 0 | |
| 9 | 5.0 | true | 1 |
Output: 1 (only 9 is above 5). The trace confirms it works for this data. A good test would also include an empty list (division by zero) and equal numbers.
Using trace tables well
- Choose test data that checks normal, boundary and erroneous cases.
- Use the trace to find the first row where the values go wrong, which points to the faulty line.
- Compare the final row with the expected result.
- Trace tables are used in flowcharts, pseudocode and code.
Worked example
Task: complete a trace table for this algorithm with the input a = 12, b = 18, and say what it calculates.
while b != 0:
t = b
b = a % b
a = t
print(a)
a |
b |
t |
Notes |
|---|---|---|---|
| 12 | 18 | start, b != 0 true |
|
| 18 | 12 | 18 | b = 12 % 18 = 12, a = 18 |
| 12 | 6 | 12 | b = 18 % 12 = 6, a = 12 |
| 6 | 0 | 6 | b = 12 % 6 = 0, a = 6 |
b is now 0, so the loop stops, and the output is 6. It calculates the highest common factor of 12 and 18 (Euclid's algorithm).
Exam tip: include a column for every variable and for output, and add a row for every change. Don't skip loop tests, since the last test (which fails) is where many marks are lost. Read the question's code carefully: watch for % and //, and for where the loop condition is tested.
Check yourself
What is a trace table used for?
To follow the values of variables and the output as an algorithm runs, so you can predict its result and find logic errors.
What is 23 % 5, and what is 23 // 5?
23 % 5 = 3 (the remainder) and 23 // 5 = 4 (whole-number division).
What is the output of: x = 3, then while x < 20: x = x * 2, then print x?
24 (3, 6, 12, 24; the loop stops when x is 24).
Why choose boundary values when tracing?
Mistakes such as using < instead of <= appear at the edges of ranges, so boundary values reveal them.