← IGCSE Computer Science Paper 2

Tracing a 1D array program with a trace table

A table that records the loop variable, index and array contents after each pass of a loop, so you can predict the final OUTPUT and spot errors such as off-by-one.

Understanding Tracing a 1D array program with a trace table in IGCSE Computer Science Paper 2

What it does: Each pass adds one element to Total. The last row of the trace table shows the value that OUTPUT displays. Each pass of the loop gets its own row. This lets you see exactly how the values change. The upper bound is 5. The loop must stop at 5, so a row with Index = 6 reveals the off-by-one error. Trace it: i = 2 changes Max to 8; i = 3 compares 2 > 8, which is false, so Max stays 8.

Common mistakes: The index must be the loop variable Index. Using Total as the index means the trace table shows the wrong elements being read. It should be Score[Index]. The trace table only fills Num[1] to Num[4]. Num[5] is outside the array (or never set). It should be OUTPUT Num[4], which gives 8.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 459–468) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which columns should a trace table for a FOR loop over a 1D array include?
A column for Index and a column for each variable and array element being changedOnly one column showing the final OUTPUT
Matches
Trace table has only an Index column, with no column for Total or the array contents
↓
The table is missing columns for the variables that change, so you cannot see the array contents or the running total after each pass
Applying
Meanings
Term
trace table
Meaning
A table that records the loop variable, index and array contents after each pass of a loop, so you can predict the final OUTPUT and spot errors such as off-by-one.
Practice
DECLARE Num : ARRAY[1:4] OF INTEGER
Testing
Gaps
FOR Index ← 1 3
Check Answer
Spots
2FOR Index ← 1 TO 3
3 Total ← Total + Score[Total]

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Woodpecker' badge — Tap Tap Tap, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which columns should a trace table for a FOR loop over a 1D array include?
A. Only one column showing the final OUTPUT
B. A column for Index and a column for each variable and array element being changed ✓
A trace table records values after each pass, so it needs a column for the index and for every variable or element that changes.
2. When do you add a new row to a trace table?
A. After each pass of the loop, to record the new values ✓
B. Only once, after the loop has finished
Each pass of the loop gets its own row. This lets you see exactly how the values change.
3. FOR Index ← 1 TO 3 then Scores[Index] ← Index * 2 then NEXT Index. After the loop, what is Scores[3]?
A. 6 ✓
B. 3
On the third pass Index is 3, so Scores[3] ← 3 * 2 = 6. The trace table row for Index = 3 shows this.
4. Nums holds 4, 5, 6. Total ← 0, then FOR i ← 1 TO 3: Total ← Total + Nums[i]. What is Total after the loop?
A. 9
B. 15 ✓
Total goes 4, then 9, then 15 in the three rows of the trace table. 9 is only the value after pass 2.
5. An array is declared ARRAY[1:5], but the loop is FOR Index ← 1 TO 6. What does the trace table show on the last row?
A. Index = 6, which is outside the bounds 1 to 5 (off-by-one error) ✓
B. Index = 6, which is fine because the loop says TO 6
The upper bound is 5. The loop must stop at 5, so a row with Index = 6 reveals the off-by-one error.

Match the Pairs

Index | Total | Scores[Index] → Column headings of a trace table: the loop variable, a running variable and the array element being used
Scores[3] ← 7 (row 3 of the table) → Record the new array contents after the pass where Index is 3
Use a trace table to find the final OUTPUT by following the loop one pass at a time → FOR Index ← 1 TO 3 Total ← Total + Nums[Index] NEXT Index OUTPUT Total
A trace table shows an off-by-one error when Index goes past the upper bound of the array → DECLARE Nums : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 6 OUTPUT Nums[Index] NEXT Index
Trace table has only an Index column, with no column for Total or the array contents → The table is missing columns for the variables that change, so you cannot see the array contents or the running total after each pass
FOR Index ← 0 TO 4 on ARRAY[1:5], trace starts with Index = 1 → The trace table starts at the wrong index. The loop begins at 0, which is below the lower bound of 1, so the first row should show Index = 0 and an error

Key Term

trace table
A table that records the loop variable, index and array contents after each pass of a loop, so you can predict the final OUTPUT and spot errors such as off-by-one.

Fill in the Blanks

1. DECLARE Score : ARRAY[1:3] OF INTEGER FOR Index ← 1 TO 3 OUTPUT Score[Index]
Hint: Type the keyword that gives the last value of Index, which is the final row of your trace table.
2. FOR Index ← 1 TO 3 Total ← Total + Score[Index] NEXT Index
Hint: Type the loop variable that picks the element; it is the Index column in the trace table.
3. DECLARE Mark : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 5 OUTPUT Mark[Index]
Hint: Type the upper bound of the array so the trace table stops at the last element and avoids an off-by-one error.
4. DECLARE Mark : ARRAY[0:2] OF INTEGER FOR Index ← 0 TO 2 Mark[Index] ← Index * 2
Hint: Type the lower bound of the array, which is the first Index value in the trace table.
5. Total ← 0 Total ← Total + Score[1] OUTPUT Total
Hint: Type the starting value of Total, which is shown in the first row of the trace table before any pass.

Spot the Error

Question 1
A Total ← 0
B FOR Index ← 1 TO 3
C Total ← Total + Score[Total]
D NEXT Index
E OUTPUT Total
Line C — The index must be the loop variable Index. Using Total as the index means the trace table shows the wrong elements being read. It should be Score[Index].
Question 2
A FOR i ← 1 TO 4
B Num[i] ← i * 2
C NEXT i
D OUTPUT Num[5]
Line D — The trace table only fills Num[1] to Num[4]. Num[5] is outside the array (or never set). It should be OUTPUT Num[4], which gives 8.
Question 3
A // Temp has indexes 1 to 5
B FOR Day ← 0 TO 5
C Temp[Day] ← Day * 10
D NEXT Day
E OUTPUT Temp[5]
Line B — This is an off-by-one error. The first row of the trace table would use Day = 0, but Temp[0] does not exist. The loop should start at 1.
Question 4
A Total ← 1
B FOR i ← 1 TO 3
C Total ← Total + Data[i]
D NEXT i
E OUTPUT Total
Line A — The Total column in the trace table must start at 0. Starting at 1 makes every row, and the final OUTPUT, too big by 1. It should be Total ← 0.
Question 5
A Big ← Data[1] // Data[1:4]
B FOR i ← 2 TO 3
C IF Data[i] > Big THEN
D Big ← Data[i]
E ENDIF
F NEXT i
Line B — This is an off-by-one error. The loop stops at 3, so the trace table never checks Data[4]. The upper limit should be 4.

Student with a class code? Join your class →