Array, element and index

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

Array, element and index

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement describes an array?
A A single variable that holds exactly one value
B A fixed-length structure that holds several elements under one name
2. In Marks[3], what is the 3?
A The index: the position of the element
B The value stored in the element
3. Which line reads element number 5 of the array Scores?
A Scores[5]
B Scores(5)
4. What is an element of an array?
A The whole array with all its values
B One stored value at a single index
5. Which set of index numbers could belong to an array?
A 1, 2, 3, 4, 5
B 1, 3, 8, 20, 21

Match the Pairs

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

Left columnRight column
1Marks[3] AIndex 6 does not exist because the array has a fixed length and its indexes only run from 1 to 5.
2DECLARE Marks : ARRAY[1:5] OF INTEGER BMarks[1], Marks[2], Marks[3]
3An element is one stored value, and it is reached by giving its index CThe element at index 3 of the array Marks; the index is the position number in square brackets
4An array is better than separate variables because one name holds many values that are reached by index DMarks[2] ← 75
5Marks(3) ← 60 ERound brackets are used for the index. The index must go in square brackets, as in Marks[3].
6Marks[6] ← 60 (array declared as ARRAY[1:5]) FCreates one array of 5 elements with consecutive indexes 1, 2, 3, 4, 5, each holding an integer

Definitions

ARRAY

Fill in the Blanks

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

1.
DECLARE Marks : _____[1:5] OF INTEGER
2.
DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[_] ← 90
3.
Marks[1] ← 60 Marks[2] ← 71 Marks[_] ← 84
4.
DECLARE Scores : ARRAY[1:3] OF INTEGER ______[2] ← 14
5.
Total ← Marks[1] _ Marks[2]

Spot the Error

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

Question 1
A Marks[1] ← 70
B Marks[2] ← 85
C Marks(3) ← 64
D OUTPUT Marks[2]
Question 2
A Age[] ← 15
B Age[2] ← 16
C OUTPUT Age[1]
Question 3
A // Prices: indexes 1 to 3
B Prices[0] ← 4.5
C Prices[2] ← 6
D OUTPUT Prices[2]
Question 4
A Names[1] ← "Ali"
B Names[2] ← "Bea"
C Names[3] ← "Cy"
D OUTPUT Names["Cy"]
Question 5
A Temp[1] ← 18, 21
B Temp[2] ← 21
C OUTPUT Temp[1] + Temp[2]

Answer Key

Array, element and index
Section A: Quick Questions
Q1
B — A fixed-length structure that holds several elements under one name
Q2
A — The index: the position of the element
Q3
A — Scores[5]
Q4
B — One stored value at a single index
Q5
A — 1, 2, 3, 4, 5
Section B: Match the Pairs
Q1
Marks[3] → The element at index 3 of the array Marks; the index is the position number in square brackets
Q2
DECLARE Marks : ARRAY[1:5] OF INTEGER → Creates one array of 5 elements with consecutive indexes 1, 2, 3, 4, 5, each holding an integer
Q3
An element is one stored value, and it is reached by giving its index → Marks[2] ← 75
Q4
An array is better than separate variables because one name holds many values that are reached by index → Marks[1], Marks[2], Marks[3]
Q5
Marks(3) ← 60 → Round brackets are used for the index. The index must go in square brackets, as in Marks[3].
Q6
Marks[6] ← 60 (array declared as ARRAY[1:5]) → Index 6 does not exist because the array has a fixed length and its indexes only run from 1 to 5.
Section C: Definitions
Q1
ARRAY — A fixed-length structure of elements; each element holds one value and is reached by its index, the position number in square brackets, such as Marks[3].
Section D: Fill in the Blanks
Q1
DECLARE Marks : ARRAY[1:5] OF INTEGER
Q2
DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[5] ← 90
Q3
Marks[1] ← 60 Marks[2] ← 71 Marks[3] ← 84
Q4
DECLARE Scores : ARRAY[1:3] OF INTEGER Scores[2] ← 14
Q5
Total ← Marks[1] + Marks[2]
Section E: Spot the Error
Q1
C — The index of an array element goes in square brackets, not round brackets. It should be Marks[3] ← 64.
Q2
A — Each element is reached by its index, so the position number must be written inside the square brackets. It should be Age[1] ← 15.
Q3
B — The comment says the indexes run from 1 to 3, so index 0 does not exist in this array. The first element is Prices[1] ← 4.5.
Q4
D — The index must be a whole number giving the position of the element, not the value stored in it. It should be OUTPUT Names[3].
Q5
A — Each element holds only one value, so you cannot store 18 and 21 in Temp[1]. Use a separate element for each value, such as Temp[1] ← 18 and Temp[2] ← 21.