Using a variable as an array index

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

Using a variable as an array index

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Index ← 3 then OUTPUT Marks[Index]. Which element is displayed?
A Marks[Index], because the name Index is used as the position
B Marks[3], because Index holds 3
2. The same statement OUTPUT Marks[Index] runs twice. Index is 2 the first time and 5 the second time. What happens?
A It displays Marks[2] and then Marks[5]
B It displays Marks[2] both times
3. Which declaration suits a variable used as an array index?
A DECLARE Index : INTEGER
B DECLARE Index : STRING
4. Which line stores the value 72 in the element at the position held by Index?
A Index ← Marks[72]
B Marks[Index] ← 72
5. Index ← 4. Which line copies the element at that position into Score?
A Score ← Marks[Index]
B Marks[Index] ← Score

Match the Pairs

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

Left columnRight column
1Index ← 3 OUTPUT Marks[Index] AStores 50 in whichever element Index currently points to
2Marks[Index] ← 50 BIndex ← 2 OUTPUT Marks[Index] Index ← 5 OUTPUT Marks[Index]
3Changing the value of the index variable makes the same statement reach a different element CIndex is in quotation marks, so it is treated as text, not as the variable holding 4. It should be Marks[Index].
4The variable used as an index must be an INTEGER DDisplays the value stored in element 3 of Marks, because Index currently holds 3
5DECLARE Index : STRING Index ← 3 OUTPUT Marks[Index] EDECLARE Index : INTEGER
6Index ← 4 OUTPUT Marks["Index"] FIndex is declared as STRING, but a variable used as an array index must be an INTEGER whole number.

Definitions

Marks[Index]

Fill in the Blanks

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

1.
Index ← 3 OUTPUT Marks[_____]
2.
DECLARE Index : _______ Index ← 3 Marks[Index] ← 85
3.
Index ← 3 Index ← Index _ 1 OUTPUT Marks[Index]
4.
Index ← 3 Marks[Index] _ 85
5.
Index ← 5 ______ Marks[Index]

Spot the Error

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

Question 1
A DECLARE Index : STRING
B Index ← 3
C Marks[Index] ← 75
D OUTPUT Marks[Index]
Question 2
A Index ← 2
B Marks[Index] ← 60
C Marks(Index + 1) ← 70
D OUTPUT Marks[Index + 1]
Question 3
A Index ← 4
B Total ← Marks[Idx]
C OUTPUT Total
Question 4
A Index ← 2
B Marks[Index] ← 77
C Index ← 3
D Marks["Index"] ← 64
Question 5
A DECLARE Pos : INTEGER
B Pos ← 2.5
C Marks[Pos] ← 90
D OUTPUT Marks[Pos]

Answer Key

Using a variable as an array index
Section A: Quick Questions
Q1
B — Marks[3], because Index holds 3
Q2
A — It displays Marks[2] and then Marks[5]
Q3
A — DECLARE Index : INTEGER
Q4
B — Marks[Index] ← 72
Q5
A — Score ← Marks[Index]
Section B: Match the Pairs
Q1
Index ← 3 OUTPUT Marks[Index] → Displays the value stored in element 3 of Marks, because Index currently holds 3
Q2
Marks[Index] ← 50 → Stores 50 in whichever element Index currently points to
Q3
Changing the value of the index variable makes the same statement reach a different element → Index ← 2 OUTPUT Marks[Index] Index ← 5 OUTPUT Marks[Index]
Q4
The variable used as an index must be an INTEGER → DECLARE Index : INTEGER
Q5
DECLARE Index : STRING Index ← 3 OUTPUT Marks[Index] → Index is declared as STRING, but a variable used as an array index must be an INTEGER whole number.
Q6
Index ← 4 OUTPUT Marks["Index"] → Index is in quotation marks, so it is treated as text, not as the variable holding 4. It should be Marks[Index].
Section C: Definitions
Q1
Marks[Index] — A variable inside the brackets picks the element whose index matches its value. Change Index (an INTEGER) and the same statement reaches a different element of Marks.
Section D: Fill in the Blanks
Q1
Index ← 3 OUTPUT Marks[Index]
Q2
DECLARE Index : INTEGER Index ← 3 Marks[Index] ← 85
Q3
Index ← 3 Index ← Index + 1 OUTPUT Marks[Index]
Q4
Index ← 3 Marks[Index] ← 85
Q5
Index ← 5 OUTPUT Marks[Index]
Section E: Spot the Error
Q1
A — A variable used as an array index must be an INTEGER. Index is declared as STRING here. It should be DECLARE Index : INTEGER.
Q2
C — Array indexes go inside square brackets, not round brackets. It should be Marks[Index + 1] ← 70.
Q3
B — The index variable is called Index, but Idx has never been given a value. The brackets must use the same variable name: Total ← Marks[Index].
Q4
D — Quotes turn Index into a piece of text, not the variable. The variable name must be written without quotes: Marks[Index] ← 64.
Q5
B — Pos is an INTEGER and is used as an index, so it must be a whole number. An index such as 2.5 does not exist. It should be Pos ← 2.