Storing 30 test marks in 30 separate variables would be painful. An array keeps them all under one identifier, numbered by an index, so a single loop can input, total or search the lot.
An array is a data structure that holds a fixed number of elements of the same data type under one identifier. Each element is reached by its index, written in square brackets: Scores[3]. Declare it with its lower and upper bounds: DECLARE Scores : ARRAY[1:5] OF INTEGER. The first index can be 0 or 1 (exam pseudocode generally uses 1). Because the index can be a variable, a FOR loop can visit every element in turn.
The declaration gives the identifier, the range of indexes and the data type of every element.
DECLARE Scores : ARRAY[1:5] OF INTEGER Scores[1] ← 12 Scores[2] ← 7 Scores[3] ← 15 Scores[4] ← 9 Scores[5] ← 11 OUTPUT Scores[3]
This outputs 15, the value in element 3.
The same idea in Python (for comparison only: the exam uses pseudocode)
scores = [12, 7, 15, 9, 11] # a Python list; the first index is 0 print(scores[2]) # 15, the third element
Using an index outside the bounds, such as Scores[6] or Scores[0] for ARRAY[1:5], is an error: that element does not exist.
In the lab, choose Maximum and minimum. Six scores are input into an array by one loop, and a second loop compares each element with the largest and smallest so far.
DECLARE Temps : ARRAY[1:7] OF REAL
DECLARE Day : INTEGER
DECLARE Total : REAL
FOR Day ← 1 TO 7
INPUT Temps[Day]
NEXT Day
Total ← 0
FOR Day ← 1 TO 7
Total ← Total + Temps[Day]
NEXT Day
OUTPUT "Average: ", Total / 7The same idea in Python (for comparison only: the exam uses pseudocode)
temps = []
for day in range(7):
temps.append(float(input()))
print("Average:", sum(temps) / 7)
Highest ← Scores[1], then FOR Index ← 2 TO 5: IF Scores[Index] > Highest THEN Highest ← Scores[Index]; after the loop, OUTPUT Highest.Start Highest at the first element, not at 0: if every value were negative, a starting value of 0 would be wrongly output as the largest.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which statement about an array is correct?
How many elements are in DECLARE Marks : ARRAY[1:30] OF INTEGER?
In the lab's Maximum and minimum program with inputs 34, 71, 12, 58, 90, 45, what is output?
In a Python list, what is the index of the first element?
The index can be any expression that gives an integer, so a program can search an array, or move values between neighbouring elements, such as Names[Count + 1].
DECLARE Found : BOOLEAN
DECLARE Index : INTEGER
DECLARE Target : INTEGER
INPUT Target
Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= 5 DO
IF Scores[Index] = Target
THEN
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found = TRUE
THEN
OUTPUT "Found at position ", Index
ELSE
OUTPUT "Not found"
ENDIF
Names[Count + 1] refer to?Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Which is a correct declaration of an array to hold 10 names?
Data holds 4, 8, 2, 6 at indexes 1 to 4. What is Data[2] + Data[4]?
Why is a FOR loop often used with an array?
FOR i ← 1 TO 4, Nums[i] ← i * 3, NEXT i. What is stored in Nums[3]?
This skill is now recorded on your profile. Keep the chain going.