Reading and changing array elements

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

Reading and changing array elements

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores 55 in the third element of the array Marks?
A Marks[3] ← 55
B Marks ← 55[3]
2. Marks is declared as ARRAY[1:5] OF INTEGER. Which statement causes an error?
A Marks[5] ← 40
B Marks[6] ← 40
3. Index ← 4. Which expression refers to the fourth element of Marks?
A Marks(Index)
B Marks[Index]
4. Marks holds 60, 72, 45, 80, 90 at indexes 1 to 5. What does OUTPUT Marks[3] display?
A 45
B 72
5. Marks[3] currently holds 45. What happens when Marks[3] ← 55 runs?
A The 45 is replaced by 55
B 55 is inserted and the other values shift along

Match the Pairs

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

Left columnRight column
1Marks[3] ← 55 AIndex 6 is outside the bounds 1 to 5, so this causes an error. Use an index from 1 to 5, such as Marks[5].
2OUTPUT Marks[Index] BDisplays the element whose position is given by the variable Index
3Reading one element: the value stored at index 3 is copied into a variable CScore ← Marks[3]
4Using a variable as the index so the same line works on different elements DRound brackets are used instead of square brackets. The element should be written as Marks[3] ← 55.
5DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[6] ← 55 EChanges the element at index 3 of the array Marks to the value 55
6Marks(3) ← 55 FMarks[Index] ← 0

Definitions

Marks[3]

Fill in the Blanks

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

1.
// Change the element at index 3 Marks[_] ← 55
2.
DECLARE Marks : ARRAY[1:5] OF INTEGER OUTPUT _____[3]
3.
Index ← 4 OUTPUT Marks[_____]
4.
// Add 5 to element 2 Marks[2] _ Marks[2] + 5
5.
DECLARE Marks : ARRAY[1:5] OF INTEGER // Highest valid index is used here Marks[_] ← 70

Spot the Error

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

Question 1
A // Marks is indexed 1 to 5
B Marks[1] ← 70
C Marks[5] ← 64
D Marks(3) ← 55
E OUTPUT Marks[3]
Question 2
A // Marks is indexed 1 to 5
B Index ← 2
C Marks[Index] ← 80
D OUTPUT Marks[Indx]
Question 3
A Marks[2] ← 60
B Marks[3] = Marks[2] + 5
C OUTPUT Marks[3]
Question 4
A Marks ← 55
B OUTPUT Marks[3]
C Marks[3] ← Marks[3] + 5
Question 5
A // Marks is indexed 1 to 5
B FOR Index ← 1 TO 5
C OUTPUT Marks[Index + 1]
D NEXT Index

Answer Key

Reading and changing array elements
Section A: Quick Questions
Q1
A — Marks[3] ← 55
Q2
B — Marks[6] ← 40
Q3
B — Marks[Index]
Q4
A — 45
Q5
A — The 45 is replaced by 55
Section B: Match the Pairs
Q1
Marks[3] ← 55 → Changes the element at index 3 of the array Marks to the value 55
Q2
OUTPUT Marks[Index] → Displays the element whose position is given by the variable Index
Q3
Reading one element: the value stored at index 3 is copied into a variable → Score ← Marks[3]
Q4
Using a variable as the index so the same line works on different elements → Marks[Index] ← 0
Q5
DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[6] ← 55 → Index 6 is outside the bounds 1 to 5, so this causes an error. Use an index from 1 to 5, such as Marks[5].
Q6
Marks(3) ← 55 → Round brackets are used instead of square brackets. The element should be written as Marks[3] ← 55.
Section C: Definitions
Q1
Marks[3] — Refers to one element of the array Marks by its index, here 3. You can read it or assign to it, e.g. Marks[3] ← 55. An index outside the array bounds causes an error.
Section D: Fill in the Blanks
Q1
// Change the element at index 3 Marks[3] ← 55
Q2
DECLARE Marks : ARRAY[1:5] OF INTEGER OUTPUT Marks[3]
Q3
Index ← 4 OUTPUT Marks[Index]
Q4
// Add 5 to element 2 Marks[2] ← Marks[2] + 5
Q5
DECLARE Marks : ARRAY[1:5] OF INTEGER // Highest valid index is used here Marks[5] ← 70
Section E: Spot the Error
Q1
D — An element is chosen with square brackets, not round brackets. It should be Marks[3] ← 55.
Q2
D — The index variable is spelt Index, but Indx was never given a value. It should be OUTPUT Marks[Index].
Q3
B — Assigning to an element needs the assignment arrow ←, not =. It should be Marks[3] ← Marks[2] + 5.
Q4
A — Marks is the whole array. To store 55 in one element you must give its index, so it should be Marks[3] ← 55.
Q5
C — When Index is 5, Marks[Index + 1] is Marks[6], which is outside the bounds 1 to 5 and causes an error. It should be OUTPUT Marks[Index].