Common Algorithms

Highest & Lowest

Finding the maximum (highest) and minimum (lowest) values in a set of data. A comparison variable tracks the extreme seen so far — and it must be initialised to the FIRST element, never to 0.

16.1 What is Highest & Lowest?

Highest & Lowest is the algorithm of finding the maximum (highest) and minimum (lowest) values in a set of data. A comparison variable tracks the extreme value seen so far while looping through the data.

  • Comparison variable — INTEGER (or REAL), initialised to the FIRST element.
  • Highest — updated with IF Value > Highest THEN Highest <- Value.
  • Lowest — updated with IF Value < Lowest THEN Lowest <- Value.
  • Common uses — highest score, lowest temperature, maximum price, minimum time.

Key idea: totalling adds every value to an accumulator; counting matches a criterion. Highest & Lowest is different — it keeps only the most extreme value seen so far, replacing it whenever a new extreme appears.

Common mistake: initialising the comparison variable to 0. If all values are negative, 0 would be wrongly reported as the highest; if all are positive, 0 would wrongly be the lowest. Always seed with the FIRST element.

16.2 Finding the Highest Value

The highest-value pattern has four steps: initialise Highest to the first element, loop through the remaining elements, compare each element to Highest using > and update if larger, and output the final Highest after the loop.

// Find the highest of 5 numbers entered by the user
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Highest : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter number: "
  INPUT Numbers[I]
NEXT I
Highest <- Numbers[1]
FOR I <- 2 TO 5
  IF Numbers[I] > Highest THEN
    Highest <- Numbers[I]
  ENDIF
NEXT I
OUTPUT "Highest: ", Highest
  • Seed before loopHighest <- Numbers[1], never 0.
  • Loop from 2 — start at FOR I <- 2 TO N because index 1 is already in Highest.
  • Greater-than comparison — update only when Numbers[I] > Highest.
  • OUTPUT after loop — print the final highest value, not the running maximum.

Exam tip: if a question says "find the largest", "find the maximum", or "find the highest", that is this algorithm. Look for the keyword "largest" / "maximum" / "highest".

16.3 Finding the Lowest Value

The lowest-value pattern mirrors the highest pattern — only the comparison operator changes. Initialise Lowest to the first element, loop through the rest, compare each using <, and update if smaller.

// Find the lowest of 5 numbers entered by the user
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I : INTEGER
DECLARE Lowest : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter number: "
  INPUT Numbers[I]
NEXT I
Lowest <- Numbers[1]
FOR I <- 2 TO 5
  IF Numbers[I] < Lowest THEN
    Lowest <- Numbers[I]
  ENDIF
NEXT I
OUTPUT "Lowest: ", Lowest
  • Seed before loopLowest <- Numbers[1], never 0.
  • Less-than comparison — update only when Numbers[I] < Lowest.
  • OUTPUT after loop — print the final lowest value after the loop ends.
  • Same skeleton as highest — only the operator (< vs >) changes.

Watch out: initialising Lowest to 0 when all values are positive. Lowest stays 0 forever (no positive value is smaller than 0), giving the wrong answer. Always seed with the first real element.

Exam tip: "Find the cheapest", "find the coldest", "find the minimum" — these all signal the lowest-value algorithm. Swap > for < and you are done.

16.4 Finding Both Highest and Lowest

You can find both extremes in a single pass through the data. Use two variables (Highest and Lowest), seed both to the first element, and inside one loop run two independent IF statements — one with > for Highest, one with< for Lowest.

// Find both highest and lowest in one loop
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE I, Highest, Lowest : INTEGER
FOR I <- 1 TO 5
  OUTPUT "Enter number: "
  INPUT Numbers[I]
NEXT I
Highest <- Numbers[1]
Lowest  <- Numbers[1]
FOR I <- 2 TO 5
  IF Numbers[I] > Highest THEN
    Highest <- Numbers[I]
  ENDIF
  IF Numbers[I] < Lowest THEN
    Lowest <- Numbers[I]
  ENDIF
NEXT I
OUTPUT "Highest: ", Highest
OUTPUT "Lowest: ", Lowest
  • Seed bothHighest <- Numbers[1] and Lowest <- Numbers[1].
  • Two IFs per iteration — one for > (Highest), one for < (Lowest).
  • Independent checks — both IFs run for every element; they are NOT else-branches.
  • Single pass — visits each element once (more efficient than two loops).

Why one loop? A single loop that updates both variables visits each element once (N comparisons × 2 IFs). Two separate loops would visit each element twice (2N comparisons). The result is identical — the single pass is just more efficient.

16.5 Practical Examples

Four classic highest & lowest problems: hottest/coldest week, cheapest item, and a sentinel-controlled version that reads values from the user.

Example 1 — Hottest and Coldest Day of the Week

DECLARE Temp : ARRAY[1:7] OF INTEGER
DECLARE I, Highest, Lowest : INTEGER
FOR I <- 1 TO 7
  OUTPUT "Enter temperature: "
  INPUT Temp[I]
NEXT I
Highest <- Temp[1]
Lowest  <- Temp[1]
FOR I <- 2 TO 7
  IF Temp[I] > Highest THEN Highest <- Temp[I] ENDIF
  IF Temp[I] < Lowest  THEN Lowest  <- Temp[I] ENDIF
