1. Arrays, index and bounds
An array is declared with an identifier, its bounds and the data type of the items it holds: `DECLARE Data : ARRAY[1:150] OF STRING`.
In `ThisArray[n] ← 42`, the variable n is the index (or subscript). The minimum and maximum values n may take are the lower bound and the upper bound: 1 and 150 for Data. `ARRAY[1:2, 1:50]` has two dimensions.
An index outside the bounds is not valid. A loop `FOR PCount ← 0 TO 49` used on an array declared from 1 to 50 is an error.
number of elements = upper bound - lower bound + 1
Examiner insight
Worked example 13 marks
The following pseudocode represents part of the algorithm for a program.
Line numbers are for reference only.
``` 10 DECLARE Sheet4 : ARRAY[1:2, 1:50] OF INTEGER … 100 FOR PCount ← 0 TO 49 101 Sheet4[1, PCount] ← 0 102 Sheet4[2, PCount] ← 47 103 NEXT PCount ```
The pseudocode contains references to an array.
Complete the table by writing the answer for each row.
| Answer | |
|---|---|
| The dimension of the array | |
| The name of the variable used as an array index | |
| The number of elements in the array |
- 1
The dimension of the array: 2 (two pairs of bounds in the declaration) [1]
- 2
The variable used as an array index: `PCount` [1]
- 3
The number of elements in the array: 2 × 50 = 100 [1]