Using an expression as an array index

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

Using an expression as an array index

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. n ← 3. Which element does Scores[n+1] refer to?
A Scores[4]
B Scores[3]
2. When is the expression n+1 in StudentNames[n+1] worked out?
A Before the element is chosen
B After the element has been read
3. Which line copies the value in element n into the next element?
A StudentNames[n+1] ← StudentNames[n]
B StudentNames[n] ← StudentNames[n+1]
4. n ← 5. Which element does StudentNames[n-1] refer to?
A Element 6, the next one
B Element 4, the previous one
5. An array is declared as ARRAY[1:10] OF INTEGER and n ← 10. What happens with Marks[n+1]?
A It is fine and the array grows to hold element 11
B The index is 11, which is outside the bounds, so it is an error

Match the Pairs

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

Left columnRight column
1Scores[n + 1] AWorks out n + 1 first, then uses the element at that index, the one after position n
2Scores[n - 1] BWorks out n - 1 first, then uses the element at that index, the one before position n
3Copy the value in element n into the next element along Cn - 1 works out to 0, which is below the lower bound of 1, so the index is out of range
4Check that n + 1 stays within the bounds of an array declared ARRAY[1:10] before using it DStudentNames[n + 1] ← StudentNames[n]
5StudentNames[n + 1] ← StudentNames[n] when n = 10 and the array is ARRAY[1:10] En + 1 works out to 11, which is above the upper bound of 10, so the index is out of range
6StudentNames[n - 1] ← StudentNames[n] when n = 1 and the array is ARRAY[1:10] FIF n + 1 <= 10 THEN StudentNames[n + 1] ← StudentNames[n] ENDIF

Definitions

Array[n+1]

Fill in the Blanks

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

1.
StudentNames[___] ← StudentNames[n]
2.
Diff ← Marks[n] - Marks[___]
3.
n ← 3 OUTPUT Scores[n+1] // Shows element Scores[_]
4.
IF n + 1 __ 10 THEN Names[n + 1] ← Names[n] ENDIF
5.
// Names is ARRAY[1:10] FOR n ← 1 TO _ Names[n + 1] ← Names[n]

Spot the Error

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

Question 1
A n ← 3
B Names[n-1] ← Names[n]
C OUTPUT Names[4]
Question 2
A n ← 5
B previous ← Score[n+1]
C OUTPUT previous
Question 3
A n ← 4
B IF n+1 <= 5 THEN
C Names[n]+1 ← "Zoe"
D ENDIF
Question 4
A n ← 3
B Names(n+1) ← Names[n]
C OUTPUT Names[n+1]
Question 5
A n ← 2.5
B Total ← Marks[n] + Marks[n+1]
C OUTPUT Total

Answer Key

Using an expression as an array index
Section A: Quick Questions
Q1
A — Scores[4]
Q2
A — Before the element is chosen
Q3
A — StudentNames[n+1] ← StudentNames[n]
Q4
B — Element 4, the previous one
Q5
B — The index is 11, which is outside the bounds, so it is an error
Section B: Match the Pairs
Q1
Scores[n + 1] → Works out n + 1 first, then uses the element at that index, the one after position n
Q2
Scores[n - 1] → Works out n - 1 first, then uses the element at that index, the one before position n
Q3
Copy the value in element n into the next element along → StudentNames[n + 1] ← StudentNames[n]
Q4
Check that n + 1 stays within the bounds of an array declared ARRAY[1:10] before using it → IF n + 1 <= 10 THEN StudentNames[n + 1] ← StudentNames[n] ENDIF
Q5
StudentNames[n + 1] ← StudentNames[n] when n = 10 and the array is ARRAY[1:10] → n + 1 works out to 11, which is above the upper bound of 10, so the index is out of range
Q6
StudentNames[n - 1] ← StudentNames[n] when n = 1 and the array is ARRAY[1:10] → n - 1 works out to 0, which is below the lower bound of 1, so the index is out of range
Section C: Definitions
Q1
Array[n+1] — An index can be an expression like n+1. The expression is worked out first, then that element is used. It must stay within the array's bounds. Example: n=3 gives Array[4].
Section D: Fill in the Blanks
Q1
StudentNames[n+1] ← StudentNames[n]
Q2
Diff ← Marks[n] - Marks[n-1]
Q3
n ← 3 OUTPUT Scores[n+1] // Shows element Scores[4]
Q4
IF n + 1 <= 10 THEN Names[n + 1] ← Names[n] ENDIF
Q5
// Names is ARRAY[1:10] FOR n ← 1 TO 9 Names[n + 1] ← Names[n]
Section E: Spot the Error
Q1
B — To copy a value to the NEXT element the index must be n+1. n-1 is the previous element (2), so the value goes to the wrong place and Names[4] is not set. It should be Names[n+1] ← Names[n].
Q2
B — The previous element is found with n-1. n+1 gives the next element (6), not the previous one. It should be previous ← Score[n-1].
Q3
C — The +1 must be inside the square brackets so that the index expression n+1 is worked out before the element is chosen. It should be Names[n+1] ← "Zoe".
Q4
B — Array indexes are written in square brackets, not round brackets. It should be Names[n+1] ← Names[n].
Q5
A — An array index must be a whole number. With n as 2.5, n+1 is 3.5, which is not a valid element. It should be n ← 2.