Tracing algorithms and trace tables

GCSE Computer Science (Edexcel) · Edexcel 1CP2 · 1.2.4 · Computational thinking › Algorithms

60 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (Edexcel), marked as you tap.

Revision notes

Tracing means working through an algorithm by hand, line by line, to find its output or the value a variable holds at a given point. A trace table keeps track of every variable as it changes. Expect 'complete the trace table' and 'state the output' questions worth 1 to 6 marks, and questions asking what an algorithm does.

Key facts

  • Trace table: records the value of each variable as an algorithm runs
  • One column per variable, plus an output column
  • Write a new value each time a variable changes
  • Dry run: tracing an algorithm by hand, without running it on a computer
  • range(a, b) gives a, a + 1, ... up to b − 1
  • print(a, b) separates the values with a space
  • Use a variable's current value on the right-hand side of an assignment
  • A while condition is checked before every pass, including the final check that ends the loop

Notes

What a trace table is

  • A trace table records the values of the variables, and any output, as you work through an algorithm by hand (a dry run).
  • There is one column per variable, often a column for the output, and sometimes one for a condition (True/False).
  • Each time a variable changes, write its new value in its column. Move down a row as you go, e.g. one row per pass through a loop.
  • Tracing is used to find the output for given data, to find the value of a variable at a certain point, to work out what an algorithm does and to find logic errors.

How to trace

  • Start with the values the variables have before the loop (the first row).
  • Work through one line at a time. Use the current values on the right-hand side of each assignment.
  • Check the condition every time you return to the top of a while loop. For a for loop, list the values the counter takes, e.g. range(1, 4) gives 1, 2, 3.
  • Write outputs exactly as the program would show them. print(a, b) puts a space between the two values.

Worked example

diagram
  • Trace this program.
    total = 0
    n = 1
    while total < 10:
        total = total + n
        n = n + 2
    print(total, n)
    totalnoutput
    01
    13
    45
    97
    169
    16 9
  • The condition is checked before each pass: 0, 1, 4 and 9 are less than 10, so the loop runs four times. Then 16 < 10 is False, so the loop stops and the output is 16 9.
  • Starttotal = 0n = 1total < 10?Yestotal = total + nn = n + 2NoOutput total, nStop
    The condition is checked before every pass. The loop body runs four times, then 16 < 10 is False
  • Looking for a pattern tells you the purpose: this algorithm adds odd numbers (1 + 3 + 5 + 7) until the total reaches 10 or more. Grade 7+ only

How to answer each type of question

Complete a trace table

3 to 6 marksGrade 5
  1. Fill in the first row with the starting values.
  2. Go through the loop one pass at a time, writing each variable's new value.
  3. At each if, check the condition with the current values before changing anything.
  4. Finish with the output, written exactly as it would be displayed.

Example. Here is a program.

numbers = [4, 9, 2, 7]
biggest = numbers[0]
count = 0
for i in range(1, len(numbers)):
    if numbers[i] > biggest:
        biggest = numbers[i]
        count = count + 1
print(biggest, count)
Complete the trace table. The first row shows the values before the loop starts. You may not need all the rows.
ibiggestcountoutput
40

Show the model answerHide the model answer
ibiggestcountoutput
40
191
291
391
9 1
i column: 1, 2, 3 (1); biggest column: 4, 9, 9, 9 (1); count column: 0, 1, 1, 1 (1); output: 9 1
(1).
Values written only when they change are also accepted.

State the output or the value of a variable

1 to 2 marksGrade 4
  1. Trace the algorithm, even if only in rough.
  2. Stop at the point the question asks about.
  3. Give the value exactly, with a decimal point if the result is a real number.

Example. Here is a program.

a = 7
b = 2
a = a + b
b = a * b
a = a - b
print(a, b)
State the output of this program.

Show the model answerHide the model answer
a becomes 9, then b becomes 9 × 2 = 18 (1), then a becomes 9 − 18 = −9
(1).
Output: -9 18

State the purpose of an algorithm

1 to 3 marksGrade 7
  1. Trace the algorithm with the data given (or with simple data of your own).
  2. Look at how the output is related to the input.
  3. Describe what the algorithm does in one sentence, not line by line.

