🔒
Unlock Everything
£7.99/month
or £59/year
Subscribe now →
🔢 Component 2 · 2.1 Algorithms
2.1.3e Trace Tables
OCR J277 · GCSE Computer Science · ~12 min read
Notes
Video
Slides
Worksheet
Quiz

What is a Trace Table?

A trace table (also called a dry run) is a technique used to manually track the values of variables as an algorithm executes, line by line. It lets you verify that code produces the correct output without running it on a computer.

In OCR J277 exams, you will be asked to complete trace tables for given algorithms. Each column represents a variable or output; each row represents one iteration or key step.

Purpose of Trace Tables

  • Test an algorithm's correctness before coding it
  • Identify logic errors and bugs
  • Understand what a piece of code does
  • Communicate algorithm behaviour clearly

How to Complete a Trace Table — Step by Step

1. Read through the algorithm carefully before starting

2. Create a column for every variable referenced in the algorithm, plus an output column if needed

3. Initialise: write any starting values in the first row

4. Execute each line in order, updating only the variable that changes on that line

5. Leave other cells blank if their value did not change on that step

6. Continue until the algorithm ends or reaches the required condition

Exam tip: You do NOT need to fill in every cell every row — only the value that changes. Examiners award marks for each correct cell. Show every change, even temporary ones (e.g. a temp variable in a swap).

Worked Example 1 — Simple Loop with Counter

total = 0
FOR i = 1 TO 4
    total = total + i
NEXT i
OUTPUT total
itotalOUTPUT
Initialise0
i = 111
i = 223
i = 336
i = 4410
Output10

Worked Example 2 — Bubble Sort Trace (first pass)

List: [5, 3, 8, 1] — trace the first full pass

FOR j = 0 TO 2
  IF list[j] > list[j+1] THEN
    temp = list[j]
    list[j] = list[j+1]
    list[j+1] = temp
  END IF
NEXT j
jlist[0]list[1]list[2]list[3]tempSwap?
Start5381
j=0: 5>3?535Yes
swap35
j=1: 5>8?58No
j=2: 8>1?818Yes
swap18
End pass 13518

Worked Example 3 — Binary Search Trace

Sorted list: [2, 5, 9, 14, 21, 34, 42] — search for target = 21

lowhighmidlist[mid]Action
0631414 < 21 → low = mid+1 = 4
4653434 > 21 → high = mid-1 = 4
44421Found! Return index 4
Exam tip: Always show the calculation for mid: mid = (low + high) DIV 2. E.g. (0+6) DIV 2 = 3. Showing your working can earn partial marks even if the final answer has a small error.
⚠️ Common Mistakes in Trace Tables
  • Copying the same value into a cell when it didn't change — leave blank if unchanged
  • Forgetting to update the loop counter (i, j) each iteration
  • Not showing the temp variable when tracing a swap
  • Using integer division (DIV) — make sure to floor, not round. E.g. (3+4) DIV 2 = 3, not 3.5
  • Stopping the trace too early — run all iterations unless told otherwise
  • Reversing the condition in binary search (confusing > and <)

Detecting Errors with Trace Tables

Trace tables are used to find logic errors — where the program runs but gives the wrong answer. Example: a loop starting at index 1 instead of 0 would skip the first element. Tracing reveals this because you can see when the expected value doesn't appear.

They can also detect infinite loops — if tracing shows a variable never reaches the loop exit condition, the loop will never terminate.

✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.1.3e Trace Tables

8 questions · 20 marks

Q1What is a trace table and why is it used?[2]
✅ Mark scheme
A trace table tracks the value of each variable as an algorithm executes line by line [1]. It is used to test/verify a program's logic, identify errors, and understand what code does without running it on a computer [1].
Q2Trace the following algorithm and complete the table: x=10; y=3; z=x MOD y; OUTPUT z[3]
✅ Mark scheme
x=10 [1]; y=3 [1]; z = 10 MOD 3 = 1; OUTPUT = 1 [1]. (MOD gives the remainder: 10 ÷ 3 = 3 remainder 1)
Q3Trace this loop: count=0; FOR i=1 TO 5; count=count+i; NEXT i; OUTPUT count. Give the value of count after each iteration.[3]
✅ Mark scheme
i=1: count=1 [½]; i=2: count=3 [½]; i=3: count=6 [½]; i=4: count=10 [½]; i=5: count=15 [½]; OUTPUT=15 [½]. Accept all correct values [3 total].
Q4A swap routine uses: temp=a; a=b; b=temp. If a=7, b=2, trace these three lines showing the value of a, b and temp after each.[3]
✅ Mark scheme
After temp=a: a=7, b=2, temp=7 [1]. After a=b: a=2, b=2, temp=7 [1]. After b=temp: a=2, b=7, temp=7 [1].
Q5Trace one pass of bubble sort on [4, 1, 7, 2]. Show the list after each comparison and whether a swap occurred.[3]
✅ Mark scheme
j=0: 4>1? Yes → swap → [1,4,7,2] [1]. j=1: 4>7? No → [1,4,7,2] [1]. j=2: 7>2? Yes → swap → [1,4,2,7] [1]. Final state: [1,4,2,7].
Q6Perform a binary search trace on [3,7,12,18,25,30,45] for target=12. Show low, high, mid, and list[mid] for each step.[3]
✅ Mark scheme
Step 1: low=0,high=6,mid=3,list[3]=18 — 18>12 so high=2 [1]. Step 2: low=0,high=2,mid=1,list[1]=7 — 7<12 so low=2 [1]. Step 3: low=2,high=2,mid=2,list[2]=12 — Found at index 2 [1].
Q7What type of error can a trace table help detect? Give an example.[2]
✅ Mark scheme
Logic errors [1] — e.g. a loop starting at index 1 instead of 0 (skipping first element), or using > instead of >= in a condition, giving a wrong result without a crash [1].
Q8A student traces a loop and notices the variable controlling the loop never reaches the exit condition. What problem does this indicate?[1]
✅ Mark scheme
An infinite loop [1] — the loop will never terminate because the exit condition can never be met.
?
out of 20 — self-mark above
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
Complete!
TermDefinition
🎯

Mini Test — 2.1.3e Trace Tables

10 questions · 10 marks · 10 minutes

← 2.1.3d Algorithm Comparison 2.1 Algorithms 2.2.1a Variables & Operators →