An array stores a fixed number of values of the same data type under one identifier, each reached by its index. One index gives a list; two give a table. Learn the vocabulary examiners use, then process whole arrays with loops.
An array is a fixed-length data structure of elements that all have the same data type, held under one identifier. Each element is reached by its index (subscript). The lower bound is the index of the first element and the upper bound the index of the last, so ARRAY[1:30] has 30 elements. A 1D array is a list with one index; a 2D array is a table reached by [row, column]. Arrays are processed one element at a time with a FOR loop; a 2D array needs nested loops.
Declare the array with its bounds and data type, then use a loop variable as the index to visit every element in turn.
Scores[3], which holds 7 in Fig. 1.Scores[i + 1].ARRAY[0:9] has 10.DECLARE Scores : ARRAY[1:6] OF INTEGER
DECLARE Max : INTEGER
DECLARE MaxIndex : INTEGER
DECLARE i : INTEGER
Max ← Scores[1]
MaxIndex ← 1
FOR i ← 2 TO 6
IF Scores[i] > Max THEN
Max ← Scores[i]
MaxIndex ← i
ENDIF
NEXT i
OUTPUT "Highest ", Max, " at position ", MaxIndex
| i | Scores[i] | Scores[i] > Max | Max | MaxIndex |
|---|---|---|---|---|
| 12 | 1 | |||
| 2 | 31 | TRUE | 31 | 2 |
| 3 | 7 | FALSE | ||
| 4 | 31 | FALSE | ||
| 5 | 25 | FALSE | ||
| 6 | 9 | FALSE |
Output: Highest 31 at position 2. Because the test is > and not >=, the first position of a tied maximum is kept.
Names : ARRAY[1:30] OF STRING to the empty string.FOR Index ← 1 TO 30 / Names[Index] ← "" / NEXT Index. The guide does not allow Names[1 TO 30] ← "": each element is assigned separately.Start the maximum at the first element, Max ← Scores[1], not at 0: if every value were negative, a start of 0 would give the wrong answer.
In the lab, run the 2D array program: it fills a 2 by 3 table with nested loops and totals each row. Watch the inner loop finish all its columns before the outer loop moves to the next row.
DECLARE Marks : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
FOR Row ← 1 TO 3
RowTotal ← 0
FOR Column ← 1 TO 4
RowTotal ← RowTotal + Marks[Row, Column]
NEXT Column
OUTPUT "Student ", Row, ": ", RowTotal
NEXT Row
Marks[Row, Column]. In Fig. 1, Marks[2, 3] is 10: row 2, column 3.ARRAY[1:3, 1:4] has 12 elements and the inner statement above runs 12 times.FOR Column ← 1 TO 4. What total is output for column 3?Keep the index order the same as the declaration. If the array is [1:3, 1:4], then Marks[4, 2] is out of range: there is no row 4.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
How many elements does DECLARE Data : ARRAY[0:15] OF INTEGER create?
In the lab's 2D array program the table has 2 rows and 3 columns. How many times does the statement inside the inner loop run?
Using Marks in Fig. 1, what is the value of Marks[3, 2]?
What is the upper bound of an array?
Examiners ask you to select a 1D or 2D array and justify it. Ask: is the data a single list, or does each value belong to a row and a column? And is it all the same data type?
| Data | Choice | Declaration |
|---|---|---|
| Rainfall for each day of one year | 1D: one list of 365 values | ARRAY[1:365] OF REAL |
| Rainfall for each day of each of 52 weeks | 2D: week (row) and day (column) | ARRAY[1:52, 1:7] OF REAL |
| Which seats in a 10-row, 20-seat cinema are booked | 2D of BOOLEAN | ARRAY[1:10, 1:20] OF BOOLEAN |
| Name, age and fee for 30 members | 1D array of records (mixed types) | ARRAY[1:30] OF MemberRecord |
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR.Board[2, 2] ← 'O'Do not mix data types in one array. "A 2D array with a column of names and a column of marks" would need every element to be STRING, so the marks could not be used in arithmetic without conversion.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
Which declaration is correct for an 8 by 8 board where each square holds one character?
Values holds 4, 9, 2, 7, 5 in elements 1 to 5. After Total ← 0 and FOR i ← 2 TO 5 STEP 2 / Total ← Total + Values[i] / NEXT i, what is Total?
Which data is best stored in a 2D array?
Nums is declared as ARRAY[1:10] OF INTEGER. A loop runs FOR i ← 0 TO 10 and outputs Nums[i]. What happens?
This skill is now recorded on your profile. Keep the chain going.