Algorithm Design and Standard Methods: Question 9
Syllabus 7.4
An online store keeps the prices, in dollars, of 5 items in a one-dimensional (1D) array called Prices. The pseudocode algorithm below is run on this array.
DECLARE Prices : ARRAY[1:5] OF REAL
DECLARE Index : INTEGER
DECLARE Cheapest : REAL
Cheapest ← Prices[1]
FOR Index ← 2 TO 5
IF Prices[Index] < Cheapest THEN
Cheapest ← Prices[Index]
ENDIF
NEXT Index
OUTPUT Cheapest
What does this algorithm calculate?
Show worked solution Hide worked solution
Worked solution
Step 1: Identify what Cheapest starts as and how it changes
Cheapest is initialised to Prices[1], so it begins as one of the actual prices in the array,
not zero and not a running total. Inside the loop, Cheapest is only ever replaced entirely
by Prices[Index]. It is never added to and never divided.
Step 2: Trace the comparison being made
For each remaining item (Index from 2 to 5), the algorithm checks
IF Prices[Index] < Cheapest THEN. Only when a price is smaller than the current value of
Cheapest does the replacement Cheapest ← Prices[Index] happen. This means Cheapest can only
ever get smaller (or stay the same) as the loop runs. It never increases.
Step 3: Rule out the other standard methods
- Totalling (option A) would need
Cheapest ← Cheapest + Prices[Index], adding every price together; here, values only replaceCheapest, they are never added to it. - Counting (option B) would need a separate counter variable incremented by 1 each time a
condition is true;
Cheapestalways holds a full price value, not a count of comparisons. - Averaging (option D) would need a running total divided by 5 at the end; there is no division anywhere in this algorithm.
Step 4: Confirm what the algorithm actually finds
Because Cheapest starts at one price and is only ever overwritten by a smaller price found
later in the array, after the loop finishes Cheapest holds the smallest of all 5 prices, the
standard “finding a minimum” method, applied here to find the cheapest item.
Final answer
- The algorithm finds the cheapest (lowest) price among the 5 items, option C.