Trace table questions

Trace table 1 of 12

A running total

total = 0for i = 1 to 5    total = total + i * 2next iprint(total)

One row for each pass of the loop, then one for the print. OCR for loops include the end value, so i reaches 5. Write every column in every row. Row 1 is done for you.

itotalOutput
12

Questions people ask

What is a trace table?

A table with a column for each variable (and one for output) that records the values as a program runs. It lets you check what an algorithm does line by line without a computer.

When do I start a new row?

Start a new row when a value changes and the row you are on already has a value in that column, or when a loop starts a new pass. Keep one clear rule and apply it all the way down.

Does an OCR for loop include the last number?

Yes. for i = 1 to 5 runs for 1, 2, 3, 4 and 5: five passes. That is the most common trace table slip.

What do DIV and MOD do?

DIV is whole-number division and MOD is the remainder. 17 DIV 5 = 3 and 17 MOD 5 = 2.

How do I trace recursion?

Record each call with its own parameter values. A call waits until the call it makes returns, so the base case returns first and the first call returns last.

How we work this out

What it does

Twelve OCR-style programs. Fill in the trace table and it checks every row.

Twelve original programs in the pseudocode style of the OCR A-level Computer Science specification (H446, appendix 5d): for, while and do until loops, nested loops, selection, arrays, strings, functions, recursion, by-value and by-reference parameters, a bubble sort pass and a binary search.

Method

  1. Each program was run by our own interpreter for OCR-style pseudocode, and its trace table was cut from that run: one row each time the program reaches the line the table records, with every traced variable's value then.
  2. The conventions follow OCR's guide: a for loop includes its end value, arrays and strings count from 0, DIV and MOD are whole-number division and remainder, and parameters are passed by value unless marked byRef.
  3. Your table is checked row by row. A cell counts as right when it matches the run, ignoring capital letters, quotes and spaces. The first row that differs is named, with the columns that differ and why that row comes out as it does.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms