Arrays

Arrays - Intermediate

Step beyond the basics with searching, finding max/min, counting and totalling, reversing and filtering arrays using CIE pseudocode patterns.

20.1 Searching in Arrays

  • Linear search checks each element one by one until the value is found or the end of the array is reached.
  • Use a Found flag (BOOLEAN) and a position variable to track progress.
  • The WHILE loop continues while I <= N AND NOT Found.
  • When a match is found, set Found <- TRUE — the loop exits and the position is reported.

Example: search for a name in a 5-element array

DECLARE Names : ARRAY[1:5] OF STRING
DECLARE SearchName : STRING
DECLARE I : INTEGER
DECLARE Found : BOOLEAN
Found <- FALSE
I <- 1
OUTPUT "Enter name to search: "
INPUT SearchName
WHILE I <= 5 AND NOT Found DO
  IF Names[I] = SearchName THEN
    Found <- TRUE
  ELSE
    I <- I + 1
  ENDIF
ENDWHILE
IF Found THEN
  OUTPUT "Found at position ", I
ELSE
  OUTPUT "Not found"
ENDIF

Key idea: The AND in the WHILE condition lets the loop exit for EITHER reason — found the value, OR ran out of elements. Always check Found after the loop to decide which message to print.

20.2 Finding Maximum and Minimum

  • Initialise Max or Min to the first element (never to 0).
  • Loop through the remaining elements starting from index 2.
  • For Max: update whenever Numbers[I] > Max.
  • For Min: update whenever Numbers[I] < Min.

Example: find the highest score in a 5-element array

DECLARE Scores : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Highest : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter score: "
  INPUT Scores[I]
NEXT I
Highest <- Scores[1]
FOR I <- 2 TO 5
  IF Scores[I] > Highest THEN
    Highest <- Scores[I]
  ENDIF
NEXT I
OUTPUT "Highest score: ", Highest

Common mistake: Initialising Max to 0. If every value in the array is negative, Max stays 0 and the answer is wrong. Always start Max at Numbers[1].

20.3 Counting and Totalling Array Elements

  • Count — increment a counter when an element meets a condition (e.g. count passes).
  • Total — add every matching element to an accumulator.
  • Average — divide Total / Count (NOT Total / N).
  • Always initialise Count <- 0 and Total <- 0 before the loop.

Example: count and average of positive numbers

DECLARE Numbers : ARRAY[1:10] OF INTEGER
DECLARE I : INTEGER
DECLARE Count : INTEGER
DECLARE Total : INTEGER
DECLARE Average : REAL
Count <- 0
Total <- 0
FOR I <- 1 TO 10
  OUTPUT "Enter number: "
  INPUT Numbers[I]
  IF Numbers[I] > 0 THEN
    Count <- Count + 1
    Total <- Total + Numbers[I]
  ENDIF
NEXT I
IF Count > 0 THEN
  Average <- Total / Count
ENDIF
OUTPUT "Count: ", Count
OUTPUT "Average: ", Average

Exam tip: Guard the average with IF Count > 0 to avoid division by zero. The examiners love to test whether you remember this check.

20.4 Reversing an Array

  • Swap elements from both ends moving toward the centre.
  • Use two indices: one from the start (I), one from the end ((N + 1) - I or N + 1 - I).
  • A reversal needs N DIV 2 swaps — the middle element (if any) stays put.
  • A Temp variable is required to swap two values safely without losing either.

Example: reverse an array of 5 elements

DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Temp : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter number: "
  INPUT Numbers[I]
NEXT I
FOR I <- 1 TO 2
  Temp <- Numbers[I]
  Numbers[I] <- Numbers[6 - I]
  Numbers[6 - I] <- Temp
NEXT I
FOR I <- 1 TO 5
  OUTPUT Numbers[I]
NEXT I

Key idea: The expression 6 - I gives the mirror index. When I = 1, you swap with index 5; when I = 2, you swap with index 4. Index 3 is the middle and never needs swapping.

20.5 Filtering and Copying Arrays

  • Copy elements meeting a condition to a new array.
  • Track how many elements were copied with a separate counter (e.g. EvenCount).
  • Use MOD to test even/odd: Numbers[I] MOD 2 = 0.
  • Declare the new array with the same maximum size as the source — it may need every slot.

