Cambridge IGCSE · Topic 8.2
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Rows and columns.

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.

The key idea

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.

Sales : ARRAY[1:3, 1:4] OF INTEGER column (week) 1234 row 1row 2row 3 5826 3947 6158 Sales[2, 3] = 4 first index = row, second index = column
Fig. 1: a 2D array of sales for 3 shops (rows) over 4 weeks (columns)
Section 01

Declaring a 2D array.

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]
SalesColumn 1Column 2Column 3Column 4
Row 15826
Row 23947
Row 36158

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
Your turn
Using the table, what are Sales[3, 1] and Sales[1, 4], and how many elements does Sales have?
Examiner trap

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).

Section 02

Nested loops.

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 Row

The 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)
Worked example Row totals
Using the Sales table, what does the program above output?
  1. Row 1: RowTotal resets to 0, then 5 + 8 + 2 + 6 = 21.
  2. Row 2: reset, then 3 + 9 + 4 + 7 = 23.
  3. Row 3: reset, then 6 + 1 + 5 + 8 = 20.
  4. The inner statement runs 3 × 4 = 12 times in all.
Answer: Shop 1 total: 21, Shop 2 total: 23, Shop 3 total: 20
Examiner trap

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.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

How many elements are in ARRAY[1:4, 1:5]?

Quick check+10 XP

Using the Sales table in this lesson, what is Sales[3, 2]?

Quick check+10 XP

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?

Quick check+10 XP

Which statement puts 'X' in row 2, column 3 of Board?

Section 03

Column totals and grand totals.

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
Worked example Column totals
Using the Sales table, what does this program output?
  1. Week 1: 5 + 3 + 6 = 14. Week 2: 8 + 9 + 1 = 18.
  2. Week 3: 2 + 4 + 5 = 11. Week 4: 6 + 7 + 8 = 21.
  3. Grand total: 14 + 18 + 11 + 21 = 64.
  4. Check with the row totals: 21 + 23 + 20 = 64.
Answer: 14, 18, 11, 21, then Grand total: 64
Your turn
A 2D array is declared ARRAY[1:5, 1:6]. How many times does the statement inside a pair of nested loops over every element run?
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which declares a 2D array to store 3 test marks (REAL) for each of 10 students, one row per student?

Exam style+20 XP

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?

Exam style+20 XP

In a row-total program, why is RowTotal ← 0 placed inside the outer loop?

Exam style+20 XP

An outer loop runs 4 times and its inner loop runs 6 times for each pass. How many times does the inner statement run?

Skill unlocked

Two-dimensional arrays, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Declaring 2D arrays, reading elements by row and column, and using nested loops for totals.