1-D arrays
A list of the same type
- An array holds a fixed number of values of one type.
- The guide's lower bound is 1 unless a question says otherwise.
- One index, in square brackets, picks an element.
DECLARE Score : ARRAY[1:5] OF INTEGER
Score[1] ← 4
Score[2] ← 8
OUTPUT Score[1]
A loop visits every cell
- A
FORloop from the lower bound to the upper bound is how you fill or read the array.
DECLARE Score : ARRAY[1:3] OF INTEGER
Score[1] ← 4
Score[2] ← 8
Score[3] ← 6
DECLARE Total : INTEGER
DECLARE I : INTEGER
Total ← 0
FOR I ← 1 TO 3
Total ← Total + Score[I]
NEXT I
OUTPUT Total
Watch out
Score[1]is the first cell when the array was declared[1:5].- The index can be an expression:
Score[I + 1].
Common mistakes
For a maximum search, a stored candidate represents the best item seen so far. Trace an all-negative array: an initial candidate that is not an array item can give a wrong result.
- Declare the array before you store into a cell.
- The index must stay inside the declared bounds.
Now you try
- Store three numbers and total them.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Declare Score as ARRAY[1:3] OF INTEGER. Store 4 in cell 1 and output that cell.
Click Run to see the output here.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
The array is filled for you. Total the three values and output the total (18).
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Read NumberOfValues (1 to 6), then NumberOfValues integers into an array. Output how many are odd. Values may be negative.
Click Run to see the output here.