Example. Here is a program.

n = int(input("Enter a number: "))
result = 0
while n > 0:
    result = result + n % 10
    n = n // 10
print(result)
(a) State the output when the user enters 473.
(b) State the purpose of the algorithm.

Show the model answerHide the model answer
(a) 14 (1): result goes 3, 10, 14 while n goes 47, 4, 0
(b) It adds up the digits of the number that is entered
(1).

Don’t lose marks

  • Using a variable's new value too early, before the line that changes it.
  • Doing one pass too many or too few, e.g. running range(1, 4) with i = 4.
  • Forgetting the final check of a while loop's condition, or carrying on after it is False.
  • Writing a whole number where Python would show a real number: 8 / 2 is shown as 4.0.
  • Leaving the output column until the end and then forgetting it.

More tips

Memory tricks

  • Put a ruler or your finger on the current line and move it down as you trace.
  • Tick each pass of a loop in the margin so that you do not lose count.
  • Count the passes first: for i in range(1, 4) runs 3 times; a while loop runs until its condition is False.
  • Sense check your final answer against what the algorithm seems to do.

Exam technique

  • The table often has one row per pass. If it says 'you may not need all the rows', leave the extra rows blank.
  • Each column often carries its own mark, so a slip in one column need not cost you the others. Keep going.
  • Write output exactly as the screen would show it: print("Hi") shows Hi, without quotation marks.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Follow a short sequence to find the outputWork through a few assignments and a print statement in order.
  2. Grade 4
    Trace a simple loop in a trace tableRecord each variable's new value on each pass through the loop.
  3. Grade 5
    Trace selection inside a loopCheck the if condition on every pass and only update the variables it changes.
  4. Grade 6
    Trace algorithms that use arrays or stringsUse the index on each pass to find the correct item or character.
  5. Grade 7
    State the purpose of an algorithmSpot the pattern in the trace, e.g. 'it adds up the digits of a number'.
  6. Grade 8
    Trace nested loops and 2D arrays accuratelyKeep track of both loop counters as the inner loop runs in full on each pass of the outer loop.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Give one reason why a programmer might trace an algorithm.
To find logic errors or to check that the algorithm gives the expected output.

Sample questions

Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.

Question 1Easy1 mark
What is the purpose of a trace table? Tick (✓) one box.
[1]
  • To draw the flowchart of an algorithm
  • To record the values of variables as an algorithm is followed step by step
  • To translate a program into machine code
  • To store the data a program needs when it is not running
Show the answer and mark scheme
Answer: To record the values of variables as an algorithm is followed step by step
Question 2Medium4 marks
Look at this program.
01 data = [12, 7, 19, 19, 4]
02 best = data[0]
03 pos = 0
04 for i in range(1, len(data)):
05     if data[i] > best:
06         best = data[i]
07         pos = i
08 print(best, pos)
(a) State the output of the program.
[2]
(b) Describe what the program does with the numbers in data.
[1]
(c) Line 5 is changed to if data[i] >= best:. State the new output.
[1]
Show the answer and mark scheme
(a) Answer: 19 2
  • 19
  • 2
(b) Answer: It finds the largest value in the list and the position (index) where it first appears.
  • finds / outputs the largest number in the list (and its position / index)
(c) Answer: 19 3
  • 19 3
Question 3Hard6 marks
Look at this program.
01 for row in range(1, 5):
02     line = ""
03     for col in range(row, 5):
04         line = line + str(col)
05     print(line)
(a) State the output of the program.
[3]
(b) State the total number of times line 04 is executed.
[1]
(c) Line 03 is changed to for col in range(1, row + 1):. State the new output.
[2]
Show the answer and mark scheme
(a) Answer:
1234
234
34
4
  • 1234 as the first line
  • 234 then 34
  • 4 as the last line, with no other lines
(b) Answer: 10 (4 + 3 + 2 + 1)
  • 10
(c) Answer:
1
12
123
1234
  • 1 then 12
  • 123 then 1234

More in Algorithms

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.