Cambridge A-Level Computer Science 9618 – Topic 10.2 Arrays10.2 Arrays – Writing Pseudocode to Process Array Data
1. What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is accessed by an index (or subscript).
In most programming languages the first index is 0, but in pseudocode for Cambridge A‑Level we usually start at 1 unless otherwise stated.
2. Declaring and Initialising an Array
In pseudocode the general form is:
DECLARE : ARRAY[..<upperBound>] OF <type>
Example:
DECLARE scores : ARRAY[1..10] OF INTEGER
All elements are automatically initialised to a default value (0 for numbers, "" for strings, FALSE for booleans).
3. Accessing Elements
Use the index in square brackets:
SET total ← total + scores[i]
Remember that i must be within the declared bounds, otherwise a run‑time error occurs.
4. Common Array Operations
- Initialisation (setting all elements to a known value)
- Traversal (looping through the array)
- Searching (linear or binary)
- Sorting (bubble, selection, insertion)
- Insertion and deletion (shifting elements)
5. Pseudocode Templates
| Operation | Pseudocode Template |
|---|
| Initialise all elements to 0 | FOR i ← lowerBound TO upperBound DO array[i] ← 0 END FOR
|
| Sum of all elements | SET sum ← 0 FOR i ← lowerBound TO upperBound DO sum ← sum + array[i] END FOR
|
| Find maximum value | SET max ← array[lowerBound] FOR i ← lowerBound+1 TO upperBound DO IF array[i] > max THEN SET max ← array[i] END IF END FOR
|
| Linear search for a value x | SET found ← FALSE FOR i ← lowerBound TO upperBound DO IF array[i] = x THEN SET found ← TRUE EXIT FOR END IF END FOR
|
| Insert value v at position p (shifting right) | FOR i ← upperBound DOWNTO p+1 DO array[i] ← array[i-1] END FOR SET array[p] ← v
|
6. Detailed Example – Calculating the Average Grade
Given an array marks[1..n], compute the average and display it to two decimal places.
DECLARE n, i : INTEGER
DECLARE marks : ARRAY[1..100] OF INTEGER
DECLARE total, average : REAL
READ n
FOR i ← 1 TO n DO
READ marks[i]
END FOR
SET total ← 0
FOR i ← 1 TO n DO
total ← total + marks[i]
END FOR
SET average ← total / n
OUTPUT "Average = ", average
7. Common Pitfalls
- Using an index outside the declared bounds – leads to a run‑time error.
- Confusing 0‑based and 1‑based indexing when translating between languages.
- Forgetting to update the array size after insertion or deletion.
- Not resetting accumulator variables (e.g.,
sum) before reuse.
8. Summary Checklist
- Declare the array with correct bounds and type.
- Initialise if a specific start value is required.
- Use a loop that respects the lower and upper bounds.
- Apply the appropriate algorithm (search, sort, insert, delete).
- Test edge cases: empty array, single element, full capacity.
9. Practice Questions
- Write pseudocode to reverse the elements of an integer array
a[1..n] in place. - Given an array
temps[1..30] containing daily temperatures, write pseudocode to find the number of days with temperature above the monthly average. - Explain why a binary search requires the array to be sorted and give the pseudocode for binary search on a 1‑based indexed array.
Suggested diagram: Flowchart showing the steps of a linear search through an array.