Paper 1 & 2 · Cambridge Pseudocode

8.2.3 · Arrays

Reading & writing 2D arrays with nested iteration

A 2D array is just a 1D array of rows. The mistake nearly every candidate makes is mixing up which index means which.

Core for all candidates Feeds directly into the 15-mark Paper 2 programming question Also covers 8.2.1, 8.2.2

Explainer

Think of a class register: one row of numbered seats, one value per seat. Now think of a spreadsheet: rows AND columns, and every cell needs two coordinates to find it, not one. That jump, from one number to two, is the entire difference between a 1D array and a 2D array.

A 1D array is a single row of boxes, each reached by one index: RegPlate[Space]. A 2D array is a grid, and every box needs two indices to find it: one for the row, one for the column.

Df

The syllabus declares a 2D array as rows first, then columns: DECLARE WeeklyUsage : ARRAY[1:50, 1:5] OF INTEGER means 50 rows (spaces) each with 5 columns (days). Reading it is WeeklyUsage[Space, Day], always in that declared order.

!

An array's first index can legally be 0 or 1, it depends entirely on how the DECLARE statement bounds it, there is no universal rule. WeeklyUsage here is declared [1:50, 1:5], so Space and Day both start at 1, matching the pseudocode above. Always read the declared bounds before assuming which number the count starts at, don't default to "starts at 0" out of habit.

Nested iteration is how you visit every box in a 2D array: an outer loop walks the rows, an inner loop walks the columns of the current row. The inner loop finishes completely before the outer loop moves to the next row.

Worked example

Total hours parked, per space, across the week

This is the exact data structure from a real Paper 2 scenario: 50 parking spaces, 5 days, hours recorded in WeeklyUsage[Space, Day]. To find the total hours for one space, the inner loop must finish all 5 days before the outer loop moves to the next space.

Click a cell to see how it’s indexed.

WeeklyUsageDay 1Day 2Day 3Day 4Day 5
Space 11
Space 12 WeeklyUsage[12,3]
Space 13

Row = space (outer index), column = day (inner index), matching declaration order. WeeklyUsage[12,3] is Space 12’s Day 3 value, not Day 12 of Space 3. The hours recorded is 6.

// Total hours parked for each of the 50 spaces, across all 5 days
DECLARE Space : INTEGER
DECLARE Day : INTEGER
DECLARE TotalHours : INTEGER

FOR Space  1 TO 50
    TotalHours  0          // reset BEFORE the inner loop starts
    FOR Day  1 TO 5
        TotalHours  TotalHours + WeeklyUsage[Space, Day]
    NEXT Day
    OUTPUT "Space ", Space, " total hours: ", TotalHours
NEXT Space
# Total hours parked for each of the 50 spaces, across all 5 days
for space in range(1, 51):
    total_hours = 0
    for day in range(1, 6):
        total_hours += WeeklyUsage[space][day]
    print("Space", space, "total hours:", total_hours)
// Total hours parked for each of the 50 spaces, across all 5 days
for (int space = 1; space <= 50; space++) {
    int totalHours = 0;
    for (int day = 1; day <= 5; day++) {
        totalHours += WeeklyUsage[space][day];
    }
    System.out.println("Space " + space + " total hours: " + totalHours);
}
' Total hours parked for each of the 50 spaces, across all 5 days
For space = 1 To 50
    totalHours = 0
    For day = 1 To 5
        totalHours = totalHours + WeeklyUsage(space, day)
    Next day
    Console.WriteLine("Space " & space & " total hours: " & totalHours)
Next space
?

What is the outer loop (FOR Space ← 1 TO 50) for? It moves through the 50 rows of WeeklyUsage, one space at a time: Space 1, then Space 2, and so on up to Space 50. Everything indented inside it runs once per space, including the entire inner loop below it.

?

What is the inner/nested loop (FOR Day ← 1 TO 5) for? For whichever space the outer loop is currently on, it moves through that space’s 5 columns, Day 1 through Day 5, adding each day’s hours to TotalHours. It runs all 5 days to completion before the outer loop is allowed to move to the next space, which is exactly why TotalHours is reset to 0 just before it starts: each space needs its own fresh total.

