Two-Dimensional Arrays
Learn Two-Dimensional Arrays for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers. A two-dimensional array is an array of arrays, storing data in rows and columns.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
A two-dimensional array stores data in rows and columns, like a table or grid, and each element needs two indexes.
An element is accessed as grid[row][column]. As with one-dimensional arrays, both indexes start at 0, so the top-left element of any 2D array is grid[0][0].
Processing a 2D array requires nested loops: the outer loop moves through the rows and the inner loop through the columns of each row. Getting the two indexes the wrong way round is the most frequent error, and it produces either wrong values or an out-of-range error if the grid is not square.
Revision notes
Structure and access
A 2D array stores data in rows and columns.
An element is accessed as grid[row][column], with the row index first. Both indexes start at 0, so grid[0][0] is the top-left element.
Processing with nested loops
The outer loop moves through the rows; the inner loop moves through the columns of each row.
This visits every element exactly once. A grid of 3 rows and 4 columns requires 3 × 4 = 12 iterations of the inner body.
Common uses
Game boards, seating plans, spreadsheets and images.
A noughts and crosses board is a 3 by 3 array; a class register of marks might be one row per student and one column per test.
Key points
- A 2D array stores data in rows and columns.
- Elements are accessed as grid[row][column].
- The row index comes first.
- Both indexes start at 0.
- Nested loops process a 2D array.
- The outer loop moves through rows.
Worked examples
Example 1
State how to access the element in row 2, column 3 of an array called grid. [2 marks]
Working
Example 2
A 2D array has 4 rows and 6 columns. Work out how many elements it holds. [2 marks]
Working
Example 3
Explain why nested loops are used to process a 2D array. [2 marks]
Working
Common mistakes
Reversing the row and column indexes.
The row index comes first.
Starting either index at 1.
Both start at 0.
Using a single loop.
Two dimensions need two nested loops.
Adding rows and columns for the element count.
Multiply them.
Exam tips
- Write the row index first, always.
- Multiply rows by columns for the total elements.
- Use nested loops with the outer moving through rows.
- Remember both indexes start at 0.
Key terms
- Two-dimensional array
- An array storing data in rows and columns.
- Row
- The first index of a 2D array.
- Column
- The second index of a 2D array.
- Nested loop
- Two loops used to traverse a 2D array.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.