← IGCSE Computer Science Paper 2

Searching a 2D array

Inside nested FOR loops, this compares each 2D array element with the wanted value; when it matches, store Row and Column as FoundRow and FoundColumn.

Understanding Searching a 2D array in IGCSE Computer Science Paper 2

What it does: The search checks every element and keeps going after a match, so Count totals every occurrence instead of stopping at the first. The nested loops visit every cell and the IF compares it with the empty marker, setting a flag when a match is found. You must name the element with both its row and column index. Grid on its own is the whole array, not one element. The loop variables Row and Column hold the position of the match. Wanted is the value being searched for, not its position. The comparison needs the current Row and Column values, which only exist inside the inner loop.

Common mistakes: ← assigns a value, it does not compare. A search must compare the element with the wanted value using =, so the line should be IF Grid[R, C] = Want THEN. A 2D array needs both a row index and a column index. Grid[R] is missing the column, so the line should be IF Grid[R, C] = Want THEN.

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
Which line compares each element of a 2D array called Grid with the wanted value?
IF Grid = Wanted THENIF Grid[Row, Column] = Wanted THEN
Matches
IF Grid[Row, Column] <- Wanted THEN
↓
<- is the assignment arrow, so it cannot be used to compare values. Use = to compare: IF Grid[Row, Column] = Wanted THEN
Applying
Meanings
Term
IF Grid[Row, Column] = Wanted THEN
Meaning
Inside nested FOR loops, this compares each 2D array element with the wanted value; when it matches, store Row and Column as FoundRow and FoundColumn.
Practice
FoundRow ← 0
Testing
Gaps
IF Grid[Row, Column] = THEN
Check Answer
Spots
2 FOR C ← 1 TO 4
3 IF Grid[R, C] ← Want THEN

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

The 'Seal' badge — Smooth Formatter, 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 compares each element of a 2D array called Grid with the wanted value?
A. IF Grid[Row, Column] = Wanted THEN ✓
B. IF Grid = Wanted THEN
You must name the element with both its row and column index. Grid on its own is the whole array, not one element.
2. To search every cell of a 2D array, which loop structure is needed?
A. A FOR loop for rows with a FOR loop for columns inside it ✓
B. A single FOR loop that counts from 1 to Wanted
Nested loops visit every row and every column in that row, so every cell is compared once.
3. A search finds Wanted at Grid[3, 2]. Which assignments record where it was found?
A. FoundRow ← Wanted and FoundColumn ← Wanted
B. FoundRow ← Row and FoundColumn ← Column ✓
The loop variables Row and Column hold the position of the match. Wanted is the value being searched for, not its position.
4. Where should the IF Grid[Row, Column] = Wanted THEN line go?
A. Before both FOR loops, so it only checks once
B. Inside the inner FOR loop, so it checks every cell ✓
The comparison needs the current Row and Column values, which only exist inside the inner loop.
5. How do you count how many times Wanted appears in a 2D array?
A. Set Count ← 0 before the loops, then Count ← Count + 1 each time a cell matches ✓
B. Set Count ← 1 inside the inner loop each time a cell matches
Count must start at 0 before the loops and increase by 1 for each match. Setting it to 1 only shows that at least one match exists.

Match the Pairs

IF Grid[Row, Column] = Wanted THEN → Compares the element at the current row and column with the wanted value inside nested loops
FoundRow ← Row → Stores the row number where the wanted value was found
To count how many times a value appears in a 2D array, add 1 to a counter each time an element matches → IF Grid[Row, Column] = Wanted THEN Count ← Count + 1
To check a grid for an empty cell, compare each element with the empty marker such as a space → IF Grid[Row, Column] = " " THEN Empty ← TRUE
IF Grid[Row, Column] <- Wanted THEN → <- is the assignment arrow, so it cannot be used to compare values. Use = to compare: IF Grid[Row, Column] = Wanted THEN
IF Grid[Row] = Wanted THEN → A 2D array element needs both indexes. Write Grid[Row, Column] so one cell is compared with Wanted

Key Term

IF Grid[Row, Column] = Wanted THEN
Inside nested FOR loops, this compares each 2D array element with the wanted value; when it matches, store Row and Column as FoundRow and FoundColumn.

Fill in the Blanks

1. FOR Column ← 1 TO 3 IF Grid[Row, Column] = Wanted THEN
Hint: Type the variable that holds the value being searched for.
2. IF Grid[Row, Column] = Wanted THEN FoundRow ← Row FoundColumn ← Column
Hint: Type the loop variable that stores which column the match was found in.
3. IF Grid[Row, Column] = Wanted THEN Count ← Count + 1 ENDIF
Hint: Type the arithmetic operator that adds to the counter each time a match is found.
4. FOR Column ← 1 TO 3 IF Grid[Row, Column] = "" THEN Empty ← TRUE
Hint: Type the keyword that follows an IF condition to begin the code run when the cell is empty.
5. IF Grid[Row, 1] = "X" AND Grid[Row, 2] = "X" AND Grid[Row, 3] = "X" THEN
Hint: Type the logical operator that makes all three cell checks have to be true.

Spot the Error

Question 1
A FOR R ← 1 TO 3
B FOR C ← 1 TO 4
C IF Grid[R, C] ← Want THEN
D FoundRow ← R
E FoundColumn ← C
F ENDIF
Line C — ← assigns a value, it does not compare. A search must compare the element with the wanted value using =, so the line should be IF Grid[R, C] = Want THEN.
Question 2
A Count ← 0
B FOR R ← 1 TO 3
C FOR C ← 1 TO 3
D IF Grid[R] = Want THEN
E Count ← Count + 1
F ENDIF
Line D — A 2D array needs both a row index and a column index. Grid[R] is missing the column, so the line should be IF Grid[R, C] = Want THEN.
Question 3
A // Grid: 3 rows, 4 columns
B FOR R ← 1 TO 4
C FOR C ← 1 TO 4
D IF Grid[R, C] = Want THEN
E FoundRow ← R
Line B — The grid has only 3 rows, so the row loop must stop at 3. Going to 4 would use Grid[4, C], which is out of range. It should be FOR R ← 1 TO 3.
Question 4
A FOR R ← 1 TO 2
B FOR C ← 1 TO 3
C IF Grid[C, R] = " " THEN
D Empty ← TRUE
E ENDIF
Line C — The row index must come first and the column index second. Grid[C, R] swaps them and could go out of range (the grid has 2 rows). It should be IF Grid[R, C] = " " THEN.
Question 5
A Count ← 1
B FOR R ← 1 TO 3
C FOR C ← 1 TO 3
D IF Grid[R, C] = Want THEN
E Count ← Count + 1
Line A — A counter must start at 0 before the loops. Starting at 1 makes the final count one too high. It should be Count ← 0.

Student with a class code? Join your class →