GCSE · Computer Science · AQA · Spec 8525

Two-dimensional arrays

A mark book has a row for each student and a column for each test. A two-dimensional array stores data exactly like that, and finds any value with two numbers.

Computer Science · Data structures

Watch two loops walk through a table

scores holds two students' marks in three quizzes. Row 0 is Asha, row 1 is Ben; columns 0, 1 and 2 are the three quizzes. In this lesson the row index comes first and both indices count from 0.

Before each click, predict: which cell will scores[row][col] point at next?

1 scores ← [[7, 4, 9],
2 [5, 8, 6]]
3 total ← 0
4 FOR row ← 0 TO 1
5 FOR col ← 0 TO 2
6 total ← total + scores[row][col]
7 ENDFOR
8 ENDFOR
9 OUTPUT total
rowcolscores[row][col]total
0

Output

 

Step 1: Picture scores as a table: row 0 holds Asha's quiz marks (7, 4, 9) and row 1 holds Ben's (5, 8, 6). Before any loop runs, total starts at 0.

1 / 8

Keep your eye on col: it climbs 0, 1, 2 — then snaps back to 0 the moment row moves on.

Step 1 of 8: Picture scores as a table: row 0 holds Asha's quiz marks (7, 4, 9) and row 1 holds Ben's (5, 8, 6). Before any loop runs, total starts at 0..

Watch out: The inner loop doesn't run once in total — it runs in full for every row. Two rows × three columns means six visits, not three.

Predict, then check

Same rule as the trace: the row index comes first, and both indices count from 0.

marks holds three students' results in four tests — one row per student, one column per test. Row 0: 12, 15, 9, 18 · Row 1: 14, 11, 17, 10 · Row 2: 16, 13, 8, 19. What does marks[2][1] give?

Computer Science · Data structures

One index or two?

Sort each data set: to point at one value, do you need one number or two?

Still to sort

One-dimensional array (0)

A single list: one index finds any value.

Where the line is: If one thing tells you which value you mean — which pupil, which day — one index is enough.

Two-dimensional array (0)

A table: a row index and a column index find any value.

Where the line is: If it takes two things to say which value you mean — which student AND which test — the data forms a table.

6 of 6 still to sort.

An array is a data structure that keeps many values of the same data type together under one name. A one-dimensional array is a single list; a two-dimensional array is a table of rows and columns.

Watch out: Big doesn't mean two-dimensional. A list of 365 values is still one list if one number is enough to find any of them.

Computer Science · Your turn

Fix one score, then total a column

points stores three players' scores over three rounds: one row per player, one column per round, both counted from 0. The second player's score in the third round was typed in wrongly — it should be 7. Fix it, then total everyone's scores in the first round.

  1. points ← [[3, 5, 2], [4, 1, 6], [2, 8, 5]]Three rows (one per player), each with three columns (one per round).
  2. missing step
Which line is step 2?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • An array is a data structure that stores a collection of values of the same data type under one identifier.
  • A two-dimensional array arranges its elements in rows and columns, like a table. You can think of it as an array of rows, each row the same length.
  • Every element is found with two indices, one for the row and one for the column: scores[1][2]. Here the row comes first and both count from 0 — always check the convention you're given.
  • Nested loops process every element: the outer loop steps through the rows and, for each row, the inner loop steps through every column.

The big picture

A two-dimensional array stores values of one data type in rows and columns under a single name, like a table. Each element is identified by two indices, one for its row and one for its column, so scores[1][2] means row 1, column 2 (in this lesson the row comes first and both count from 0). To process every element, use nested loops: the outer loop steps through the rows and, for each row, the inner loop steps through every column.

Key points

1Read an element with name[row][column]; change one with name[row][column] ← new value.
2With one index, name[row] refers to a whole row — the second index picks one value from it.
3The inner loop runs in full for every pass of the outer loop, so the column index resets each time the row changes.
4A grid of R rows and C columns has R × C elements, and a full traversal visits each of them once.
5Row total: fix the row, loop over the columns. Column total: fix the column, loop over the rows.
6Choose a two-dimensional array when the data naturally forms a table, such as a game board, a seating plan or class test results.

Worked example

Problem

marks holds four test results for each of three students: one row per student, one column per test, both counted from 0. marks ← [[12, 15, 9, 18], [14, 11, 17, 10], [16, 13, 8, 19]]. Write an algorithm that outputs the third student's total, and work out what it outputs.

⚠ Watch out

Counting from 1 when the array counts from 0. In a grid indexed from 0, the first value in the third row is scores[2][0], not scores[3][1]. Check the convention — which index comes first, and whether counting starts at 0 or 1 — before you read or write any element.

🧠

Memory hook

Read it like a theatre ticket: find your ROW first, then walk ALONG to your seat. name[row][column] — row, then along.

✓

Check yourself

Without scrolling up: grid ← [[2, 9], [7, 4], [5, 1]], indexed from 0 with the row first. What is grid[2][0]? And which statement changes the 4 into a 6?

Flashcards

(11)
What is an array?
A data structure that stores a collection of values of the same data type under one identifier.
What is a two-dimensional array?
An array whose elements are arranged in rows and columns, like a table — you can think of it as an array of rows of equal length.
How many indices pick out one element of a 2D array?
Two: one for the row and one for the column.
Thinking of scores as an array of rows, what does scores[1] on its own give?
The whole of row 1, which is itself an array. A second index then picks one value from it.
How do you change one element of a 2D array?
Assign to it using both indices, e.g. scores[1][2] ← 9.
Which pattern visits every element of a 2D array?
Nested loops: an outer loop over the rows and, inside it, an inner loop over the columns.
In a row-by-row traversal, what happens to the column index when the row index moves on?
It resets to the first column, and the inner loop runs in full again.
How many times does the inner loop body run for a grid of R rows and C columns?
R × C times — once for every element.
To total one column, which index stays fixed?
The column index. A single loop moves the row index through every row.
When is a 2D array a better choice than a 1D array?
When the data naturally forms a table, so it takes two numbers — row and column — to say which value you mean.
Counting from 0 with the row first, what are the indices of the last element in a 3-row, 4-column array?
[2][3] — row 2 is the third row and column 3 is the fourth column.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.