Using variables as indexes in a 2D array

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

Using variables as indexes in a 2D array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which statement uses variables as BOTH indexes of a 2D array?
A Grid[Row] ← 'O'
B Grid[Row, Column] ← 'O'
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
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
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

Match the Pairs

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

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

Definitions

Grid[Row, Column]

Fill in the Blanks

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

1.
Row ← 2 Column ← 3 Grid[___, Column] ← 'O'
2.
Row ← 2 Column ← 3 OUTPUT Grid[Row, ______]
3.
Row ← 2 Column ← 3 Grid[Row _ 1, Column] ← 'O'
4.
DECLARE Grid : ARRAY[1:3, 1:4] OF CHAR Column ← _ Grid[2, Column] ← 'O'
5.
Row ← 1 Column ← 2 ______ Grid[Row, Column]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Row ← 2
B Column ← 3
C Grid[Row] ← 'O'
D OUTPUT Grid[Row, Column]
Question 2
A Row ← 1
B Column = 4
C Grid[Row, Column] ← 'X'
Question 3
A FOR Row ← 1 TO 3
B Column ← 2
C Grid[Row, "Column"] ← 0
D NEXT Row
Question 4
A // Grid is ARRAY[1:3, 1:4]
B Row ← 4
C Column ← 2
D Grid[Row, Column] ← 'O'
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]

Answer Key

Using variables as indexes in a 2D array
Section A: Quick Questions
Q1
B — The element in row 2, column 3
Q2
B — Grid[Row, Column] ← 'O'
Q3
A — Stores 'X' in row 1, column 4
Q4
A — Row 3, column 3
Q5
A — Row ← 3, Column ← 4
Section B: Match the Pairs
Q1
Grid[Row, Column] ← 'O' → Writes 'O' into the element whose row number is stored in Row and whose column number is stored in Column
Q2
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
Q3
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'
Q4
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]
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
Row ← 2 Column ← 3 Grid[Row, Column] ← 'O'
Q2
Row ← 2 Column ← 3 OUTPUT Grid[Row, Column]
Q3
Row ← 2 Column ← 3 Grid[Row + 1, Column] ← 'O'
Q4
DECLARE Grid : ARRAY[1:3, 1:4] OF CHAR Column ← 4 Grid[2, Column] ← 'O'
Q5
Row ← 1 Column ← 2 OUTPUT Grid[Row, Column]
Section E: Spot the Error
Q1
C — A 2D array needs two indexes. Grid[Row] gives only the row. It should be Grid[Row, Column] ← 'O'.
Q2
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.
Q3
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.
Q4
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.
Q5
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.