Some data comes as a table: sales for each shop in each week, a noughts and crosses board, marks for each student in each test. A two-dimensional array stores a table under one identifier, and nested loops visit every cell.
A two-dimensional (2D) array is a table of elements of the same data type, with two indexes: one for the row and one for the column. Declare it with two sets of bounds: DECLARE Sales : ARRAY[1:3, 1:4] OF INTEGER (3 rows, 4 columns, 12 elements). Access an element with Sales[Row, Column]. To process every element, use nested loops: the inner loop runs completely for every pass of the outer loop.
The two bounds in the declaration give the number of rows and the number of columns. In this course the first index is the row and the second is the column.
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR DECLARE Sales : ARRAY[1:3, 1:4] OF INTEGER Board[2, 3] ← 'X' OUTPUT Sales[2, 3]
| Sales | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | 5 | 8 | 2 | 6 |
| Row 2 | 3 | 9 | 4 | 7 |
| Row 3 | 6 | 1 | 5 | 8 |
With the values in the table, OUTPUT Sales[2, 3] outputs 4: row 2, column 3.
The same idea in Python (for comparison only: the exam uses pseudocode)
sales = [[5, 8, 2, 6],
[3, 9, 4, 7],
[6, 1, 5, 8]] # a list of lists; indexes start at 0
print(sales[1][2]) # 4: row 2, column 3
Sales[3, 1] and Sales[1, 4], and how many elements does Sales have?Do not swap the indexes. Sales[3, 2] is row 3, column 2 (the value 1), but Sales[2, 3] is row 2, column 3 (the value 4).
In the lab, choose 2D array with nested loops. Step through and watch the inner Column loop run all the way through for each value of Row.
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
FOR Row ← 1 TO 3
RowTotal ← 0
FOR Column ← 1 TO 4
RowTotal ← RowTotal + Sales[Row, Column]
NEXT Column
OUTPUT "Shop ", Row, " total: ", RowTotal
NEXT RowThe same idea in Python (for comparison only: the exam uses pseudocode)
for row in range(3):
row_total = 0
for column in range(4):
row_total = row_total + sales[row][column]
print("Shop", row + 1, "total:", row_total)
RowTotal ← 0 must be inside the outer loop but before the inner loop. Outside both loops, each row's total would include the rows before it.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
How many elements are in ARRAY[1:4, 1:5]?
Using the Sales table in this lesson, what is Sales[3, 2]?
In the lab's 2D array program the inputs are 4, 7, 2, 10, 3, 8 (row 1 first). What is the total for row 2?
Which statement puts 'X' in row 2, column 3 of Board?
Swap the loops to work down each column instead of across each row. A grand total can be built at the same time.
DECLARE ColumnTotal : INTEGER
DECLARE GrandTotal : INTEGER
GrandTotal ← 0
FOR Column ← 1 TO 4
ColumnTotal ← 0
FOR Row ← 1 TO 3
ColumnTotal ← ColumnTotal + Sales[Row, Column]
NEXT Row
OUTPUT "Week ", Column, " total: ", ColumnTotal
GrandTotal ← GrandTotal + ColumnTotal
NEXT Column
OUTPUT "Grand total: ", GrandTotal
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Which declares a 2D array to store 3 test marks (REAL) for each of 10 students, one row per student?
Total starts at 0. FOR R ← 1 TO 2, FOR C ← 1 TO 2, Total ← Total + R + C, then NEXT C and NEXT R. What is Total at the end?
In a row-total program, why is RowTotal ← 0 placed inside the outer loop?
An outer loop runs 4 times and its inner loop runs 6 times for each pass. How many times does the inner statement run?
This skill is now recorded on your profile. Keep the chain going.