- Understand how minimum, maximum, and count apply the prescribed array-traversal algorithm
- Apply the find-minimum and find-maximum algorithms to arrays using loops and selection
- Apply the count occurrences algorithm to count how many array elements meet a condition
- I can explain why the minimum variable must be initialised to
array[0], not 0 - I can write Python code to find the minimum value in an array and trace it step by step
- I can write Python code to find the maximum value in an array and trace it step by step
- I can write Python code to count how many array elements satisfy a given condition
- I can write SQA pseudocode for all three algorithms using correct loop bounds and keywords
- I can identify and correct common errors in minimum, maximum, and count algorithms
Answer before the lesson begins. These check prior knowledge from earlier lessons — it's fine if you're unsure.
1. An array is declared as scores = [45, 78, 62, 91, 30]. What is the value of scores[4]?
2. What must be done to a variable (such as total) before it is used inside a loop to accumulate a running total?
3. The array scores = [45, 78, 62, 91, 30] is traversed and the condition scores[index] >= 60 is checked each time. How many times does the condition evaluate to True?
Key vocabulary
Array Traversal Patterns: Min, Max, Count
Where these patterns fit at N5
The SQA N5 specification names exactly three standard algorithms: input validation, a running total within a loop, and traversing a one-dimensional array. Finding a minimum, finding a maximum, and counting values are not separately named standard algorithms. They are useful application patterns built from array traversal, a variable initialised before the loop, and a comparison or update inside the loop.
All three algorithms in this lesson share the same underlying structure: set up a variable before the loop, traverse the array element by element, update the variable inside the loop based on a condition, and use the result after the loop ends.
Find minimum
The find-minimum algorithm locates the smallest value in an array. The key steps are:
- Initialise a variable called
minimumto the value of the first element of the array:minimum = array[0]. - Loop from index 1 to the last index (you have already accounted for index 0).
- Compare each element to
minimum. If the element is smaller, updateminimumto that element's value. - After the loop,
minimumholds the smallest value in the array.
temps = [18, 12, 25, 9, 21, 14]
minimum = temps[0] # initialise to first element (18)
for index in range(1, len(temps)):
if temps[index] < minimum:
minimum = temps[index]
print("Minimum:", minimum)
Trace: minimum starts at 18. index=1: 12<18 → minimum=12. index=2: 25<12? No. index=3: 9<12 → minimum=9. index=4: 21<9? No. index=5: 14<9? No. Output: Minimum: 9.
Find maximum
The find-maximum algorithm is structurally identical to find-minimum — only the comparison operator changes. Instead of checking if the current element is smaller than the running minimum, check if it is larger than the running maximum:
scores = [45, 78, 62, 91, 30, 55]
maximum = scores[0] # initialise to first element (45)
for index in range(1, len(scores)):
if scores[index] > maximum:
maximum = scores[index]
print("Maximum:", maximum)
Trace: maximum starts at 45. index=1: 78>45 → maximum=78. index=2: 62>78? No. index=3: 91>78 → maximum=91. index=4: 30>91? No. index=5: 55>91? No. Output: Maximum: 91.
The only difference from find-minimum is > instead of < in the if condition.
Count occurrences
The count algorithm counts how many elements in an array satisfy a given condition — for example, how many scores are above 50, or how many temperatures are below zero. The steps are:
- Initialise a counter variable to 0:
count = 0. - Loop from index 0 to the last index (the full array, not starting at 1).
- Check the condition for each element. If it is True, add 1 to the counter:
count = count + 1. - After the loop,
countholds the number of elements that met the condition.
ages = [16, 14, 17, 15, 16, 18, 16, 13]
count = 0
for index in range(len(ages)):
if ages[index] == 16:
count = count + 1
print("Count:", count)
Trace: 16==16 → count=1. 14==16? No. 17==16? No. 15==16? No. 16==16 → count=2. 18==16? No. 16==16 → count=3. 13==16? No. Output: Count: 3.
The condition can use any comparison operator: ==, >, <, >=, <=, or !=. For example, counting all values greater than 50 uses if ages[index] > 50.
SQA pseudocode for the three algorithms
In the exam, questions may ask you to write pseudocode rather than Python. The structure maps across directly, with SET … TO replacing assignment, IF … THEN … END IF replacing if, and FOR index FROM … TO … DO … END FOR replacing for … in range(…).
| Algorithm | Loop start | Initialise to | Condition |
|---|---|---|---|
| Find minimum | index 1 | array[0] | array[index] < minimum |
| Find maximum | index 1 | array[0] | array[index] > maximum |
| Count | index 0 | 0 | condition appropriate to the problem |
Note that find-minimum and find-maximum loop from index 1 (since the initial value is already set to index 0), while the count algorithm loops from index 0 (because the counter starts at 0, not at the first element's value).
Worked examples
An array stores the daily low temperatures (°C) for six days: [18, 12, 25, 9, 21, 14]. Write Python code to find and display the minimum temperature.
minimum = temps[0] — this gives minimum the value 18, which is a real value from the array.range(1, len(temps)) to visit indices 1 through 5. Index 0 is already handled by the initialisation.
temps = [18, 12, 25, 9, 21, 14]
minimum = temps[0]
for index in range(1, len(temps)):
if temps[index] < minimum:
minimum = temps[index]
print("Minimum:", minimum)Minimum: 9. Verified: 9 is the smallest value ✓Six exam scores are stored: [45, 78, 62, 91, 30, 55]. Find and display the highest score.
maximum = scores[0] (value 45). Loop from index 1. Change the condition to > instead of <.scores = [45, 78, 62, 91, 30, 55]
maximum = scores[0]
for index in range(1, len(scores)):
if scores[index] > maximum:
maximum = scores[index]
print("Maximum:", maximum)Maximum: 91. Verified: 91 is the largest value ✓An array stores the ages of pupils who attended an after-school club across 8 sessions: [16, 14, 17, 15, 16, 18, 16, 13]. Count how many sessions were attended by a pupil aged exactly 16.
count = 0. The counter starts at zero — no sessions have been counted yet.ages = [16, 14, 17, 15, 16, 18, 16, 13]
count = 0
for index in range(len(ages)):
if ages[index] == 16:
count = count + 1
print("Count:", count)Count: 3. Verified: there are three 16s in the array ✓Write SQA pseudocode to find the minimum value in an array of five prices: [4.50, 2.99, 6.75, 3.20, 5.00].
SET … TO for assignment, IF … THEN … END IF for conditions, FOR index FROM … TO … DO … END FOR for loops. The upper bound is LEN(prices) - 1 (= 4 for a 5-element array).SET prices TO [4.50, 2.99, 6.75, 3.20, 5.00]
SET minimum TO prices[0]
FOR index FROM 1 TO LEN(prices) - 1 DO
IF prices[index] < minimum THEN
SET minimum TO prices[index]
END IF
END FOR
SEND "Minimum price: " & minimum TO DISPLAYMinimum price: 2.99. Verified: 2.99 is the smallest price ✓An array stores the rainfall (mm) recorded each day for a week: rainfall = [23, 45, 12, 67, 34, 19, 8].
Answer the following:
- Write Python code to find and display the maximum rainfall recorded in the week.
- Write Python code to count how many days had rainfall of 20 mm or less.
- By tracing the find-minimum algorithm, what is the minimum rainfall recorded? Show your working.
-
rainfall = [23, 45, 12, 67, 34, 19, 8] maximum = rainfall[0] for index in range(1, len(rainfall)): if rainfall[index] > maximum: maximum = rainfall[index] print("Maximum rainfall:", maximum)Trace: maximum=23 → index=1: 45>23 ✓ max=45 → index=2: 12>45? No → index=3: 67>45 ✓ max=67 → index=4: 34>67? No → index=5: 19>67? No → index=6: 8>67? No. Output:Maximum rainfall: 67✓ -
rainfall = [23, 45, 12, 67, 34, 19, 8] count = 0 for index in range(len(rainfall)): if rainfall[index] <= 20: count = count + 1 print("Days with 20mm or less:", count)Trace: 23≤20? No. 45≤20? No. 12≤20? Yes (count=1). 67≤20? No. 34≤20? No. 19≤20? Yes (count=2). 8≤20? Yes (count=3). Output:Days with 20mm or less: 3✓ - minimum=rainfall[0]=23. index=1: 45<23? No. index=2: 12<23 ✓ minimum=12. index=3: 67<12? No. index=4: 34<12? No. index=5: 19<12? No. index=6: 8<12 ✓ minimum=8. Minimum rainfall: 8 mm ✓
minimum = 0 seems reasonable, but it breaks the algorithm the moment any array value is positive. For example, if the array is [5, 3, 8], no element is less than 0, so minimum stays at 0 — a value that is not even in the data. Always initialise to array[0], which guarantees the starting value is a real entry from the dataset.for index in range(1, len(data)): skips the first element, so it will never be counted even if it satisfies the condition. Use range(len(data)) for count — no starting offset needed because the counter is initialised to 0, not to data[0].= instead of == in the count condition. Writing if data[index] = 16: causes a SyntaxError — a single equals sign is assignment in Python, not a comparison. The equality comparison operator is always == (double equals). This applies inside both if statements and loops.count = 1 before the loop, every final answer will be one too high. A counter starts at zero because no elements have been examined yet — the loop adds 1 each time it finds a match, so the final result equals the number of matches found.FOR index FROM 1 TO LEN(array) - 1 DO. Writing TO LEN(array) (without the minus 1) attempts to access an index that does not exist and would cause an out-of-bounds error. The minus 1 is always required when the loop upper bound is expressed using LEN().These patterns are useful practice for array-traversal questions. In a trace question, work through the loop iteration by iteration and write down the value of the key variable (minimum, maximum, or count) after each step — do not try to work it out in your head. Show the trace as well as the final answer.
When asked to write pseudocode for find-minimum or find-maximum, the two most commonly penalised errors are: (a) initialising to 0 instead of array[0], and (b) writing TO LEN(array) instead of TO LEN(array) - 1 in the loop bounds. Both cost marks even if the rest of the answer is correct.
Also watch for the command word "describe" — it asks for a step-by-step description of how the algorithm works, not just the code. A good description names the initialisation, the comparison, and the update step: "Set minimum to the first element. Loop through the remaining elements. If the current element is less than minimum, update minimum to that element's value."
Questions 1–5 are auto-checked. Questions 6–8 are self-marked — write your answer, then reveal the model answer to check your work. Questions 9–10 are practical PyCharm implementation tasks.
1. What value should the variable minimum be initialised to at the start of a find-minimum algorithm? TYPE 1
2. An array is declared as nums = [7, 3, 9, 1, 5]. After a find-minimum algorithm runs, what is the value of minimum? TYPE 1
3. A count algorithm is used on an array. What value must count be set to before the loop begins? TYPE 1
4. An array is declared as values = [10, 25, 8, 17, 32, 4]. What is the maximum value? TYPE 1
5. The following code runs on data = [5, 12, 3, 8, 12, 7, 12]. What does it output?
count = 0
for index in range(len(data)):
if data[index] == 12:
count = count + 1
print(count)
TYPE 1
6. An array temps = [15, 8, 22, 5, 19, 11] stores temperatures in °C. Write Python code to find and display the minimum temperature. Include a trace of the minimum variable at each step. TYPE 2
temps = [15, 8, 22, 5, 19, 11]
minimum = temps[0]
for index in range(1, len(temps)):
if temps[index] < minimum:
minimum = temps[index]
print("Minimum temperature:", minimum)
Trace: minimum=15. index=1: 8<15 ✓ minimum=8. index=2: 22<8? No. index=3: 5<8 ✓ minimum=5. index=4: 19<5? No. index=5: 11<5? No. Output: Minimum temperature: 5. Verified: 5 is the smallest value in [15, 8, 22, 5, 19, 11] ✓
7. Write SQA pseudocode for a program that finds the maximum value in an array called prices containing the values [12.99, 8.50, 24.99, 6.75, 15.00]. Display the result. TYPE 2
SET prices TO [12.99, 8.50, 24.99, 6.75, 15.00]
SET maximum TO prices[0]
FOR index FROM 1 TO LEN(prices) - 1 DO
IF prices[index] > maximum THEN
SET maximum TO prices[index]
END IF
END FOR
SEND "Maximum price: " & maximum TO DISPLAY
Trace: maximum=12.99. index=1: 8.50>12.99? No. index=2: 24.99>12.99 ✓ maximum=24.99. index=3: 6.75>24.99? No. index=4: 15.00>24.99? No. Result: 24.99 ✓. Note the loop bound is LEN(prices) - 1 = 4, so the loop visits indices 1, 2, 3, 4.
8. The code below is meant to count how many values in the array are greater than 50, but it has an error. Identify the error, explain what happens when the program runs, and write the corrected code.
marks = [45, 72, 38, 88, 61, 55]
count = 1
for index in range(len(marks)):
if marks[index] > 50:
count = count + 1
print("Count:", count)
TYPE 2
Error: count = 1 — the counter is initialised to 1 instead of 0. This means the final count will be 1 too high, producing the wrong answer without any error message from Python.
Effect: Values >50 in the array are 72, 88, 61, 55 — four values. The loop adds 1 four times, but because count started at 1, the output is Count: 5 instead of the correct Count: 4.
Corrected code:
marks = [45, 72, 38, 88, 61, 55]
count = 0
for index in range(len(marks)):
if marks[index] > 50:
count = count + 1
print("Count:", count)
Trace: 45>50? No. 72>50? Yes (count=1). 38>50? No. 88>50? Yes (count=2). 61>50? Yes (count=3). 55>50? Yes (count=4). Output: Count: 4 ✓
9. Practical: In PyCharm, create a new Python file called sdd14_min_max_count.py. Store [67, 42, 88, 55, 73, 90] and display the highest score, lowest score, and count of scores at least 60. Test once with that array and once with [60, 60, 59, 100]. Record both complete outputs. TYPE 3
scores = [67, 42, 88, 55, 73, 90]
maximum = scores[0]
minimum = scores[0]
count = 0
for index in range(len(scores)):
if scores[index] > maximum:
maximum = scores[index]
if scores[index] < minimum:
minimum = scores[index]
if scores[index] >= 60:
count = count + 1
print("Highest score:", maximum)
print("Lowest score:", minimum)
print("Passes (60 or above):", count)
Trace (index=0 to 5):
index=0: 67>67? No. 67<67? No. 67≥60? Yes (count=1).
index=1: 42>67? No. 42<67 ✓ min=42. 42≥60? No.
index=2: 88>67 ✓ max=88. 88<42? No. 88≥60? Yes (count=2).
index=3: 55>88? No. 55<42? No. 55≥60? No.
index=4: 73>88? No. 73<42? No. 73≥60? Yes (count=3).
index=5: 90>88 ✓ max=90. 90<42? No. 90≥60? Yes (count=4).
Exact expected outputs:
[67, 42, 88, 55, 73, 90] Highest score: 90 Lowest score: 42 Passes (60 or above): 4 [60, 60, 59, 100] Highest score: 100 Lowest score: 59 Passes (60 or above): 3
10. Practical: In sdd14_min_max_count.py, replace the previous program with the faulty Python below. Run it and record the exception type. Fix both defects, then test with [34, 12, 56, 8, 29] and [-4, -12, -1], recording both corrected outputs.
numbers = [34, 12, 56, 8, 29]
minimum = 0
for index in range(len(numbers) + 1):
if numbers[index] < minimum:
minimum = numbers[index]
print("Minimum:", minimum)
TYPE 3
The faulty code raises IndexError when it tries to access index 5. It also initialises minimum to 0 instead of the first array value.
numbers = [34, 12, 56, 8, 29]
minimum = numbers[0]
for index in range(1, len(numbers)):
if numbers[index] < minimum:
minimum = numbers[index]
print("Minimum:", minimum)
Exact expected outputs:
[34, 12, 56, 8, 29] → Minimum: 8 [-4, -12, -1] → Minimum: -12
Suggested timing: 60 minutes. Warm up 8 min; notes 15 min; worked examples 15 min; now you try 7 min; task set 15 min.
Key misconception to address: Initialising minimum or maximum to 0. This is by far the most common error. Use a concrete counter-example: ask pupils to find the minimum of [5, 8, 3] using minimum = 0 — walk through the trace and show that no update ever fires, so minimum wrongly stays at 0. Then show the correct version with minimum = array[0] and confirm it works.
Live demo suggestion: Open a Python REPL. Build the find-minimum algorithm incrementally: first show the array and the initialisation, then add the loop step by step, adding print statements to show minimum changing at each iteration. Run the buggy version (count=1) from Q8 first so pupils can see the wrong output — then fix it live and show the correct result.
Comparing the three algorithms: Draw a quick summary table on the board: min (initialise to array[0], loop from 1, condition <), max (same, condition >), count (initialise to 0, loop from 0, condition = problem-specific). Pupils tend to remember these as a family once they see the pattern side by side.
Extension question: Ask pupils to combine all three into a single program that reads 5 integers from the keyboard, stores them in an array, then prints the minimum, maximum, and count of values above the average. This integrates SDD11 (round), SDD9 (fixed loops), SDD12 (arrays), and SDD14 into one problem — good preparation for the assignment.
SQA command words covered: "trace" (Q2, Q4, Q5, Q6), "write" (Q6, Q7, Q9), "identify" and "explain" (Q8, Q10), "describe" (implicit in pseudocode questions — see exam tip).