Objectives: Students should be able to —
- 1 Define the term array and explain why arrays are used in programming.
- 2 Describe the structure of a one-dimensional (1D) array — elements, index and size.
- 3 Declare a 1D array with a name, data type and size in pseudocode.
- 4 Initialise array elements with values at the point of declaration.
- 5 Populate an array by assignment and by user INPUT in a loop.
- 6 Access individual array elements using their index.
- 7 Explain zero-based indexing and state the valid index range of an array of size N.
- 8 Traverse an array using a FOR loop to read or print every element.
- 9 Use an array to calculate the total (sum) and the count of values meeting a condition.
- 10 Find the maximum and minimum values stored in an array.
- 11 Search an array for a specific value (linear search).
- 12 Use parallel arrays to store and process related data items.
Arrays: Concept and Structure
An array is a named collection of variables — all of the same data type — that are stored together in memory under one identifier (name).
Each individual value is called an element, and each element is identified by its position number called the index (or subscript).
Why arrays are used:
- They group related data items together (e.g. test scores of 30 students).
- They avoid declaring many separate variables like Score1, Score2, Score3 … Score30.
- They allow a loop (usually a FOR loop) to process all elements with just a few lines of code.
- They make code shorter, clearer and easier to maintain.
A 1D array is a single row of elements, each holding a value of the same data type. Each element is accessed by its index — the position it occupies in the row.
The array Scores has 5 elements, indexed 0 to 4 (zero-based):
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 56 | 78 | 45 | 89 | 67 |
Size of the array = 5 (the total number of elements).
Valid index range = 0 to 4 (i.e. 0 to Size − 1).
The index (also called the subscript) is the position number used to access an individual element of an array, written in square brackets after the array name — e.g. Scores[3].
IGCSE pseudocode uses zero-based indexing — the first element is at index 0.
For an array of size N (containing N elements):
- Valid indexes: 0 to N − 1.
- The first element is at index 0.
- The last element is at index N − 1.
Declaring and Initialising Arrays
Syntax:
DECLARE <identifier> : ARRAY[<lower>:<upper>] OF <dataType> - <identifier> = name of the array.
- <lower>:<upper> = the lowest and highest valid index.
- <dataType> = INTEGER, REAL, STRING, BOOLEAN or CHAR.
Example 1 — Integer array of 5 scores (index 1 to 5):
DECLARE Scores : ARRAY[1:5] OF INTEGER Example 2 — String array of 10 names (index 0 to 9):
DECLARE Names : ARRAY[0:9] OF STRING Initialising means giving every element of an array a starting value before the array is used by the rest of the program. This prevents the elements from holding unpredictable "garbage" values.
Method 1 — Assign each element individually:
Scores[1] <- 56 Scores[2] <- 78 Scores[3] <- 45 Scores[4] <- 89 Scores[5] <- 67 Method 2 — Initialising in the declaration:
DECLARE Scores : ARRAY[1:5] OF INTEGER <- 56, 78, 45, 89, 67 Populating and Accessing Arrays
(a) Populating by assignment — the programmer writes fixed values into the code:
DECLARE Scores : ARRAY[1:5] OF INTEGER Scores[1] <- 56 Scores[2] <- 78 Scores[3] <- 45 Scores[4] <- 89 Scores[5] <- 67 Used when the values are known in advance (e.g. a lookup table of days in each month).
(b) Populating by user INPUT inside a FOR loop — values are typed by the user at run-time:
DECLARE Scores : ARRAY[1:5] OF INTEGER DECLARE Index : INTEGER FOR Index <- 1 TO 5 OUTPUT "Enter score: " INPUT Scores[Index] NEXT Index Used when the values are not known until the program runs (e.g. entering each student's test score).
(a) Scores[0] — the first element:
56
(b) Scores[4] — the fifth (last) element:
67
(c) Scores[2] — the third element:
45
(d) Accessing Scores[5]:
Traversing Arrays
Traversing means visiting each element of the array in turn, usually from the first index to the last. A FOR loop is the natural construct because the number of elements is known in advance.
DECLARE Numbers : ARRAY[0:9] OF INTEGER DECLARE Index : INTEGER // ... assume the array has been populated ... FOR Index <- 0 TO 9 OUTPUT Numbers[Index] NEXT Index The loop variable Index takes the values 0, 1, 2, …, 9 in turn, so each element is accessed and printed exactly once.
DECLARE Numbers : ARRAY[0:9] OF INTEGER DECLARE Index : INTEGER // Step 1 — populate the array with user input FOR Index <- 0 TO 9 OUTPUT "Enter an integer: " INPUT Numbers[Index] NEXT Index // Step 2 — traverse and print each value doubled FOR Index <- 0 TO 9 OUTPUT Numbers[Index] * 2 NEXT Index The first loop writes values into the array; the second loop reads them back, doubles each one, and prints it.
Totalling, Counting and Searching
Method: Use an accumulator variable Total, set it to 0 before the loop, and add each element to it inside the loop.
DECLARE Numbers : ARRAY[0:9] OF INTEGER DECLARE Index : INTEGER DECLARE Total : INTEGER Total <- 0 // ... assume array already populated ... FOR Index <- 0 TO 9 Total <- Total + Numbers[Index] NEXT Index OUTPUT "The total is: ", Total Method: Use a counter variable Count, set it to 0 before the loop, and increment it by 1 each time the condition is true.
DECLARE Numbers : ARRAY[0:9] OF INTEGER DECLARE Index : INTEGER DECLARE Count : INTEGER Count <- 0 // ... assume array already populated ... FOR Index <- 0 TO 9 IF Numbers[Index] > 50 THEN Count <- Count + 1 ENDIF NEXT Index OUTPUT "Number of values greater than 50: ", Count A linear search examines each element in turn, from the first to the last, until it finds the target value or reaches the end.
DECLARE Numbers : ARRAY[0:9] OF INTEGER DECLARE Index : INTEGER DECLARE SearchValue : INTEGER DECLARE Found : BOOLEAN // ... assume array already populated ... OUTPUT "Enter the value to search for: " INPUT SearchValue Found <- FALSE Index <- 0 WHILE Found = FALSE AND Index <= 9 DO IF Numbers[Index] = SearchValue THEN Found <- TRUE ELSE Index <- Index + 1 ENDIF ENDWHILE IF Found = TRUE THEN OUTPUT "Value found at index ", Index ELSE OUTPUT "Value not found" ENDIF The loop stops as soon as the value is found, so it does not needlessly examine the rest of the array.
Finding Maximum and Minimum
Method: Initialise both Max and Min with the first element of the array, then loop through the rest of the elements, updating Max if a larger value is found and Min if a smaller value is found.
DECLARE Numbers : ARRAY[0:9] OF INTEGER DECLARE Index : INTEGER DECLARE Max : INTEGER DECLARE Min : INTEGER // ... assume array already populated ... Max <- Numbers[0] Min <- Numbers[0] FOR Index <- 1 TO 9 IF Numbers[Index] > Max THEN Max <- Numbers[Index] ENDIF IF Numbers[Index] < Min THEN Min <- Numbers[Index] ENDIF NEXT Index OUTPUT "Maximum value: ", Max OUTPUT "Minimum value: ", Min Parallel Arrays
Parallel arrays are two or more arrays of the same size whose elements at the same index are related — they describe different fields of the same record.
Example — storing the names and ages of 4 students:
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Names | "Ali" | "Sara" | "Tom" | "Maya" |
| Ages | 16 | 17 | 15 | 16 |
Here Names[1] and Ages[1] both refer to the same person — Sara, age 17. Because they share an index, a single loop can process both arrays at once.
DECLARE Names : ARRAY[1:5] OF STRING DECLARE Ages : ARRAY[1:5] OF INTEGER DECLARE Index : INTEGER DECLARE MaxAge : INTEGER DECLARE MaxIndex : INTEGER // Step 1 — populate both parallel arrays FOR Index <- 1 TO 5 OUTPUT "Enter student name: " INPUT Names[Index] OUTPUT "Enter student age: " INPUT Ages[Index] NEXT Index // Step 2 — find the index of the maximum age MaxAge <- Ages[1] MaxIndex <- 1 FOR Index <- 2 TO 5 IF Ages[Index] > MaxAge THEN MaxAge <- Ages[Index] MaxIndex <- Index ENDIF NEXT Index // Step 3 — use the index in the parallel array to print the name OUTPUT "The oldest student is: ", Names[MaxIndex] OUTPUT "Age: ", Ages[MaxIndex] Because both arrays share the same index, finding MaxIndex in the Ages array automatically gives the corresponding name in the Names array.
(a) Declare the parallel arrays:
DECLARE Names : ARRAY[1:5] OF STRING DECLARE Scores : ARRAY[1:5] OF INTEGER (b) Input 5 names and scores:
DECLARE Index : INTEGER FOR Index <- 1 TO 5 OUTPUT "Enter student name: " INPUT Names[Index] OUTPUT "Enter test score: " INPUT Scores[Index] NEXT Index (c) Calculate and output the average score:
DECLARE Total : INTEGER DECLARE Average : REAL Total <- 0 FOR Index <- 1 TO 5 Total <- Total + Scores[Index] NEXT Index Average <- Total / 5 OUTPUT "Average score: ", Average (d) Print names of students scoring above the average:
OUTPUT "Students above the average:" FOR Index <- 1 TO 5 IF Scores[Index] > Average THEN OUTPUT Names[Index], " — ", Scores[Index] ENDIF NEXT Index Revision: Statements and Key Computing Terms
| Statement | Key Term |
|---|---|
| A named collection of variables of the same data type stored together under one identifier. | Array |
| An array with a single row of elements, accessed by one index. | One-dimensional (1D) array |
| An individual value stored in an array. | Element |
| The position number used to access an individual element of an array. | Index (subscript) |
| Number of elements an array can hold. | Size / length |
| A numbering system where the first element of an array is at index 0. | Zero-based indexing |
| Giving every element of an array a starting value before the array is used. | Initialising |
| Filling an array with values, either by assignment or by user INPUT. | Populating |
| Visiting each element of an array in turn, usually with a FOR loop. | Traversing |
| A variable used to add up (sum) values inside a loop; it must be set to 0 before the loop. | Accumulator (total) |
| A variable incremented by 1 each time a condition is true; used to count matching values. | Counter |
| The largest value stored in an array. | Maximum (max) |
| The smallest value stored in an array. | Minimum (min) |
| A search that examines each element in turn until the target is found or the end is reached. | Linear search |
| Two or more arrays of the same size whose elements at the same index are related. | Parallel arrays |
| An error that occurs when the program tries to access an array index outside the valid range. | Index out of bounds error |
| The pseudocode keyword used to create an array in memory. | DECLARE … ARRAY[...] OF … |