Algorithmic Problem-Solving Guides: Tracing Algorithms Using Trace Tables (Copy)
Tracing Algorithms Using Trace Tables
What Trace Tables Are (Exam Definition)
- A trace table is a structured table used to:
- Track variable values step by step
- Show how an algorithm executes line by line
- Used to:
- Predict output
- Detect logic errors
- Verify loop and condition behaviour
- Frequently tested in:
- Dry-run questions
- “What is the output?” questions
- Debugging-style problems
Core Examiner Rule (Non-Negotiable)
- Marks are awarded for correct intermediate values, not just the final answer
- A correct final output with incorrect steps can still lose marks
Why Students Lose Easy Marks Here
- Skipping rows
- Updating variables in the wrong order
- Forgetting loop conditions
- Mixing old and new values
- Guessing instead of tracing
What A Proper Trace Table Must Show
- Variable names as column headings
- A new row every time a variable changes
- Values updated in correct execution order
Basic Trace Table Structure
| Step | Variable 1 | Variable 2 | Condition / Output |
|---|---|---|---|
| Start | Initial value | Initial value | — |
| 1 | Updated | Unchanged | — |
| 2 | Updated | Updated | OUTPUT |
Rule Before You Start Tracing
- Always write initial values first
- Even if they are 0, FALSE, or empty
Example 1: Simple Assignment Tracing
Pseudocode
a ← 5
b ← a + 3
a ← b - 2
OUTPUT a
Trace Table
| Step | a | b | Output |
|---|---|---|---|
| Start | — | — | — |
| 1 | 5 | — | — |
| 2 | 5 | 8 | — |
| 3 | 6 | 8 | — |
| 4 | 6 | 8 | 6 |
Key Learning
- Updates happen sequentially
- Later lines use latest values
Example 2: Tracing A FOR Loop
Pseudocode
total ← 0
FOR i ← 1 TO 4
total ← total + i
NEXT i
OUTPUT total
Trace Table
| i | total |
|---|---|
| Start | 0 |
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
Final Output
10
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Example 3: Tracing IF Statements
Pseudocode
x ← 4
IF x > 3 THEN
x ← x + 2
ELSE
x ← x - 2
ENDIF
OUTPUT x
Trace Table
| Step | x | Condition |
|---|---|---|
| Start | 4 | — |
| IF check | 4 | TRUE |
| Update | 6 | — |
| OUTPUT | 6 | — |
Key Learning
- Only one branch executes
- ELSE block is skipped entirely
Example 4: Tracing WHILE Loops
Pseudocode
count ← 1
total ← 0
WHILE count <= 3 DO
total ← total + count
count ← count + 1
ENDWHILE
OUTPUT total
Trace Table
| count | total |
|---|---|
| Start | 0 |
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
Loop Stops When
count = 4- Condition becomes FALSE
Example 5: Tracing REPEAT UNTIL Loops
Pseudocode
x ← 2
REPEAT
x ← x * 2
UNTIL x > 10
OUTPUT x
Trace Table
| Iteration | x |
|---|---|
| Start | 2 |
| 1 | 4 |
| 2 | 8 |
| 3 | 16 |
Key Difference
- Condition checked after execution
- Loop runs at least once
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Example 6: Tracing With Arrays
Pseudocode
DECLARE nums : ARRAY[1..3] OF INTEGER
nums[1] ← 5
nums[2] ← 2
nums[3] ← 7
sum ← 0
FOR i ← 1 TO 3
sum ← sum + nums[i]
NEXT i
OUTPUT sum
Trace Table
| i | nums[i] | sum |
|---|---|---|
| Start | — | 0 |
| 1 | 5 | 5 |
| 2 | 2 | 7 |
| 3 | 7 | 14 |
Final Output
14
Example 7: Tracing String Processing
Pseudocode
text ← "CAT"
count ← 0
FOR i ← 1 TO LENGTH(text)
IF text[i] = "A" THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Trace Table
| i | text[i] | count |
|---|---|---|
| 1 | C | 0 |
| 2 | A | 1 |
| 3 | T | 1 |
Output
1
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
How Examiners Expect Trace Tables To Be Presented
- Clear column headings
- No skipped iterations
- Logical progression
- Correct termination point
Common Trace Table Mistakes (High-Frequency)
Mistake 1: Updating Variables In Wrong Order
- Especially inside loops
Mistake 2: Forgetting Initial Values
- Causes entire table to be wrong
Mistake 3: Continuing After Loop Ends
- Loop stops as soon as condition fails
Mistake 4: Confusing Loop Variable With Data Variable
ivsnums[i]
Mistake 5: Mixing Old And New Values
- Always use latest assigned value
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Examiner Trace Table Checklist
- Are all variables included?
- Is each iteration shown?
- Are updates done in correct sequence?
- Does the loop stop correctly?
- Is the final output correct?
Exam-Ready Trace Table Strategy
- Write initial values
- Follow code line by line
- Update only when a statement executes
- Stop exactly when condition fails
- Double-check loop bounds
One-Line Rules To Memorise
- Trace, don’t guess
- Update in order
- One row per change
- Conditions control flow
- Accuracy beats speed
