Unable to open this exercise
Common exam pitfall
Students often forget the comma separating row and column indices.
A Two-Dimensional (2D) Array represents data arranged in a grid of rows and columns (a matrix). Like 1D arrays, all elements in a 2D array must be of the same data type.
1. Declaration in Cambridge Pseudocode
DECLARE <Identifier> : ARRAY[<RowStart>:<RowEnd>, <ColStart>:<ColEnd>] OF <DataType>
Example:
DECLARE ChessBoard : ARRAY[1:8, 1:8] OF CHAR
DECLARE StudentMarks : ARRAY[1:30, 1:4] OF INTEGER // 30 students, 4 subjects
Row-Major Indexing:
Cambridge pseudocode follows the standard mathematical convention where the first index represents the row and the second index represents the column:
2. Iterating Across 2D Grids
Traversing every cell in a 2D array requires nested FOR loops:
- The outer loop iterates through the rows.
- The inner loop iterates through the columns for each row:
FOR r <- 1 TO 30
TotalScore <- 0
FOR c <- 1 TO 4
TotalScore <- TotalScore + StudentMarks[r, c]
NEXT c
OUTPUT "Student ", r, " Total: ", TotalScore
NEXT r
Worked non-square traversal
For two rows [2,4,6] and [10,20,30], row totals are 12 and 60. Set Total to zero at the start of EACH row, then add its three columns. If Total is reset only once, the second result becomes 72. In ARRAY[1:2,1:3], swapping the indices can also attempt a nonexistent third row.
Before solving, draw the two-by-three table, label both index ranges and trace the reset. Then use data different from this worked example.
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Exam-Style Question: Access Specific 2D Matrix Cells
Given the 2D array SeatingPlan : ARRAY[1:5, 1:8] OF STRING:
- To reference the student sitting in Row 2, Column 5, write
SeatingPlan[Space 1]. - The total number of seats in the room is [Space 2].
Interactive exercises for Module 2 and beyond require an active plan.