Declaring a 2D array: ARRAY[l1:u1, l2:u2]

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

Declaring a 2D array: ARRAY[l1:u1, l2:u2]

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly declares a 2D array for a 3 by 3 noughts and crosses board?
A DECLARE NoughtsAndCrosses : ARRAY[1:3][1:3] OF CHAR
B DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
2. In ARRAY[1:4, 1:6] OF INTEGER, what separates the range for the rows from the range for the columns?
A A comma
B A semicolon
3. Which declaration gives an array with 5 rows and 2 columns?
A DECLARE Scores : ARRAY[1:2, 1:5] OF INTEGER
B DECLARE Scores : ARRAY[1:5, 1:2] OF INTEGER
4. True or false: ARRAY[0:2, 1:3] is a valid pair of ranges, because each dimension can have its own lower bound.
A True
B False
5. Which declaration is wrong because the array is meant to hold CHAR values in some elements and INTEGER values in others?
A DECLARE Board : ARRAY[1:3, 1:3] OF CHAR and INTEGER
B DECLARE Board : ARRAY[1:3, 1:3] OF CHAR

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 with 3 rows and 3 columns, where every element stores one CHAR
2DECLARE Marks : ARRAY[0:4, 0:2] OF INTEGER BThe keyword OF is missing before the data type. It should be ARRAY[1:4, 1:3] OF CHAR.
3The two ranges for the two dimensions are separated by a comma inside the square brackets CDECLARE Board : ARRAY[1:3, 1:3] OF CHAR
4Every element in a 2D array shares one data type, written after OF DThe two ranges are written in separate square brackets. They must be in one pair of brackets separated by a comma: ARRAY[1:3, 1:3].
5DECLARE Grid : ARRAY[1:3][1:3] OF CHAR EDeclares a 2D array with indexes 0 to 4 in the first dimension and 0 to 2 in the second, storing INTEGER values
6DECLARE Scores : ARRAY[1:4, 1:3] CHAR FDECLARE Seats : ARRAY[1:5, 1:8] OF BOOLEAN

Definitions

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

Fill in the Blanks

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

1.
_______ Board : ARRAY[1:3, 1:3] OF CHAR
2.
DECLARE Board : ARRAY[1:3, 1:_] OF CHAR
3.
DECLARE Grid : ARRAY[1:3, 1:3] __ CHAR
4.
DECLARE Board : ARRAY[1:3, 1:3] OF ____
5.
DECLARE Table : ARRAY[0:2, _:3] OF INTEGER

Spot the Error

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

Question 1
A DECLARE Grid =
B ARRAY[1:3, 1:3] OF CHAR
C Grid[1, 1] ← 'X'
D OUTPUT Grid[1, 1]
Question 2
A DECLARE Board :
B ARRAY[1:3, 1:3] OF CHAR
C Board[4, 3] ← 'O'
D OUTPUT Board[3, 3]
Question 3
A DECLARE Seats :
B ARRAY[0:2; 0:4] OF BOOLEAN
C Seats[0, 0] ← TRUE
Question 4
A DECLARE Marks :
B ARRAY[1:2, 1:3] OF INTEGER
C Marks[1, 1] ← 70
D Marks[2, 3] ← "pass"
Question 5
A DECLARE Grid :
B ARRAY[1:3, 1:3] OF CHAR
C Grid[1, 2] ← 'X'
D OUTPUT Grid[2]

Answer Key

Declaring a 2D array: ARRAY[l1:u1, l2:u2]
Section A: Quick Questions
Q1
B — DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
Q2
A — A comma
Q3
B — DECLARE Scores : ARRAY[1:5, 1:2] OF INTEGER
Q4
A — True
Q5
A — DECLARE Board : ARRAY[1:3, 1:3] OF CHAR and INTEGER
Section B: Match the Pairs
Q1
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR → Declares a 2D array with 3 rows and 3 columns, where every element stores one CHAR
Q2
DECLARE Marks : ARRAY[0:4, 0:2] OF INTEGER → Declares a 2D array with indexes 0 to 4 in the first dimension and 0 to 2 in the second, storing INTEGER values
Q3
The two ranges for the two dimensions are separated by a comma inside the square brackets → DECLARE Seats : ARRAY[1:5, 1:8] OF BOOLEAN
Q4
Every element in a 2D array shares one data type, written after OF → DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q5
DECLARE Grid : ARRAY[1:3][1:3] OF CHAR → The two ranges are written in separate square brackets. They must be in one pair of brackets separated by a comma: ARRAY[1:3, 1:3].
Q6
DECLARE Scores : ARRAY[1:4, 1:3] CHAR → The keyword OF is missing before the data type. It should be ARRAY[1:4, 1:3] OF CHAR.
Section C: Definitions
Q1
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR — Declares a 2D array called Grid. The first range is the rows, the second is the columns, split by a comma. All elements share one data type, here CHAR.
Section D: Fill in the Blanks
Q1
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q2
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q3
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Q4
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q5
DECLARE Table : ARRAY[0:2, 0:3] OF INTEGER
Section E: Spot the Error
Q1
A — A declaration needs a colon between the identifier and ARRAY. It should be DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR, so the first line should be DECLARE Grid :
Q2
C — The first range is 1:3, so the row index can only be 1, 2 or 3. Row 4 is out of range. It should be Board[3, 3] ← 'O'.
Q3
B — The two ranges must be separated by a comma, not a semicolon. It should be ARRAY[0:2, 0:4] OF BOOLEAN.
Q4
D — All elements share one data type. The array is declared OF INTEGER, so a string cannot be stored in it. It should be Marks[2, 3] ← 85.
Q5
D — A 2D array needs two indexes, one for the row and one for the column. It should be OUTPUT Grid[1, 2].