Unable to open this exercise
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.
Independent Practice: Row totals
Read six integers into a 2-row, 3-column array in row order. Output the total of row 1, then the total of row 2. Reset the total at the correct point.
Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.
Interactive exercises for Module 2 and beyond require an active plan.
Run code to inspect step-by-step variable trace states.