Example: copy all even numbers to a new array

DECLARE Numbers : ARRAY[1:10] OF INTEGER
DECLARE Evens : ARRAY[1:10] OF INTEGER
DECLARE I : INTEGER
DECLARE EvenCount : INTEGER
EvenCount <- 0
FOR I <- 1 TO 10
  OUTPUT "Enter number: "
  INPUT Numbers[I]
  IF Numbers[I] MOD 2 = 0 THEN
    EvenCount <- EvenCount + 1
    Evens[EvenCount] <- Numbers[I]
  ENDIF
NEXT I
OUTPUT "Even numbers: "
FOR I <- 1 TO EvenCount
  OUTPUT Evens[I]
NEXT I

Common mistake: Using Evens[I] instead of Evens[EvenCount]. Because I jumps in steps of 1 but only some elements match, this would leave gaps in the Evens array. Always use the separate counter.

20.6 Key Points Summary

Linear search checks each element sequentially — no sorting required.

Initialise Max/Min to the first element, NOT to 0.

Count and Total patterns work inside a single FOR loop.

Reversing uses a swap with two indices moving toward the centre.

Filtering copies matching elements into a new array.

Always use a separate counter for the filtered array.

Average = Total / Count (guard against Count = 0).

A reversal needs N DIV 2 swaps.

Exam tip: Whenever an exam question says “find the largest”, “count how many” or “copy all… that”, reach for a FOR loop with an IF inside. These three patterns cover most intermediate array tasks.

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

Question 1Multiple Choice

When does a linear search loop stop?

Question 2True / False

Linear search can only find values in arrays that have been sorted first.

Question 3Multiple Choice

DECLARE Names : ARRAY[1:5] OF STRING
DECLARE SearchName : STRING
DECLARE I : INTEGER
DECLARE Found : BOOLEAN
Names[1] <- "Ali"
Names[2] <- "Bo"
Names[3] <- "Cleo"
Names[4] <- "Dan"
Names[5] <- "Eve"
Found <- FALSE
I <- 1
INPUT SearchName  // user types Zara
WHILE I <= 5 AND NOT Found DO
  IF Names[I] = SearchName THEN
    Found <- TRUE
  ELSE
    I <- I + 1
  ENDIF
ENDWHILE
IF Found THEN
  OUTPUT "Found at position ", I
ELSE
  OUTPUT "Not found"
ENDIF

Question 4Multiple Choice

You must find the largest value in an array that may contain NEGATIVE numbers. Which initial value for Max is safest?

Question 5True / False

To find the minimum value, initialise Min to Numbers[1] and update it whenever a smaller element is found.

Question 6Multiple Choice

DECLARE Numbers : ARRAY[1:4] OF INTEGER
DECLARE I : INTEGER
DECLARE Max : INTEGER
Numbers[1] <- 3
Numbers[2] <- 8
Numbers[3] <- 1
Numbers[4] <- 5
Max <- Numbers[1]
FOR I <- 2 TO 4
  IF Numbers[I] > Max THEN
    Max <- Numbers[I]
  ENDIF
NEXT I
OUTPUT Max

Question 7Multiple Choice

Which snippet counts how many elements of a 10-element array are positive?

Question 8True / False

When averaging only the elements greater than 10, you should divide Total by N (the total array size).

Question 9Multiple Choice

DECLARE Numbers : ARRAY[1:3] OF INTEGER
DECLARE I : INTEGER
DECLARE Temp : INTEGER
Numbers[1] <- 1
Numbers[2] <- 2
Numbers[3] <- 3
FOR I <- 1 TO 1
  Temp <- Numbers[I]
  Numbers[I] <- Numbers[4 - I]
  Numbers[4 - I] <- Temp
NEXT I
FOR I <- 1 TO 3
  OUTPUT Numbers[I]
NEXT I

Question 10Multiple Choice

Which IF condition correctly selects even numbers when filtering an array into a new array called Evens?

Question 11True / False

Reversing an array of N elements in place requires N DIV 2 swaps.

Question 12Multiple Choice

Which of the following is NOT a typical intermediate array operation?

Answer all 12 questions to enable submission.