Further algorithms - EdexcelTrace tables

Programs must run correctly or they are of little value. The careful planning and testing of a program is essential, as is writing code which assists future updating.

Part ofComputer SciencePrinciples of computer science

Trace tables

One way to test short is to carry out a using paper. A dry run involves creating a that includes all the a program contains. Whenever the value of a variable changes, the change is indicated in the trace table.

Consider this simple program.

1 SET total TO 0 2 FOR count FROM 1 TO 3 DO 3 RECEIVE number FROM (INTEGER) KEYBOARD 4 SET total TO total + number 5 END FOR

Each instruction has been given a line number - 1 to 5. The program has three variables - total, count and number - which are put into a trace table.

Next, the program is tested using . If the numbers 5, 7 and 9 are input, the resulting total should be 21.

The instruction number is added to the trace table. If a variable changes with that instruction, the new variable value is written in the appropriate box:

InstructionTotalCountNumber
10
21
35
45
5
22
37
412
5
23
39
421
5
Instruction1
Total0
Count
Number
Instruction2
Total
Count1
Number
Instruction3
Total
Count
Number5
Instruction4
Total5
Count
Number
Instruction5
Total
Count
Number
Instruction2
Total
Count2
Number
Instruction3
Total
Count
Number7
Instruction4
Total12
Count
Number
Instruction5
Total
Count
Number
Instruction2
Total
Count3
Number
Instruction3
Total
Count
Number9
Instruction4
Total21
Count
Number
Instruction5
Total
Count
Number

At each step, the programmer is able to see if, and how, a variable is affected.

Trace tables are extremely useful because they enable programmers to compare what the value of each variable should be against what a program actually produces. Where the two differ is the point in the program where a has occurred.