← IGCSE Computer Science Paper 2

Using variables as indexes in a 2D array

Uses two variables as the row and column indexes, so one statement can reach any element of a 2D array. Both variables must stay within their bounds.

Understanding Using variables as indexes in a 2D array in IGCSE Computer Science Paper 2

What it does: Row and Column hold 2 and 3, so the assignment writes 'O' into Grid[2, 3] without typing the numbers inside the brackets. Row is 4, but the declared row bounds are 1 to 3, so the index is out of range and the statement would fail. The first index is the row and the second is the column. So Grid[Row, Column] with Row 2 and Column 3 is row 2, column 3. A 2D array needs two indexes. Grid[Row, Column] gives one variable for the row and one for the column. One statement with one value for each index writes to exactly one element. It does not fill a whole row.

Common mistakes: A 2D array needs two indexes. Grid[Row] gives only the row. It should be Grid[Row, Column] ← 'O'. A value is stored in a variable with the assignment arrow ←, not =. It should be Column ← 4, so Column can be used as the column index.

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 469–478) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Row ← 2 and Column ← 3. Which element does Grid[Row, Column] refer to?
The element in row 2, column 3The element in row 3, column 2
Matches
Grid["Row", "Column"] ← 'O'
↓
The variable names are in quotes, so they are treated as text, not as the numbers stored in the variables. Indexes must be whole numbers.
Applying
Meanings
Term
Grid[Row, Column]
Meaning
Uses two variables as the row and column indexes, so one statement can reach any element of a 2D array. Both variables must stay within their bounds.
Practice
Row ← 2
Testing
Gaps
Grid[, Column] ← 'O'
Check Answer
Spots
2Column ← 3
3Grid[Row] ← 'O'

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

The 'Ostrich' badge — Full Sprint, 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. Row ← 2 and Column ← 3. Which element does Grid[Row, Column] refer to?
A. The element in row 3, column 2
B. The element in row 2, column 3 ✓
The first index is the row and the second is the column. So Grid[Row, Column] with Row 2 and Column 3 is row 2, column 3.
2. Which statement uses variables as BOTH indexes of a 2D array?
A. Grid[Row] ← 'O'
B. Grid[Row, Column] ← 'O' ✓
A 2D array needs two indexes. Grid[Row, Column] gives one variable for the row and one for the column.
3. Row ← 1 and Column ← 4. Grid[Row, Column] ← 'X' does what?
A. Stores 'X' in row 1, column 4 ✓
B. Stores 'X' in every element of row 1
One statement with one value for each index writes to exactly one element. It does not fill a whole row.
4. Row ← 2. Which element does Grid[Row+1, Column] refer to when Column ← 3?
A. Row 3, column 3 ✓
B. Row 2, column 4
The expression Row+1 is worked out first, giving 3. It is the row index, so the element is row 3, column 3. Column is unchanged.
5. Grid is declared as ARRAY[1:3, 1:4] OF CHAR. Which pair of values is safe for Row and Column?
A. Row ← 3, Column ← 4 ✓
B. Row ← 4, Column ← 3
Rows must stay between 1 and 3, and columns between 1 and 4. Row 4 is outside the row bounds, so it would cause an error.

Match the Pairs

Grid[Row, Column] ← 'O' → Writes 'O' into the element whose row number is stored in Row and whose column number is stored in Column
Grid[Row+1, Column] → Uses an expression for the row index, so it reaches the element one row below the current one in the same column
Store the row and column numbers in variables first, then use both variables as the indexes to set one element → Row ← 2 Column ← 3 Grid[Row, Column] ← 'O'
Both index variables must stay within the declared bounds, such as ARRAY[1:3, 1:4], or the access is out of range → Row ← 4 OUTPUT Grid[Row, Column]
Grid["Row", "Column"] ← 'O' → The variable names are in quotes, so they are treated as text, not as the numbers stored in the variables. Indexes must be whole numbers.
Row ← 3 OUTPUT Grid[Row+1, Column] → With 3 rows (1:3), Row+1 is 4, which is outside the upper bound, so the index is out of range.

Key Term

Grid[Row, Column]
Uses two variables as the row and column indexes, so one statement can reach any element of a 2D array. Both variables must stay within their bounds.

Fill in the Blanks

1. Row ← 2 Column ← 3 Grid[Row, Column] ← 'O'
Hint: Type the variable that holds the row number.
2. Row ← 2 Column ← 3 OUTPUT Grid[Row, Column]
Hint: Type the variable that holds the column number.
3. Row ← 2 Column ← 3 Grid[Row + 1, Column] ← 'O'
Hint: Type the operator that moves the row index to the next row down.
4. DECLARE Grid : ARRAY[1:3, 1:4] OF CHAR Column ← 4 Grid[2, Column] ← 'O'
Hint: Type the highest column number the declaration allows, so the variable stays within bounds.
5. Row ← 1 Column ← 2 OUTPUT Grid[Row, Column]
Hint: Type the keyword that displays the element found using the two variables.

Spot the Error

Question 1
A Row ← 2
B Column ← 3
C Grid[Row] ← 'O'
D OUTPUT Grid[Row, Column]
Line C — A 2D array needs two indexes. Grid[Row] gives only the row. It should be Grid[Row, Column] ← 'O'.
Question 2
A Row ← 1
B Column = 4
C Grid[Row, Column] ← 'X'
Line B — A value is stored in a variable with the assignment arrow ←, not =. It should be Column ← 4, so Column can be used as the column index.
Question 3
A FOR Row ← 1 TO 3
B Column ← 2
C Grid[Row, "Column"] ← 0
D NEXT Row
Line C — Quotes turn Column into the text "Column" instead of using the variable. An index must be a number or a variable. It should be Grid[Row, Column] ← 0.
Question 4
A // Grid is ARRAY[1:3, 1:4]
B Row ← 4
C Column ← 2
D Grid[Row, Column] ← 'O'
Line B — The array has rows 1 to 3 only. Row ← 4 is out of bounds, so Grid[Row, Column] fails. Row must stay within the bounds, for example Row ← 3.
Question 5
A // Grid is ARRAY[1:3, 1:4]
B Row ← 2
C Column ← 3
D Grid[Row+2, Column] ← 'O'
E OUTPUT Grid[3, Column]
Line D — Row+2 gives 4, but the rows only go from 1 to 3. The expression must stay within the bounds. It should be Grid[Row+1, Column], which is row 3.

Student with a class code? Join your class →