GoalBuild a trace table for a program with a loop, and use it to find the first line where a value goes wrong.
New words
trace table · a table with a column for every variable and one for the output, filled in line by line as you follow a programtrace · to follow a program by hand, one line at a time, and record what each line doesoutput column · the column in which you write exactly what the program prints
Explanation
A trace table is a test run on paper. Make one column for every variable and one more for the output. If you like, add a first column for the line number. Then work through the program line by line, and do exactly what each line says. Whenever a value changes, write the new value in a new row; never rub out or cross out the old one. In a for loop the counter changes each time round, and the loop ends when the counter is past its last value. Cover the lines below the one you are on, so that you do not guess ahead. At the end compare your final values with what the program should give. The first row where a value looks wrong shows the line with the error.
Column
What you write in it
Line (optional)
the number of the line you have just run
one for each variable
its new value, in a new row, only when it changes
Output
the text printed, exactly as the program prints it
Worked examples
Pseudocode
1total ← 02FOR step ← 1 TO 33 total ← total + step * 24NEXT step
5OUTPUT total
Output
12
Setting up the table. This program has two variables, step and total, so the table has the columns Line, step, total and Output. Line 1 puts 0 in total. Line 2 gives step the value 1. Line 3 adds 1 × 2 to total, and line 4 sends the program back to line 2 with step increased to 2.
Line
step
total
Output
1
0
2
1
3
2
4
2
3
6
4
3
3
12
4
4
5
12
The complete trace of that program. Only the changed variable is written in each row. The loop runs for step 1, 2 and 3. When step becomes 4 it is past 3, so the loop ends and line 5 prints the total. The last row is highlighted: the output is 12.
Python
1total = 02for k inrange(3):
3 mark = int(input("Mark? "))
4 total = total + mark
5print("Average:", total / 2)
Output
Average: 13.5
Finding an error with a trace. The program should print the average of three marks. The user types 6, 9 and 12. In the trace, total becomes 6, then 15, then 27, and that part is right. The first wrong value is the one on the last line, where 27 is divided by 2 instead of 3. It prints 13.5, but the average is 9.0.
Class activity
Strip trace (no computer). Copy a program of five or six lines onto a strip of paper, one line per row. Fold the strip so that only one line shows. A partner reveals it line by line while you fill in your trace table. Check the last row. Then swap: each of you changes one number or one sign in the program, and the other traces the changed version. Which row of the table first shows the difference?
Practice
1
Make a trace table with the columns Line, n, big and Output for this program. What does it print?
Python
1data = [4, 12, 9, 15]
2big = 03for n in data:
4if n > 10:
5 big = big + 16print(big)
Line 1: data is the list. Line 2: big 0. Then n takes the values 4, 12, 9 and 15. Only 12 and 15 are more than 10, so big becomes 1 and then 2. Line 6 prints 2. Output: 2.
2
The program below has no loop. Complete the trace table. The lines are: 1 price ← 8; 2 qty ← 3; 3 total ← price * qty; 4 qty ← qty + 2; 5 total ← total + price * qty; 6 OUTPUT total.
Line
price
qty
total
Output
1
?
2
?
3
?
4
?
5
?
6
?
Line 1: price 8. Line 2: qty 3. Line 3: total 24. Line 4: qty 5. Line 5: total 24 + 8 × 5 = 64. Line 6: Output 64.
3
This program should print the sum of 1 to 5. Trace it and say what it really prints. Which row shows the fault, and how do you repair it?
Python
1total = 02for n inrange(1, 5):
3 total = total + n
4print("Sum of 1 to 5:", total)
The values of n are 1, 2, 3 and 4, because range(1, 5) stops before 5. The total becomes 1, 3, 6 and 10, so it prints Sum of 1 to 5: 10. The fault shows in the last value of n, which is 4 instead of 5. Use range(1, 6), which gives 15.
4
Why must every change of value be written in a new row, and not written over the old value?
The old values show the history of the program. If an answer is wrong, you can look back through the rows and find the first row where a value went wrong. If you rub out the old values, you lose that trail.