Trace Tables
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.
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.
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.
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_sum | gold | judge | judge <= 3 | mark | mark > 75 | Output |
|---|---|---|---|---|---|---|
| running total of the marks entered | count of marks above 75 | counts the rounds, 1 to 3 | True or False | value typed in | True or False | text 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.
OCR GCSE Computer Science · Trace Tables