Menu

Trace Tables

Trace Table Columns

A trace table is only as useful as the columns you give it, so decide on the columns before you start filling in any values. Get them right and the table stays readable and records everything that matters.

What deserves a column

  • Variable columns — one for each variable that matters: a variable whose value changes, is tested in a condition, or is printed.
  • An output column — needed whenever the algorithm prints something, because the printed values are usually what you are asked for.
  • A condition column — worth adding when the same loop or selection condition is tested again and again, so you can record True or False each time it is checked.
  • A line number column — worth adding when the algorithm is long, or when the same variable is changed in several different places and you need to show which line did it.

Include only what the algorithm needs. A value that never changes, such as a fixed price used in a calculation, does not need its own column, and neither does a variable that is never changed, tested or printed. Extra columns add no information and make the table harder to read.

Order and labels

Label each column clearly with the variable name, or a short description of the test such as mark > 75, rather than a vague heading. Set the columns out in a clear order — often the order the variables appear in the algorithm — so a row can be read left to right and the values followed easily. Putting the output column at the end, after everything that feeds into it, is a sensible choice.

Choosing columns for a program

gold = 0
marks_sum = 0
judge = 1
while judge <= 3:
    mark = int(input("Mark: "))
    marks_sum = marks_sum + mark
    if mark > 75:
        gold = gold + 1
    judge = judge + 1
print("Total marks", marks_sum)
print("Gold awards", gold)

Output when the user types 82, 60, 91:

Mark: 82
Mark: 60
Mark: 91
Total marks 233
Gold awards 2

Three variables need tracking: marks_sum keeps a running total, gold keeps a count and judge counts the rounds, so each needs a column, and the two print lines mean you also need an output column. The mark typed in is used in the total and in the test mark > 75, so it earns a column too. The loop test judge <= 3 is checked before every pass of the loop, and mark > 75 is checked inside each pass, so both conditions are worth a column:

marks_sumgoldjudgejudge <= 3markmark > 75Output
running total of the marks enteredcount of marks above 75counts the rounds, 1 to 3True or Falsevalue typed inTrue or Falsetext printed to the screen

No line number column is needed here, because each variable is changed in only one place, so there is nothing to confuse. A long algorithm that added to marks_sum in three different places would be a different matter.

Practise this