Searching a 2D array

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Searching a 2D array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

IF Grid[Row, Column] = Wanted THEN

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
FOR Column ← 1 TO 3 IF Grid[Row, Column] = ______ THEN
2.
IF Grid[Row, Column] = Wanted THEN FoundRow ← Row FoundColumn ← ______
3.
IF Grid[Row, Column] = Wanted THEN Count ← Count _ 1 ENDIF
4.
FOR Column ← 1 TO 3 IF Grid[Row, Column] = "" ____ Empty ← TRUE
5.
IF Grid[Row, 1] = "X" AND Grid[Row, 2] = "X" ___ Grid[Row, 3] = "X" THEN

Spot the Error

Shade in the letter of the line that contains an 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
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
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
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
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

Answer Key

Searching a 2D array
Section A: Quick Questions
Q1
A — IF Grid[Row, Column] = Wanted THEN
Q2
A — A FOR loop for rows with a FOR loop for columns inside it
Q3
B — FoundRow ← Row and FoundColumn ← Column
Q4
B — Inside the inner FOR loop, so it checks every cell
Q5
A — Set Count ← 0 before the loops, then Count ← Count + 1 each time a cell matches
Section B: Match the Pairs
Q1
IF Grid[Row, Column] = Wanted THEN → Compares the element at the current row and column with the wanted value inside nested loops
Q2
FoundRow ← Row → Stores the row number where the wanted value was found
Q3
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
Q4
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
Q5
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
Q6
IF Grid[Row] = Wanted THEN → A 2D array element needs both indexes. Write Grid[Row, Column] so one cell is compared with Wanted
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
FOR Column ← 1 TO 3 IF Grid[Row, Column] = Wanted THEN
Q2
IF Grid[Row, Column] = Wanted THEN FoundRow ← Row FoundColumn ← Column
Q3
IF Grid[Row, Column] = Wanted THEN Count ← Count + 1 ENDIF
Q4
FOR Column ← 1 TO 3 IF Grid[Row, Column] = "" THEN Empty ← TRUE
Q5
IF Grid[Row, 1] = "X" AND Grid[Row, 2] = "X" AND Grid[Row, 3] = "X" THEN
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.