Arrays · Cambridge syllabus 7.4, 8.2 · Lesson 3 of 3
Two-Dimensional Arrays
A two-dimensional array uses two indexes, usually row and column. Nested loops process every location: the outer loop chooses a row and the inner loop visits its columns. Keep the declared bounds and ordering consistent.
By the end, you can
- Declare and use two-dimensional arrays.
- Access values using row and column indexes.
- Use nested iteration to process 2D arrays.
2D arrays
A two-dimensional array is a table: elements arranged in rows and columns, each reached by two indexes. DECLARE Grid : ARRAY[1:4, 1:6] OF INTEGER gives four rows of six columns, twenty-four elements in all.
An element is addressed by row and then column: Grid[2, 5] is the element in row 2, column 5. The order is a convention that has to be kept consistently, because Grid[5, 2] is a different element and, in this array, does not exist at all.
It suits data that is naturally a table: marks for several pupils across several subjects, a seating plan, a game board, a timetable, a spreadsheet. The row identifies one thing and the column another.
Processing needs nested loops, with the outer loop over the rows and the inner over the columns. For each row, the inner loop runs completely before the outer one advances, so the elements are visited row by row from left to right — which is the order a table is read.
The common patterns follow from that. Totalling a single row means one loop over the columns with the row index fixed; totalling a column means one loop over the rows with the column fixed; totalling everything means both loops with one accumulator initialised before either of them. Printing a row on a line means starting a new line at the end of each pass of the outer loop, not the inner one.
Worth remembering
- Two indexes, row first then column: Grid[2, 5] is row 2, column 5.
- Nested loops process it, outer over rows and inner over columns.
- The inner loop completes in full on every pass of the outer loop.
Worked example
A 3-row, 4-column array has 12 elements. Grid[2,3] selects row 2, column 3 in a one-based declaration.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
4 × 5 = 20 values.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.