7.7 Trace tables to document dry runs of algorithms

← Topic 7.6 Test dataComputer Science contentsTopic 7.8 Identifying errors in algorithms →
Chapter 7 · Algorithm design and problem solving

7.7 Trace tables to document dry runs of algorithms

A trace table records what happens to variables and outputs as an algorithm is followed step by step. This manual step-by-step process is called a dry run and is carried out using chosen test data.

Trace tablesDry runsVariablesOutputTest data

Trace tables and dry runs

A trace table is used to record the results from each step of an algorithm. It records the value of a variable each time that value changes. A dry run is the manual process of working through the algorithm one step at a time.

Trace tables can be used with algorithms shown as either flowcharts or pseudocode. Suitable test data is supplied, and the algorithm is followed exactly in the order shown.

How to set up a trace table: include one column for every variable whose value needs to be followed and a separate column for any output produced by the algorithm.

What to record

When this happensWhat goes in the trace table
A variable changes valueWrite the new value in that variable's column.
The algorithm outputs a valueWrite that value in the OUTPUT column.
A variable does not changeNo new value needs to be entered for that variable on that step.
Check the rules for trace tables.

Worked example: tracing a flowchart

The textbook example uses the following algorithm. It starts with A ← 0, B ← 0 and C ← 100. Each value is input into X. The algorithm compares X with B and C, updates B or C when required, increments A, and repeats until ten values have been processed.

Figure 7.15 flowchart used for a trace-table dry run
Figure 7.15 — Flowchart to trace

Test data

The test data used for the worked dry run is:

9, 7, 3, 12, 6, 4, 15, 2, 8, 5

The starting row of the trace table contains the initial values A = 0, B = 0 and C = 100. As each input is processed, only changed values are entered in the relevant columns.

ABCXOUTPUT
00100
1999
277
333
41212
56
64
71515
822
98
105
15  2

Understanding the dry run

For the first input, X is 9. Since 9 is greater than B (0), B changes to 9. Since 9 is also less than C (100), C changes to 9. A is then increased to 1.

For the next input, X is 7. It is not greater than B, so B stays 9. It is less than C, so C changes to 7. This continues for all ten values.

After the ten values have been processed, the output is 15 and 2. From this output, the purpose of the algorithm can be identified: it finds the largest and smallest values from a list of ten positive numbers.

Follow the worked dry run yourself.

Tracing the same algorithm as pseudocode

The same process can be represented in pseudocode. A trace table can still be used in exactly the same way: follow each statement in order, record every changed variable value, and record each output when it occurs.

A ← 0
B ← 0
C ← 100
OUTPUT "Enter your ten values"
REPEAT
  INPUT X
  IF X > B
    THEN
      B ← X
  ENDIF
  IF X < C
    THEN
      C ← X
  ENDIF
  A ← A + 1
UNTIL A = 10
OUTPUT B, C

When dry-running this pseudocode, the prompt Enter your ten values is the first output. The quotation marks are not written in the OUTPUT column because they are only used in pseudocode to mark the text string.

Exam skill: a question may use short variable names such as A, B, C and X. The trace table helps you discover what the algorithm is doing by showing how those values change.

Topic 7.7 revision checklist

Define a trace table.
Define a dry run.
Explain why test data is needed for a dry run.
Set up a trace table with one column for each variable and an output column.
Record a new value whenever a variable changes.
Record every value produced by an OUTPUT statement.
Dry run an algorithm shown as a flowchart.
Dry run an algorithm shown as pseudocode.
Use the completed trace table to identify the purpose of an algorithm.
Recognise from the worked example that B stores the largest value and C stores the smallest value.
Ready for a mixed Topic 7.7 check?
← Topic 7.6 Test dataComputer Science contentsTopic 7.8 Identifying errors in algorithms →