Working out how many elements a 2D array holds

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

Working out how many elements a 2D array holds

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How many elements does ARRAY[1:3, 1:3] hold?
A 9
B 6
2. How many elements does ARRAY[1:30, 1:4] hold?
A 120
B 34
3. Which formula gives the total number of elements in a 2D array?
A rows + columns
B rows * columns
4. How many values does one dimension of ARRAY[0:4, 1:3] have for its first index (0:4)?
A 5
B 4
5. How many elements does ARRAY[0:4, 1:3] hold?
A 12
B 15

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 INTEGER AThis 2D array has 30 rows and 4 columns, so it holds 30 * 4 = 120 elements
2DECLARE Marks : ARRAY[1:30, 1:4] OF INTEGER BThis 2D array has 3 rows and 3 columns, so it holds 3 * 3 = 9 elements
3The total number of elements in a 2D array is the number of rows multiplied by the number of columns CThe sizes of the dimensions were added instead of multiplied. It should be 3 * 3 = 9 elements.
4The size of each dimension is the upper bound minus the lower bound plus one DSize ← Upper - Lower + 1
5ARRAY[1:3, 1:3] holds 3 + 3 = 6 elements ETotal ← Rows * Columns
6ARRAY[1:30, 1:4] has 29 * 3 = 87 elements FUpper minus lower was used without adding 1. It should be (30 - 1 + 1) * (4 - 1 + 1) = 30 * 4 = 120 elements.

Definitions

rows * columns

Fill in the Blanks

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

1.
rows ← 3 columns ← 4 total ← rows _ columns
2.
// ARRAY[1:3, 1:3] holds 3 * 3 = _ elements
3.
// 30 rows * 4 columns = 120 elements DECLARE Marks : ARRAY[1:30, 1:_] OF INTEGER
4.
upper ← 30 lower ← 1 rowCount ← upper - lower + _
5.
DECLARE Board : ARRAY[0:2, 0:3] OF CHAR columns ← 3 - _ + 1 OUTPUT 3 * columns

Spot the Error

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

Question 1
A // Grid: ARRAY[1:3, 1:3]
B Rows ← 3
C Cols ← 3
D Total ← Rows + Cols
E OUTPUT Total
Question 2
A // Seats: ARRAY[1:30, 1:4]
B Rows ← 30 - 1
C Cols ← 4 - 1 + 1
D Total ← Rows * Cols
Question 3
A // Marks: ARRAY[1:30, 1:4]
B Rows ← 30
C Cols ← 3
D Total ← Rows * Cols
Question 4
A // Board: ARRAY[1:3, 1:3]
B Rows ← 3 - 1 + 1
C Cols ← 3 - 1 + 1
D Total ← Rows / Cols
E OUTPUT "Total is ", Total
Question 5
A Count ← 0 // ARRAY[1:3, 1:3]
B FOR R ← 1 TO 3
C FOR C ← 1 TO 2
D Count ← Count + 1
E NEXT C
F NEXT R

Answer Key

Working out how many elements a 2D array holds
Section A: Quick Questions
Q1
A — 9
Q2
A — 120
Q3
B — rows * columns
Q4
A — 5
Q5
B — 15
Section B: Match the Pairs
Q1
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER → This 2D array has 3 rows and 3 columns, so it holds 3 * 3 = 9 elements
Q2
DECLARE Marks : ARRAY[1:30, 1:4] OF INTEGER → This 2D array has 30 rows and 4 columns, so it holds 30 * 4 = 120 elements
Q3
The total number of elements in a 2D array is the number of rows multiplied by the number of columns → Total ← Rows * Columns
Q4
The size of each dimension is the upper bound minus the lower bound plus one → Size ← Upper - Lower + 1
Q5
ARRAY[1:3, 1:3] holds 3 + 3 = 6 elements → The sizes of the dimensions were added instead of multiplied. It should be 3 * 3 = 9 elements.
Q6
ARRAY[1:30, 1:4] has 29 * 3 = 87 elements → Upper minus lower was used without adding 1. It should be (30 - 1 + 1) * (4 - 1 + 1) = 30 * 4 = 120 elements.
Section C: Definitions
Q1
rows * columns — The total number of elements in a 2D array. Each dimension holds upper - lower + 1 values, so ARRAY[1:3, 1:3] holds 3 * 3 = 9 and ARRAY[1:30, 1:4] holds 30 * 4 = 120.
Section D: Fill in the Blanks
Q1
rows ← 3 columns ← 4 total ← rows * columns
Q2
// ARRAY[1:3, 1:3] holds 3 * 3 = 9 elements
Q3
// 30 rows * 4 columns = 120 elements DECLARE Marks : ARRAY[1:30, 1:4] OF INTEGER
Q4
upper ← 30 lower ← 1 rowCount ← upper - lower + 1
Q5
DECLARE Board : ARRAY[0:2, 0:3] OF CHAR columns ← 3 - 0 + 1 OUTPUT 3 * columns
Section E: Spot the Error
Q1
D — The number of elements is rows multiplied by columns, not added. It should be Total ← Rows * Cols, which gives 3 * 3 = 9.
Q2
B — Each dimension holds upper - lower + 1 values. The +1 is missing, so Rows would be 29. It should be Rows ← 30 - 1 + 1, which gives 30.
Q3
C — The second dimension is 1:4, so it holds 4 - 1 + 1 = 4 columns, not 3. It should be Cols ← 4, giving Total = 30 * 4 = 120.
Q4
D — Dividing rows by columns gives 1, not the number of elements. It should be Total ← Rows * Cols, which gives 9.
Q5
C — The inner loop must visit all 3 columns. TO 2 skips the last column, so Count would be 6 instead of 9. It should be FOR C ← 1 TO 3.