Declaring and using a 2D array

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

Declaring and using a 2D array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
3. Which statement stores 'X' in row 2, column 3 of the grid?
A NoughtsAndCrosses[2,3] ← 'X'
B NoughtsAndCrosses[3,2] ← 'X'
4. How many elements does DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR hold?
A 9
B 6
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

Match the Pairs

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

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

Definitions

DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR

Fill in the Blanks

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

1.
DECLARE Grid : _____[1:3, 1:3] OF CHAR
2.
DECLARE Grid : ARRAY[1:3, 1:3] OF ____
3.
// 3 rows and 4 columns DECLARE Board : ARRAY[1:3, 1:_] OF CHAR
4.
// row 2, column 3 NoughtsAndCrosses[_,3] ← 'X'
5.
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 Grid[Row, ___] ← '-'

Spot the Error

Shade in the letter of the line that contains an 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
Question 2
A DECLARE Board :
B ARRAY[1:3] OF CHAR
C Board[2,3] ← 'X'
Question 3
A Board[1,1] ← 'O'
B Board[2][3] ← 'X'
C OUTPUT Board[2,3]
Question 4
A Board[1,1] ← 'X'
B Board[2,2] ← 'X'
C Board[3,3] ← 'X'
D OUTPUT Board[2,4]
Question 5
A FOR Row ← 1 TO 3
B FOR Col ← 1 TO 3
C INPUT Board[Row]
D NEXT Col
E NEXT Row

Answer Key

Declaring and using a 2D array
Section A: Quick Questions
Q1
B — DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Q2
A — The rows, from lower index 1 to upper index 3
Q3
A — NoughtsAndCrosses[2,3] ← 'X'
Q4
A — 9
Q5
B — A FOR loop for Row with a nested FOR loop for Column inside it
Section B: Match the Pairs
Q1
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR → Declares a 2D array of characters with 3 rows and 3 columns, giving 9 elements
Q2
NoughtsAndCrosses[2,3] ← 'X' → Stores 'X' in the element in row 2, column 3
Q3
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
Q4
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
Q5
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].
Q6
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].
Section C: Definitions
Q1
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'.
Section D: Fill in the Blanks
Q1
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Q2
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Q3
// 3 rows and 4 columns DECLARE Board : ARRAY[1:3, 1:4] OF CHAR
Q4
// row 2, column 3 NoughtsAndCrosses[2,3] ← 'X'
Q5
FOR Row ← 1 TO 3 FOR Col ← 1 TO 3 Grid[Row, Col] ← '-'
Section E: Spot the Error
Q1
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'.
Q2
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.
Q3
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'.
Q4
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].
Q5
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].