← IGCSE Computer Science Paper 2

Declaring and using a 2D array

Declares a 2D array of characters with 3 rows and 3 columns. Reach one element with a row index then a column index, e.g. NoughtsAndCrosses[2,3] ← 'X'.

Understanding Declaring and using a 2D array in IGCSE Computer Science Paper 2

What it does: The two index ranges 1:4 and 1:5 give 4 rows and 5 columns, which is how a 2D array is declared. For each row the inner loop runs through all the columns, so every element is reached by its row index and column index. The first range gives the row indices (lower:upper). The second range gives the column indices. The data type comes after OF. 3 rows x 3 columns = 9 elements. Do not add the two sizes together. The row index must be between 1 and 3. Row 4 is outside the declared range, so [4,1] does not exist.

Common mistakes: The inner loop is controlled by Col, so it must end with NEXT Col. NEXT Row belongs to the outer loop on the last line. The line should be ' NEXT Col'. A 2D array needs two index ranges, one for rows and one for columns. ARRAY[1:3] only declares a 1D array. It should be ARRAY[1:3, 1:3] OF CHAR.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 176–187) and word search (Modules 166–187).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly declares a 2D array of characters with 3 rows and 3 columns?
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHARDECLARE NoughtsAndCrosses : ARRAY[1:3][1:3] OF CHAR
Matches
DECLARE Board : ARRAY[1:3] OF CHAR
↓
Only one index range is given, so this declares a 1D array. A 2D array needs two ranges, for example ARRAY[1:3, 1:3].
Applying
Meanings
Term
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Meaning
Declares a 2D array of characters with 3 rows and 3 columns. Reach one element with a row index then a column index, e.g. NoughtsAndCrosses[2,3] ← 'X'.
Practice
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Testing
Gaps
DECLARE Grid : [1:3, 1:3] OF CHAR
Check Answer
Spots
3 Board[Row,Col] ← '-'
4 NEXT Row

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Gulper Eel' badge — Deep Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly declares a 2D array of characters with 3 rows and 3 columns?
A. DECLARE NoughtsAndCrosses : ARRAY[1:3][1:3] OF CHAR
B. DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR ✓
Cambridge pseudocode puts both index ranges inside ONE pair of square brackets, separated by a comma: ARRAY[l1:u1, l2:u2].
2. In ARRAY[1:3, 1:3], what does the first range 1:3 describe?
A. The rows, from lower index 1 to upper index 3 ✓
B. The data type of every element
The first range gives the row indices (lower:upper). The second range gives the column indices. The data type comes after OF.
3. Which statement stores 'X' in row 2, column 3 of the grid?
A. NoughtsAndCrosses[2,3] ← 'X' ✓
B. NoughtsAndCrosses[3,2] ← 'X'
The row index comes first and the column index second, so row 2, column 3 is written [2,3].
4. How many elements does DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR hold?
A. 9 ✓
B. 6
3 rows x 3 columns = 9 elements. Do not add the two sizes together.
5. Which code visits every element of the 3 by 3 grid?
A. A single FOR loop from 1 TO 3 using one index
B. A FOR loop for Row with a nested FOR loop for Column inside it ✓
A 2D array needs two indices, so use nested FOR loops: the outer loop steps through the rows and the inner loop steps through the columns.

Match the Pairs

DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR → Declares a 2D array of characters with 3 rows and 3 columns, giving 9 elements
NoughtsAndCrosses[2,3] ← 'X' → Stores 'X' in the element in row 2, column 3
A 2D array is declared with a lower and upper bound for the rows and for the columns → DECLARE Grid : ARRAY[1:4, 1:5] OF INTEGER
Nested FOR loops visit every element of a 2D array, the outer loop for rows and the inner loop for columns → FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 OUTPUT NoughtsAndCrosses[Row,Col] NEXT Col NEXT Row
DECLARE Board : ARRAY[1:3] OF CHAR → Only one index range is given, so this declares a 1D array. A 2D array needs two ranges, for example ARRAY[1:3, 1:3].
NoughtsAndCrosses[2][3] ← 'X' → The row and column indexes are written in separate brackets. In pseudocode they go in one pair of brackets separated by a comma: [2,3].

Key Term

DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Declares a 2D array of characters with 3 rows and 3 columns. Reach one element with a row index then a column index, e.g. NoughtsAndCrosses[2,3] ← 'X'.

Fill in the Blanks

1. DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Hint: Type the keyword that declares a table of values with two index ranges.
2. DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Hint: Type the data type used to store single characters such as X and O.
3. // 3 rows and 4 columns DECLARE Board : ARRAY[1:3, 1:4] OF CHAR
Hint: Type the upper bound of the second range, which sets the number of columns.
4. // row 2, column 3 NoughtsAndCrosses[2,3] ← 'X'
Hint: Type the first index, which picks the row.
5. FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 Grid[Row, Col] ← '-'
Hint: Type the inner loop variable that is used as the column index.

Spot the Error

Question 1
A FOR Row ← 1 TO 3
B FOR Col ← 1 TO 3
C Board[Row,Col] ← '-'
D NEXT Row
E NEXT Row
Line D — The inner loop is controlled by Col, so it must end with NEXT Col. NEXT Row belongs to the outer loop on the last line. The line should be ' NEXT Col'.
Question 2
A DECLARE Board :
B ARRAY[1:3] OF CHAR
C Board[2,3] ← 'X'
Line B — A 2D array needs two index ranges, one for rows and one for columns. ARRAY[1:3] only declares a 1D array. It should be ARRAY[1:3, 1:3] OF CHAR.
Question 3
A Board[1,1] ← 'O'
B Board[2][3] ← 'X'
C OUTPUT Board[2,3]
Line B — In Cambridge pseudocode both indices go inside one pair of square brackets, separated by a comma: row then column. It should be Board[2,3] ← 'X'.
Question 4
A Board[1,1] ← 'X'
B Board[2,2] ← 'X'
C Board[3,3] ← 'X'
D OUTPUT Board[2,4]
Line D — Board is declared as ARRAY[1:3, 1:3], so the column index can only be 1, 2 or 3. Column 4 does not exist. Use a valid index such as Board[2,2].
Question 5
A FOR Row ← 1 TO 3
B FOR Col ← 1 TO 3
C INPUT Board[Row]
D NEXT Col
E NEXT Row
Line C — An element of a 2D array needs both a row index and a column index. Board[Row] has no column. It should be INPUT Board[Row,Col].

Student with a class code? Join your class →