Detailed · Deep study
1Declaring arrays
A data structure that holds a fixed number of elements, all of the same data type, under one identifier; each element is accessed using its index.
A one-dimensional (1D) array is like a list; a two-dimensional (2D) array is like a table, with an index for the row and an index for the column. The first index can be zero or one – Cambridge pseudocode usually uses 1, but Python lists start at 0.
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHAR
StudentNames[1] ← "Ali"
NoughtsAndCrosses[2, 3] ← 'X'2Why use arrays?
Without an array, storing 30 names would need 30 separate variables. An array keeps related data together, so it can be processed with a loop using a variable as the index – the same few lines of code work for 30 or 3000 items. Arrays are used for searching, sorting, totalling and finding the maximum and minimum.
See Topic 7 · Algorithm design and problem-solvingLinear search and bubble sort on arrays are in 7.1.3Using iteration with arrays
DECLARE Names : ARRAY[1:5] OF STRING
DECLARE Index : INTEGER
FOR Index ← 1 TO 5
OUTPUT "Enter name ", Index
INPUT Names[Index] // write values into the array
NEXT Index
FOR Index ← 1 TO 5
OUTPUT Names[Index] // read values from the array
NEXT IndexDECLARE 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
INPUT Marks[Row, Column]
RowTotal ← RowTotal + Marks[Row, Column]
NEXT Column
OUTPUT "Total for student ", Row, " is ", RowTotal
NEXT Row4Exam focus
Key words: array; element; index; one-dimensional; two-dimensional; row; column; nested iteration.
Quick self-check
Answer in your head first, then reveal.
DECLARE Temperatures : ARRAY[1:50] OF REAL (1 for name/size, 1 for data type).
Data can be processed with a loop using the index / less code / related data kept together (1).
90 (1).
Max ← Scores[1] (1); loop 2 to 20 (1); IF Scores[i] > Max THEN Max ← Scores[i] (1); OUTPUT Max after the loop (1).
