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"
ENDIFKey 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.
Searching in Arrays
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: ", HighestCommon 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].
Finding Maximum and Minimum
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 <- 0andTotal <- 0before 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: ", AverageExam tip: Guard the average with IF Count > 0 to avoid division by zero. The examiners love to test whether you remember this check.
Counting and Totalling Array Elements
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 IKey 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.
Reversing an Array
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
MODto 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 ICommon 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.
Filtering and Copying Arrays
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.