Computer ScienceExam code: 0984

Arrays

Programming

A FOR loop is the natural way to visit every element of an array in turn. The counter variable becomes the index.

DECLARE Index : INTEGER
FOR Index ← 1 TO 5
   OUTPUT Scores[Index]
NEXT Index

This prints every element of Scores in turn:

12
100
5
2
8

Common array-processing patterns

Sum every of a numeric array:

DECLARE Total : INTEGER
Total ← 0
FOR Index ← 1 TO 5
   Total ← Total + Scores[Index]
NEXT Index
OUTPUT "Total: ", Total

Find the largest element (assumes at least one element exists):

DECLARE Highest : INTEGER
Highest ← Scores[1]
FOR Index ← 2 TO 5
   IF Scores[Index] > Highest
     THEN
        Highest ← Scores[Index]
   ENDIF
NEXT Index
OUTPUT "Highest: ", Highest

Common exam question

Finding the highest or lowest value in an array

Question: Write an algorithm with a single loop that finds the lowest or highest value in an array and outputs it once, sometimes with the name stored at the same index in another array (4 marks on its own).

Asked in 3 of the 17 papers, once as a four-mark algorithm and twice inside the 15-mark program. The four marks come from five points: a variable started no lower than anything the array can hold (or at its first element), a loop over every element, an IF comparing each element with the lowest so far, replacing it when the element is smaller, and one output after the loop. Starting at 0 fails when every value is positive.

For the highest, start below any possible value. To output a name, record the index in the same IF and use it on the parallel array afterwards. Where joint winners are possible, a second loop collects every index whose value equals the maximum.

Count how many satisfy a condition (e.g. count pass marks ≥ 50):

DECLARE Passes : INTEGER
Passes ← 0
FOR Index ← 1 TO 30
   IF Marks[Index] >= 50
     THEN
        Passes ← Passes + 1
   ENDIF
NEXT Index
OUTPUT "Number of passes: ", Passes

Common exam question

Completing an algorithm that counts or totals an array

Question: Complete a given pseudocode algorithm that counts, totals or averages the values in an array, then output its results with suitable messages (3 marks for each part).

Asked in 2 of the 17 papers. Completing the code earns one mark per missing line: a counter or index set before the loop (Count ← 0, Index ← 1), the test or update inside it (IF Values[Index] = 0, Total ← Total + Values[Index]), and the line that closes the loop or works out the average after it. The output part gives a mark for each value displayed and a third for suitable messages, so label both.

When a WHILE loop stops at a 0 marking the end of the data, the index has moved one past the last value: with 6 and 8 stored before the 0, it stops at 3. So the number of values is the index minus 1, and the average divides the total by that.

Search for a value (linear search, topic 20):

DECLARE Found : BOOLEAN
Found ← FALSE
FOR Index ← 1 TO 30
   IF Marks[Index] = Target
     THEN
        Found ← TRUE
        OUTPUT "Found at position ", Index
   ENDIF
NEXT Index
IF Found = FALSE
  THEN
     OUTPUT "Not found"
ENDIF

Storing the array size in a variable

Real algorithms usually need to know the array's size at run time, not as a hard-coded number. A common idiom:

DECLARE Size : INTEGER
Size ← 30
DECLARE Marks : ARRAY[1:Size] OF INTEGER

FOR Index ← 1 TO Size
   OUTPUT Marks[Index]
NEXT Index

Now if the class size changes, only the line Size ← 30 needs to be edited.

Quick check

4 short questions on this note. See what has stuck, and where to look if something hasn't.

About 2 minutes

Build on this topic