Common algorithm patterns
Sum, count, max/min, search, sort β all show up on Paper 2.
Running total / sum.
Total β 0
FOR Counter β 1 TO N
INPUT Number
Total β Total + Number
NEXT Counter
OUTPUT Total
Count items meeting a condition.
Count β 0
FOR Counter β 1 TO N
INPUT X
IF X > 50 THEN
Count β Count + 1
ENDIF
NEXT Counter
OUTPUT Count
Find the maximum.
INPUT Number
Max β Number
FOR Counter β 2 TO N
INPUT Number
IF Number > Max THEN
Max β Number
ENDIF
NEXT Counter
OUTPUT Max
Find the minimum. Same pattern, with < instead of > and Min instead of Max.
Average. Compute the running total then divide by N.
Total β 0
FOR Counter β 1 TO N
INPUT Number
Total β Total + Number
NEXT Counter
Average β Total / N
OUTPUT Average
Cambridge tip. Memorise the patterns so you don't have to invent them in the exam. Most Paper 2 algorithm questions are variations on these.
- Running total: Total β Total + Number.
- Counter: Count β Count + 1.
- Max: initialise to FIRST input, not 0.
- Average: total / count.
See the full worked example for writing algorithms and identifying errors β