Write pseudocode to process array data

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Topic 10.2 Arrays

10.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

OperationPseudocode 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

  1. Declare the array with correct bounds and type.
  2. Initialise if a specific start value is required.
  3. Use a loop that respects the lower and upper bounds.
  4. Apply the appropriate algorithm (search, sort, insert, delete).
  5. Test edge cases: empty array, single element, full capacity.

9. Practice Questions

  1. Write pseudocode to reverse the elements of an integer array a[1..n] in place.
  2. Given an array temps[1..30] containing daily temperatures, write pseudocode to find the number of days with temperature above the monthly average.
  3. 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.