Callum's favourite question is "can you just tell me which one is cheapest?" The answer is always yes, because Quayside Code has written find minimum about four hundred times. Jess asked if there was a library function for it. "There is," said Priya, "and in the exam you're not allowed it. Write the loop."
Higher Computing Science
Try it free
A real sample of the course: no account needed. Write some Python, answer five exam-style questions and read a topic note. The full course has every topic in Higher Computing Science.
1. Write real code
The first lesson of Python for Higher: records and arrays of records. Type in the box, press Run, and the exercises check your code for you. It all runs in your browser: nothing to install.
2. Five exam-style questions
One from each part of the course. Choose an answer, say how sure you are, then press Check. Every wrong answer tells you why it is wrong.
3. A topic note
Every topic has a short note like this one, ending with what the exam asks and what gains the mark.
Higher has four standard algorithms, used with 1-D arrays or arrays of records. The papers never name them: they describe a task, and you recognise which one it needs.
Linear search
SET found TO FALSE
SET index TO 0
WHILE found = FALSE AND index < length(names) DO
IF names[index] = target THEN
SET found TO TRUE
ELSE
SET index TO index + 1
END IF
END WHILE
IF found = FALSE THEN
SEND "Not found" TO DISPLAY
END IF
found starts FALSE and becomes TRUE on a match. The "not found" message comes after the loop. A conditional loop stops at the first match, which is more efficient than checking every element.
Count occurrences
SET count TO 0
FOR index FROM 0 TO length(tours) - 1 DO
IF tours[index].price < 40 AND tours[index].children = TRUE THEN
SET count TO count + 1
END IF
END FOR
Find minimum (and maximum)
SET minPos TO 0
FOR index FROM 1 TO length(prices) - 1 DO
IF prices[index] < prices[minPos] THEN
SET minPos TO index
END IF
END FOR
Start with the first element, not 0: if every value were negative, or every price above 0, a starting value of 0 would never be replaced. For maximum, use >.
Only the filled elements
If an array can hold 10 000 entries but only numEntries have been filled, loop to numEntries - 1. The rest are not real data.
- This is where the biggest marks are, 3 to 6 at a time, and usually combined: the cheapest item that meets two conditions, a count with two conditions, whether a value is present.
- Count: 1 mark for initialising and incrementing the counter. Minimum or maximum: 1 mark for a suitable starting value and updating it. Search: found starts FALSE, set TRUE on a match, the "not found" message after the loop.
- The loop must cover the whole array, or exactly the filled elements.
- Write each algorithm with your own loop and IF. A built-in such as max() or a sort does not show the algorithm, and the reports say it cannot gain the marks.
More free help: revision guides to the questions candidates find hardest.