Skip to main content
ScotlandComputer ScienceSyllabus dot point

What standard patterns solve the most common data-processing problems?

Standard algorithms: input validation, running total within a loop, traversing a 1-D array, linear search, counting occurrences, and finding the minimum or maximum.

An SQA Higher Computing Science answer on the standard algorithms, covering input validation, running totals, traversing a 1-D array, linear search, counting occurrences, and finding the minimum or maximum value.

Generated by Claude Opus 4.812 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this key area is asking
  2. Input validation
  3. Running total within a loop
  4. Traversing a 1-D array
  5. Linear search
  6. Counting occurrences
  7. Finding the minimum or maximum
  8. Examples in context
  9. Try this

What this key area is asking

The SQA wants you to know the standard algorithms and be able to design them in pseudocode: input validation, a running total within a loop, traversing a 1-D array, linear search, counting occurrences, and finding the minimum or maximum. These patterns recur in almost every program and the exam tests both writing and reading them.

Input validation

REPEAT
   RECEIVE age FROM (keyboard)
UNTIL age >= 0 AND age <= 120

This guarantees the rest of the program works with sensible data, a key part of making software robust.

Running total within a loop

A running total accumulates a sum (or count) across a loop. The accumulator is set to 0 before the loop, then each iteration adds to it:

SET total TO 0
FOR i FROM 0 TO 9 DO
   SET total TO total + values[i]
END FOR

The same pattern computes averages (divide the total by the count after the loop). Forgetting to initialise the total to 0, or initialising it inside the loop, is the classic error.

Traversing a 1-D array

Traversing means visiting every element of an array in order, normally with a fixed loop whose bounds match the array size. Traversal underlies the running total, search, count and find-max patterns - they are all traversals that do something at each element:

FOR i FROM 0 TO size-1 DO
   ... process array[i] ...
END FOR

Linear search works on unsorted data, which is why it is the standard search at Higher. It checks up to every element, so on average it inspects half the array.

Counting occurrences

Counting occurrences tallies how many elements meet a condition. A counter starts at 0 and is incremented inside an IF during a traversal:

SET count TO 0
FOR i FROM 0 TO size-1 DO
   IF marks[i] >= 50 THEN
      SET count TO count + 1
   END IF
END FOR

This answers questions such as "how many pupils passed?" The counter increments only when the condition is true, unlike a running total which adds the value itself.

Finding the minimum or maximum

SET highest TO data[0]
FOR i FROM 1 TO size-1 DO
   IF data[i] > highest THEN
      SET highest TO data[i]
   END IF
END FOR

Examples in context

These six patterns are the backbone of real software. A search box runs a search; a "total basket" figure is a running total; "items in stock below reorder level" is a count of occurrences; a leaderboard's top score is a find-maximum; form fields that reject bad input use validation. More advanced searches (binary search) and sorts are built on the same traversal idea you learn here, so mastering the Higher standard algorithms makes later study far easier.

Try this

Q1. State which type of loop input validation uses and why. [2 marks]

  • Cue. A conditional loop, because the number of invalid attempts is not known in advance.

Q2. State what value a maximum-finding algorithm should be initialised to. [1 mark]

  • Cue. The first element of the array (not 0).

Q3. State the difference between a running total and a counting-occurrences algorithm. [2 marks]

  • Cue. A running total adds each value to an accumulator; counting occurrences adds 1 to a counter only when a condition is true.

Exam-style practice questions

Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

SQA Higher (style)4 marksAn array temps holds 30 temperature readings. Using pseudocode, design a linear search that reports whether a target temperature entered by the user is present.
Show worked answer →

A linear search checks each element in turn, stopping when the target is found.

1. RECEIVE target FROM (keyboard)
2. SET found TO false
3. SET index TO 0
4. WHILE index < 30 AND found = false DO
5.    IF temps[index] = target THEN
6.       SET found TO true
7.    END IF
8.    SET index TO index + 1
9. END WHILE
10. IF found = true THEN
11.    SEND "Found" TO (display)
12. ELSE
13.    SEND "Not found" TO (display)
14. END IF

Markers reward a Boolean found flag initialised to false, a loop that stops early when found, the correct comparison, and reporting the outcome. A fixed loop that checks all elements and sets found is also acceptable.

SQA Higher (style)3 marksAn array scores holds 20 values. Using pseudocode, design an algorithm to find and display the highest score.
Show worked answer →

The find-maximum pattern assumes the first element is the highest, then updates if a larger one is found.

1. SET highest TO scores[0]
2. FOR index FROM 1 TO 19 DO
3.    IF scores[index] > highest THEN
4.       SET highest TO scores[index]
5.    END IF
6. END FOR
7. SEND highest TO (display)

Markers reward initialising highest to the first element (not 0), looping from the second element, the correct > comparison, updating highest, and displaying the result. Starting from index 0 is acceptable if highest is initialised to scores[0].

Related dot points

Sources & how we know this