GCSE · Computer Science · AQA · Spec 8525

Two-dimensional arrays

A mark book has a row per student and a column per test. Want one mark? You need two numbers. That's a two-dimensional array.

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, call it: 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 is Asha's quiz marks (7, 4, 9), row 1 is Ben's (5, 8, 6). Nothing's been added yet, so 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 is Asha's quiz marks (7, 4, 9), row 1 is Ben's (5, 8, 6). Nothing's been added yet, so 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.

Call it, 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: a collection of values of the same data type, stored under one identifier.
  • A two-dimensional array sets those values out in rows and columns, like a table.
  • Think of it as an array of rows, where every row is the same length.
  • Every element has two indices, one for its row and one for its column — like scores[1][2].
  • Here the row comes first and both count from 0. Other places may differ, so always check the convention.
  • Have a goYour mate Jo is sure the 14 in marks ← [[12, 15], [14, 11]] lives at marks[1][2]. Row first, counting from 0 — where does it really live?

    marks[1][0]

    14 sits in the second row (row 1) but the first column (column 0). Jo got the row right, then counted the column from 1 — and there's no column 2 here at all.

  • Changing a value works the same way: grid[0][1] ← 5 replaces just that one element.
  • Choose a 2D array when the data naturally forms a table — a game board, a seating plan, class test scores.
  • Have a goA shop records how many of each of its 5 products it sells on each of 7 days. One index or two?

    Two — one for the product, one for the day.

    To point at one sales figure you must say which product AND which day, so the data forms a table. One index alone can't tell you which number you mean.

  • Nested loops visit every element: the outer loop steps through the rows, the inner loop sweeps each row's columns.
  • The inner loop runs in full for every row, so R rows and C columns means R × C visits.
  • Have a goA chessboard is stored as 8 rows by 8 columns. Sam reckons a nested loop over it visits 16 squares. How many does it really visit?

    64

    The inner loop sweeps all 8 columns for each of the 8 rows: 8 × 8 = 64. Sam added the loops (8 + 8) instead of multiplying them.

  • Fix the row and loop the columns for a row total; fix the column and loop the rows for a column total.

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 has 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. It feels natural — but indexed from 0, the first value in the third row is scores[2][0], not scores[3][1]. Before touching any element, check: which index comes first, and does counting start at 0 or 1?

🧠

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 28 October 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.