Two-dimensional array: rows and columns

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

Two-dimensional array: rows and columns

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A noughts and crosses board has 3 rows and 3 columns. Which declaration suits it?
A DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
B DECLARE Board : ARRAY[1:3] OF CHAR
2. In Marks[2, 3], what does the first index (2) tell you?
A The column number
B The row number
3. Which statement about a 2D array is true?
A Rows run across and columns run down
B Rows run down and columns run across
4. Which element is in row 1, column 3 of the Board array?
A Board[3, 1]
B Board[1, 3]
5. How many index numbers do you need to identify ONE element in a 2D array?
A Two: a row and a column
B One: just the row

Match the Pairs

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

Left columnRight column
1DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR AOnly one range is given, so this is a 1D list, not a table. A 2D array needs two ranges, such as ARRAY[1:4, 1:3].
2Marks[2, 3] BA 2D array with 3 rows and 3 columns that holds characters, such as a noughts and crosses board
3The first index is the row and the second is the column, so this puts an X in the top row, third column of a noughts and crosses grid COnly one index is given. An element of a 2D array needs two indexes, a row and a column, such as Grid[2, 1].
4A table of marks for 4 students and 3 tests needs rows and columns, so the array has two ranges DGrid[1, 3] ← 'X'
5Grid[2] ← 'O' EDECLARE Marks : ARRAY[1:4, 1:3] OF INTEGER
6DECLARE Marks : ARRAY[1:4] OF INTEGER FThe element in row 2 and column 3 of the Marks table

Definitions

2D array

Fill in the Blanks

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

1.
DECLARE Board : _____[1:3, 1:3] OF CHAR
2.
// Put X in row 2, column 3 Board[2, _] ← 'X'
3.
// Student 4 scored 75 in test 2 Marks[_, 2] ← 75
4.
// Show the mark in row 2, column 3 ______ Marks[2, 3]
5.
FOR Row ← 1 TO 5 OUTPUT Marks[___, 1] NEXT Row

Spot the Error

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

Question 1
A Board[1, 1] ← "X"
B Board[2, 2] ← "O"
C Board[3] ← "X"
D OUTPUT Board[2, 2]
Question 2
A Marks[1, 1] ← 72
B Marks[1, 2] ← 65
C Marks[2, 1] ← 80
D Marks(2, 2) ← 91
Question 3
A // Marks[student, test]
B Marks[3, 2] ← 58
C Total ← Marks[3, 1]
D Total ← Total + Marks[2, 3]
Question 4
A Row ← 2
B Col ← 3
C Grid[Row, Col, 1] ← "X"
D OUTPUT Grid[Row, Col]
Question 5
A Board[3, 1] ← "O"
B OUTPUT "Top right square:"
C OUTPUT Board[1, 3]

Answer Key

Two-dimensional array: rows and columns
Section A: Quick Questions
Q1
A — DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q2
B — The row number
Q3
A — Rows run across and columns run down
Q4
B — Board[1, 3]
Q5
A — Two: a row and a column
Section B: Match the Pairs
Q1
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR → A 2D array with 3 rows and 3 columns that holds characters, such as a noughts and crosses board
Q2
Marks[2, 3] → The element in row 2 and column 3 of the Marks table
Q3
The first index is the row and the second is the column, so this puts an X in the top row, third column of a noughts and crosses grid → Grid[1, 3] ← 'X'
Q4
A table of marks for 4 students and 3 tests needs rows and columns, so the array has two ranges → DECLARE Marks : ARRAY[1:4, 1:3] OF INTEGER
Q5
Grid[2] ← 'O' → Only one index is given. An element of a 2D array needs two indexes, a row and a column, such as Grid[2, 1].
Q6
DECLARE Marks : ARRAY[1:4] OF INTEGER → Only one range is given, so this is a 1D list, not a table. A 2D array needs two ranges, such as ARRAY[1:4, 1:3].
Section C: Definitions
Q1
2D array — A table of rows and columns held in one variable. Each element needs two indexes: the first is the row (across), the second is the column (down), e.g. Marks[2, 3].
Section D: Fill in the Blanks
Q1
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q2
// Put X in row 2, column 3 Board[2, 3] ← 'X'
Q3
// Student 4 scored 75 in test 2 Marks[4, 2] ← 75
Q4
// Show the mark in row 2, column 3 OUTPUT Marks[2, 3]
Q5
FOR Row ← 1 TO 5 OUTPUT Marks[Row, 1] NEXT Row
Section E: Spot the Error
Q1
C — A 2D array element needs two indexes: the row and then the column. Board[3] gives only one. It should be Board[3, 1] ← "X".
Q2
D — Array indexes go inside square brackets, not round brackets. The line should be Marks[2, 2] ← 91.
Q3
D — The first index is the row (the student) and the second is the column (the test). Student 3's second test is Marks[3, 2], not Marks[2, 3].
Q4
C — A 2D array needs exactly two indexes, one for the row and one for the column. A third index is wrong. It should be Grid[Row, Col] ← "X".
Q5
A — The top right square is in row 1 (the first index) and column 3 (the second index). Board[3, 1] is the bottom left square. It should be Board[1, 3] ← "O".