NEXT I
OUTPUT "Hottest: ", Highest
OUTPUT "Coldest: ", Lowest

Reads 7 temperatures, then finds both extremes in one pass through the array. Both Highest and Lowest are seeded with Temp[1] before the loop.

Example 2 — Cheapest Item in a Shopping List

DECLARE Price : ARRAY[1:10] OF REAL
DECLARE I : INTEGER
DECLARE Cheapest : REAL
FOR I <- 1 TO 10
  OUTPUT "Enter price: "
  INPUT Price[I]
NEXT I
Cheapest <- Price[1]
FOR I <- 2 TO 10
  IF Price[I] < Cheapest THEN
    Cheapest <- Price[I]
  ENDIF
NEXT I
OUTPUT "Cheapest item costs: ", Cheapest

Uses REAL for prices (decimals). The lowest-value pattern with a < comparison finds the minimum price in the array.

Example 3 — Sentinel-Controlled Highest & Lowest

DECLARE V, Highest, Lowest : INTEGER
INPUT V
Highest <- V
Lowest  <- V
WHILE V <> -1 DO
  IF V > Highest THEN Highest <- V ENDIF
  IF V < Lowest  THEN Lowest  <- V ENDIF
  INPUT V
ENDWHILE
OUTPUT "Highest: ", Highest
OUTPUT "Lowest: ", Lowest

Reads values until -1 is entered. The first value seeds both Highest and Lowest before the loop; subsequent values are compared inside the WHILE body. The sentinel -1 is never compared.

Watch out: forgetting the inner INPUT in the sentinel loop creates an infinite loop — the same value is compared forever. Always read the next value inside the loop body before ENDWHILE.

16.6 Key Points Summary

A quick recap of everything covered on highest & lowest:

  • PurposeFind the maximum (highest) and/or minimum (lowest) value in data.
  • Initialise to FIRST elementHighest/Lowest <- Numbers[1] BEFORE the loop, never 0.
  • Use > for highestIF Value > Highest THEN Highest <- Value.
  • Use < for lowestIF Value < Lowest THEN Lowest <- Value.
  • Both in one loopSeed both to first element; two independent IFs inside one loop.
  • Loop from index 2FOR I <- 2 TO N — index 1 is already in Highest/Lowest.
  • OUTPUT after loopPrint the final value AFTER the loop ends.
  • Sentinel excludedThe stop value (-1 or 0) is never compared to Highest/Lowest.
  • Works on any sequenceArrays (FOR), user input (WHILE + sentinel) — pattern is the same.
  • Common mistakeInitialising to 0 instead of first element — fails for all-negative or all-positive data.

Exam tip: look for the keywords "largest", "maximum", "highest", "smallest", "minimum", "lowest", "cheapest", "hottest", "coldest". They all signal the highest & lowest algorithm — seed with the first element and compare with > or <.

Question Bank

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

0/12 answered

Question 1Multiple Choice

What does the highest & lowest algorithm compute?

Question 2True / False

The comparison variable for highest or lowest must be initialised to the FIRST element of the data, not to 0.

Question 3Multiple Choice

What is the final value of Highest?

DECLARE N : ARRAY[1:5] OF INTEGER
DECLARE I, Highest : INTEGER
N[1] <- 4; N[2] <- 7; N[3] <- 2; N[4] <- 9; N[5] <- 1
Highest <- N[1]
FOR I <- 2 TO 5
  IF N[I] > Highest THEN
    Highest <- N[I]
  ENDIF
NEXT I
OUTPUT Highest

Question 4True / False

To find the lowest value, you use the comparison IF N[I] > Lowest THEN update Lowest.

Question 5Multiple Choice

Which statement correctly updates Highest when a larger value is found?

Question 6True / False

Finding both the highest and lowest values requires two separate loops through the data.

Question 7Multiple Choice

What is the final value of Lowest?

DECLARE N : ARRAY[1:5] OF INTEGER
DECLARE I, Lowest : INTEGER
N[1] <- 6; N[2] <- 3; N[3] <- 8; N[4] <- 1; N[5] <- 5
Lowest <- N[1]
FOR I <- 2 TO 5
  IF N[I] < Lowest THEN
    Lowest <- N[I]
  ENDIF
NEXT I
OUTPUT Lowest

Question 8True / False

When using a sentinel-controlled loop to find the highest, the sentinel value itself is compared against Highest.

Question 9Multiple Choice

What is wrong with this code that aims to find the highest of 5 numbers?

DECLARE N : ARRAY[1:5] OF INTEGER
DECLARE I, Highest : INTEGER
Highest <- 0
FOR I <- 1 TO 5
  IF N[I] > Highest THEN
    Highest <- N[I]
  ENDIF
NEXT I
OUTPUT Highest

Question 10True / False

In a single-pass loop for both extremes, both Highest and Lowest are initialised to the first element before the loop begins.

Question 11Multiple Choice

Which problem BEST fits the highest/lowest algorithm?

Question 12True / False

The highest/lowest algorithm can be applied to user input using a sentinel loop, not just to arrays.

Answer all 12 questions to enable submission.