AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Stop the crash

Week 13 · 1.8 Testing · 3 lessons
3

Fill in the trace

Computing 9: Topic 1.8, pp. 135–154
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.

ColumnWhat you write in it
Line (optional)the number of the line you have just run
one for each variableits new value, in a new row, only when it changes
Outputthe text printed, exactly as the program prints it
Worked examples
Pseudocode
1total ← 0
2FOR step ← 1 TO 3
3    total ← total + step * 2
4NEXT 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.
LinesteptotalOutput
10
21
32
42
36
43
312
44
512
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 = 0
2for k in range(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 = 0
3for n in data:
4    if n > 10:
5        big = big + 1
6print(big)
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.
LinepriceqtytotalOutput
1?
2?
3?
4?
5?
6?
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 = 0
2for n in range(1, 5):
3    total = total + n
4print("Sum of 1 to 5:", total)
4
Why must every change of value be written in a new row, and not written over the old value?