Pseudocode or program code accepted here: the 15-mark scenario question is the one place in Paper 2 where Cambridge allows Python, Java or Visual Basic instead of pseudocode. Every other Paper 2 question (validation, trace tables, error-correction) stays pseudocode-only.

Note the order inside the brackets never changes: it’s always [Space, Day] (or [space][day] in Python/Java), matching how WeeklyUsage was declared. Swap them and you read the wrong cell.

Check your understanding

In Cambridge pseudocode, Space 12’s hours on Day 3 are needed. Which line reads it correctly?

Explainer

How to trace code that indexes a 2D list

Before answering a “what does this print?” question, trace it exactly like a trace table: one row per loop pass, tracking the index and the running total. Here it is worked in full on a fresh example: rainfall in mm, recorded at 3 stations over 3 months.

# RainfallMM[0] = Station A, [1] = Station B, [2] = Station C
RainfallMM = [
    [12, 8, 15],
    [20, 5, 9],
    [3, 11, 7]
]

total = 0
for month in range(3):
    total = total + RainfallMM[0][month]
print(total)
?

What is RainfallMM[0]? It’s the whole first row, [12, 8, 15]: Station A’s three months of rainfall. Read individually, that’s Month 1 = 12mm, Month 2 = 8mm, Month 3 = 15mm.

?

What is RainfallMM[0][month]? It refers to Station A’s rainfall in that month, whichever month the loop is currently on. The first index (0) stays fixed at Station A for the whole loop; the second index (month) is the one that changes, counting 0, 1, 2 as the loop runs.

PassmonthRainfallMM[0][month]total
startn/an/a0
101212
21820
321535

The row index (0, fixed) never changes. Only month moves, from 0 to 2. Output: 35.

Check your understanding

Same technique, different row. What does this code output?

# same RainfallMM as above
total = 0
for month in range(3):
    total = total + RainfallMM[2][month]
print(total)

Common mistake

Markers reported this pattern for 8.2.1 / 8.2.3

RecurringMarkers reported the same failure repeatedly: candidates reading a 2D array incorrectly, almost always because they swapped or hard-coded one of the two indices instead of using both loop counters. The candidates who scored well were consistently the ones who correctly worked with both dimensions, not just one.

A second pattern shows up earlier, at the design stage: candidates who chose the wrong structure entirely, reaching for two separate 1D arrays instead of the single 2D array the scenario called for. Before writing any code, check the question: does one value depend on two things (a space and a day)? If yes, it’s a 2D array.

What this mistake looks like: TotalHours ← TotalHours + WeeklyUsage[Day, Space]: the indices are swapped. Cambridge pseudocode won’t flag this as a syntax error; it will just silently read the wrong cell, since [Day, Space] is still a valid position in the array, just not the one intended.

What it should say: TotalHours ← TotalHours + WeeklyUsage[Space, Day], matching the declared order every time, not just when it “feels right.” The loop variable that changes fastest (Day) still goes second; the outer loop variable (Space) still goes first.

Go deeper

15-mark question

Ready for the exam-standard version?

This statement is exactly what the hardest Paper 2 question tests: nested iteration over 1D and 2D arrays combined with validation and accumulation. The full worked remediation (anatomy, common mistakes, scaffold and mark scheme) is already built.

Open the 15-mark worksheet

Revision questions

Past-paper style, marked by the same AI feedback engine as your assignments.

Question 1 · 1 mark

[1]State the number of indices needed to identify one element in a two-dimensional array.

Question 2 · 3 marks

[3]Write pseudocode using nested iteration to output every value stored in WeeklyUsage[1:50, 1:5].

Question 3 · 4 marks

[4]A programmer writes WeeklyUsage[Day, Space] instead of WeeklyUsage[Space, Day] throughout their program. Explain the effect this has, and how to fix it.

🔒

2 more questions, AI-marked

Sign up free to unlock the rest of this set and get instant feedback on where marks were gained or lost.

Sign up, it’s free

Flashcards

Active recall for this statement’s key terms.

nested iteration
🔒
🔒
🔒
🔒