How do you use a trace table to work through an algorithm and find its output or its purpose?
Using trace tables to determine the output of an algorithm and to follow how the values of variables change, and determining the purpose of a simple algorithm.
An OCR J277 2.1.2 answer on using trace tables to follow an algorithm step by step, record how variable values change, find the output, and determine the purpose of a simple algorithm.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
OCR wants you to use a trace table to follow an algorithm by hand, recording how each variable changes step by step, so you can find the output. You must also be able to look at a short algorithm and determine its purpose, that is say what it calculates overall. Tracing is a frequent Paper 2 skill and is also how you debug an algorithm.
What a trace table is
How to build a trace table
A neat way to lay the trace out:
| i | numbers[i] | max | output |
|---|---|---|---|
| 4 | |||
| 1 | 9 | 9 | |
| 2 | 2 | 9 | |
| 3 | 7 | 9 | 9 |
Determining the purpose of an algorithm
To work out what an algorithm is for, step back and look at the overall effect rather than each line. Ask what goes in, what comes out, and what the loop or calculation achieves. A loop that adds each value to a running total is summing; if it then divides by the count it is averaging; a loop that compares each value to a stored best is finding a maximum or minimum; a loop that swaps out-of-order pairs is sorting. Naming the overall job (the average of the numbers, the largest value, a count of how many pass a test) is what the question rewards.
Try this
Q1. State what a trace table is used for. [1 mark]
- Cue. To record how the variables in an algorithm change as it runs, so you can find its output and spot errors (a dry run).
Q2. After the loop total = 0; for i = 1 to 3; total = total + i; next i, what value does total hold? [1 mark]
- Cue. 6 (1 + 2 + 3).
Q3. An algorithm reads ten numbers, counts how many are greater than 100, and prints the count. State its purpose in one sentence. [1 mark]
- Cue. It counts (and outputs) how many of the ten numbers entered are greater than 100.
Exam-style practice questions
Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
OCR 20214 marksComplete a trace table for the algorithm below and state the value that is output.
x = 5
y = 0
while x > 0
y = y + x
x = x - 1
endwhile
print(y)Show worked answer →
Trace the loop, writing each new value of x and y on its own row. Starting x = 5, y = 0:
Pass 1: y = 0 + 5 = 5, x = 4. Pass 2: y = 5 + 4 = 9, x = 3. Pass 3: y = 9 + 3 = 12, x = 2. Pass 4: y = 12 + 2 = 14, x = 1. Pass 5: y = 14 + 1 = 15, x = 0. The condition x > 0 is now false, so the loop stops.
The value output is 15 (this is 5 + 4 + 3 + 2 + 1, the sum of 1 to 5).
Markers reward a correct row for each pass showing both variables changing, and the correct final output of 15. A common slip is updating x before adding it, or stopping one pass too early.
OCR 20222 marksLook at the algorithm below and state its purpose (what it calculates).
total = 0
for i = 1 to 5
num = int(input("Enter a number: "))
total = total + num
next i
average = total / 5
print(average)Show worked answer →
Work out what the algorithm does overall, not line by line. It reads in five numbers (the loop runs five times, each time reading a number into num), adds each to a running total, then divides the total by 5 and prints the result.
Purpose: it calculates and outputs the average (mean) of five numbers entered by the user.
Markers reward identifying the overall purpose (the average or mean of five numbers), not just describing each line. Saying "it adds numbers up" only is not enough, because it misses the division that makes it an average.
Related dot points
- Producing algorithms using pseudocode and flowcharts to solve a problem, identifying the inputs, processes and outputs, and interpreting, correcting and refining algorithms others have written.
An OCR J277 2.1.2 answer on designing algorithms with pseudocode and flowcharts: identifying inputs, processes and outputs, the OCR Exam Reference Language, the standard flowchart symbols, and interpreting, correcting and refining algorithms.
- The principles of computational thinking: abstraction, decomposition and algorithmic thinking, and how each is used to analyse a problem and design a solution.
An OCR J277 2.1.1 answer on the principles of computational thinking: abstraction (removing unnecessary detail), decomposition (breaking a problem into smaller parts) and algorithmic thinking (a clear sequence of steps), with examples of how each is applied to a problem.
- The three basic programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), and when to use each.
An OCR J277 2.2.2 answer on the three programming constructs: sequence, selection (if and switch/case) and iteration (count-controlled for loops and condition-controlled while and do until loops), with the OCR Exam Reference Language for each.
- Standard searching algorithms: linear search and binary search, how each works step by step, the requirement that binary search needs a sorted list, and the comparison of their efficiency.
An OCR J277 2.1.3 answer on the two standard searching algorithms: how linear search and binary search work step by step, why binary search needs a sorted list, and how their efficiency compares.
- The purpose of testing, the difference between iterative and terminal (final) testing, and the types of test data (normal, boundary and erroneous or invalid), with how to choose suitable test data.
An OCR J277 2.3.2 answer on testing: the purpose of testing, iterative versus terminal (final) testing, and the three types of test data (normal, boundary and erroneous or invalid), with how to choose suitable test data and build a test plan.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)