Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Computing Foundations

Module 1 · Lesson 3 · Core

Tracing an algorithm by hand

Use a trace table to follow every step and see how the stored value changes.

  • 50 minutes
  • Serves O1 and O6
  • Assumes M1.L2

Phase 1: Prepare

Step 1 Warm-up recall

From M1.L2: list the four properties of an algorithm.

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • fill in a trace table for a short algorithm (Foundation);
  • use a trace to find where an algorithm goes wrong (Core);
  • predict the output before running a method (Core).

Phase 2: Teach

Step 3 A familiar example

A teacher marks a student’s long division by reading each line and checking what changed. A trace does the same for a method: you act as the machine, one step at a time.

Step 4 Clear explanation

To trace an algorithm, you carry out its steps yourself and record every value that changes. A trace table has one row per step and one column per stored value. It is the main way programmers find mistakes, because it shows what the method really does rather than what you think it does.

40912273Position numbers start at 0 in most programming languages.
The list 4, 9, 2, 7 as cells. The largest so far changes only when a bigger item is met.

Trace the largest-number method on the list 4, 9, 2, 7:

StepItem looked atLargest so far
14 (first item)4
299
329
479
Endnone leftOutput: 9

Read the table from top to bottom. “Largest so far” starts with the first item, rises to 9 at step 2 and does not change after that, so the output is 9. If a trace shows a value you did not expect, the fault lies in the step that produced it.

Step 5 Worked example (4 marks)

Trace “add each number to a running total, starting at 0” on the list 3, 5, 2.

  1. Start: total = 0. (1 mark)
  2. After 3: total = 3. After 5: total = 8. (1 mark)
  3. After 2: total = 10. (1 mark)
  4. No items remain, so output 10. (1 mark)

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. What does a trace table record?

2. Trace “largest so far” on 3, 8, 5. What is the output?

3. Trace a running total from 0 on 4, 4, 4. What is the output?

4. Why act as the machine when tracing?

Phase 4: Close

Step 7 Recap

  • A trace follows the steps by hand and records every changing value.
  • A trace table has a row per step and a column per stored value.
  • Trace before you trust a method.

Step 8 Exit ticket (ungraded)

Trace “largest so far” on the list 1, 6, 6, 2 and write the final value.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Invent a four-item list where the largest item is the last one, and trace the method on it.

Common mistakes

MistakeWhy it fails
Skipping steps to save timeThe bug hides in the step you skipped.
Tracing what you meant, not what is writtenFollow the text exactly.
Not recording the starting valueRow one should show the initial state.

Limits. Hand tracing suits short methods. Longer programs use tools that record the trace automatically.