Working out how many elements an array holds

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

Working out how many elements an array holds

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How many elements does ARRAY[1:30] hold?
A 29
B 30
2. How many elements does ARRAY[0:9] hold?
A 10
B 9
3. Which formula gives the number of elements in a 1D array?
A upper - lower
B upper - lower + 1
4. How many elements does ARRAY[5:9] hold?
A 5
B 9
5. An array needs to hold 10 values and starts at index 0. Which declaration is correct?
A ARRAY[0:9] OF INTEGER
B ARRAY[0:10] OF INTEGER

Match the Pairs

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

Left columnRight column
1ARRAY[1:30] OF STRING AHolds 30 elements, because 30 - 1 + 1 = 30
2ARRAY[5:9] OF INTEGER BThe upper bound is not the size when the lower bound is not 1. The correct size is 9 - 5 + 1 = 5 elements.
3A lower bound of 0 means the size is one more than the upper bound, so an array with 10 elements ends at index 9 CThis forgets to add 1. The correct calculation is 9 - 0 + 1 = 10 elements.
4The number of elements is the upper bound minus the lower bound plus one DHolds 5 elements (indexes 5, 6, 7, 8 and 9)
5ARRAY[0:9] holds 9 elements because 9 - 0 = 9 ESize <- 30 - 1 + 1
6ARRAY[5:9] holds 9 elements because the upper bound is 9 FDECLARE Scores : ARRAY[0:9] OF INTEGER

Definitions

upper - lower + 1

Fill in the Blanks

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

1.
// ARRAY[0:9] Elements ← 9 - 0 + _
2.
Size ← _____ - lower + 1
3.
Size ← 9 _ 5 + 1 // ARRAY[5:9] holds 5 elements
4.
DECLARE Scores : ARRAY[5:_] OF INTEGER // holds 5 elements
5.
DECLARE Names : ARRAY[0:__] OF STRING // holds 30 elements

Spot the Error

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

Question 1
A // Marks : ARRAY[1:30]
B Size ← 30 + 1 + 1
C OUTPUT Size
Question 2
A Lower ← 0
B Upper ← 9
C Size ← Lower - Upper + 1
D OUTPUT Size
Question 3
A // Temps : ARRAY[5:9]
B Lower ← 5
C Upper ← 9
D Size ← Upper - Lower - 1
E OUTPUT Size
Question 4
A Size ← 9 - 0
B // ARRAY[0:9] holds Size items
C OUTPUT Size
Question 5
A // Scores : ARRAY[0:19]
B Lower ← 0
C Upper ← 19
D Size ← Upper - Lower
E OUTPUT Size

Answer Key

Working out how many elements an array holds
Section A: Quick Questions
Q1
B — 30
Q2
A — 10
Q3
B — upper - lower + 1
Q4
A — 5
Q5
A — ARRAY[0:9] OF INTEGER
Section B: Match the Pairs
Q1
ARRAY[1:30] OF STRING → Holds 30 elements, because 30 - 1 + 1 = 30
Q2
ARRAY[5:9] OF INTEGER → Holds 5 elements (indexes 5, 6, 7, 8 and 9)
Q3
A lower bound of 0 means the size is one more than the upper bound, so an array with 10 elements ends at index 9 → DECLARE Scores : ARRAY[0:9] OF INTEGER
Q4
The number of elements is the upper bound minus the lower bound plus one → Size <- 30 - 1 + 1
Q5
ARRAY[0:9] holds 9 elements because 9 - 0 = 9 → This forgets to add 1. The correct calculation is 9 - 0 + 1 = 10 elements.
Q6
ARRAY[5:9] holds 9 elements because the upper bound is 9 → The upper bound is not the size when the lower bound is not 1. The correct size is 9 - 5 + 1 = 5 elements.
Section C: Definitions
Q1
upper - lower + 1 — The number of elements in a 1D array: upper bound minus lower bound plus one. ARRAY[0:9] holds 9 - 0 + 1 = 10 elements, and ARRAY[5:9] holds 5.
Section D: Fill in the Blanks
Q1
// ARRAY[0:9] Elements ← 9 - 0 + 1
Q2
Size ← upper - lower + 1
Q3
Size ← 9 - 5 + 1 // ARRAY[5:9] holds 5 elements
Q4
DECLARE Scores : ARRAY[5:9] OF INTEGER // holds 5 elements
Q5
DECLARE Names : ARRAY[0:29] OF STRING // holds 30 elements
Section E: Spot the Error
Q1
B — The number of elements is upper - lower + 1, not upper + lower + 1. It should be Size ← 30 - 1 + 1, which gives 30.
Q2
C — The lower bound has been subtracted from the upper bound the wrong way round, giving a negative number. It should be Size ← Upper - Lower + 1, which gives 10.
Q3
D — You must add 1, not subtract it, because both the lower and upper bound positions count. It should be Size ← Upper - Lower + 1, which gives 5.
Q4
A — The + 1 is missing, so the program gives 9 instead of 10. Both bounds are elements, so it should be Size ← 9 - 0 + 1.
Q5
D — Leaving out + 1 gives 19, but ARRAY[0:19] holds 20 elements. The upper bound is one less than the size when the lower bound is 0. It should be Size ← Upper - Lower + 1.