OCR H446, section 2.3.1 (a)
An algorithm is a finite sequence of unambiguous steps that solves a problem. You need to be able to analyse a situation and design an algorithm for it, then show and check it.
Step 1: analyse the situation
Use the computational thinking ideas from 2.1 and 2.2:
- State the goal exactly.
- List the inputs and outputs (2.1.2 (a)).
- Identify the data to be stored, and choose data structures.
- List the rules and decisions (2.1.4).
- Note the constraints (size, speed, memory) and preconditions (2.1.2 (b)).
- Look for a known problem (2.2.2 (b)) whose algorithm can be reused.
- Decompose it (2.2.2 (c)).
Step 2: design the algorithm
Common ways to write down an algorithm:
| Method | Notes |
|---|---|
| Structured English | Numbered steps in plain language |
| Pseudocode | Code-like, not tied to a language. OCR has its own style |
| Flowchart | Symbols and arrows: shows the order and decisions clearly |
| Hierarchy chart | Shows the breakdown into sub-procedures |
Flowchart symbols
| Symbol | Meaning |
|---|---|
| Rounded rectangle (terminator) | Start or stop |
| Parallelogram | Input or output |
| Rectangle | Process (calculation or assignment) |
| Diamond | Decision (two exits: yes / no) |
| Arrow | Flow of control |
| Rectangle with double sides | Subroutine call |
OCR pseudocode reminders
a = 3 // assignment
x = input("Number: ") // input
print(a) // output
if a > 2 then ... elseif ... else ... endif
switch a: case 1: ... default: ... endswitch
for i = 0 to 9 ... next i
while a < 10 ... endwhile
do ... until a == 10
function name(x) ... return y ... endfunction
procedure name(x) ... endprocedure
array nums[5] // fixed-size array
Operators include MOD, DIV, ^ (power), AND, OR, NOT. Strings have .length and .substring(start, length).
Step 3: check the design
Trace table
Follow the algorithm by hand, recording each variable:
total = 0
for i = 1 to 3
total = total + i * i
next i
print(total)
i |
i * i |
total |
|---|---|---|
| 0 | ||
| 1 | 1 | 1 |
| 2 | 4 | 5 |
| 3 | 9 | 14 |
Output: 14.
Test data
Choose data that tests the algorithm properly:
| Type | Meaning | Example (a mark from 0 to 100) |
|---|---|---|
| Normal | Typical valid data | 55 |
| Boundary | On the edges of valid ranges | 0, 100, and the pass mark |
| Erroneous (invalid) | Should be rejected | -5, 101, "abc" |
Qualities of a good algorithm
- Correct: gives the right output for every valid input.
- Unambiguous and finite: each step is clear, and it ends.
- Efficient: uses a reasonable amount of time and memory (2.3.1 (b) to (d)).
- Readable and well structured.
- Robust: copes with unexpected input.
- General: works for any size of data, not just one example.
Worked example
Situation: A shop gives a loyalty discount. A customer enters their total spend. If it's over £100, they get 10% off. If it's over £200, they get 20% off. The shop wants the final price displayed, and a message for invalid (negative) values.
Analysis
- Input:
spend(a real number). Output: the final price, or an error. - Rules: more than 200: 20% off. More than 100: 10% off. Otherwise none.
- Constraint:
spendmust not be negative.
Design (pseudocode)
spend = float(input("Total spend: "))
if spend < 0 then
print("Invalid amount")
else
if spend > 200 then
price = spend * 0.8
elseif spend > 100 then
price = spend * 0.9
else
price = spend
endif
print(price)
endif
The higher threshold is tested first, so a spend of 250 gets 20% and not 10%.
Check with test data
| Type | spend |
Expected | Reason |
|---|---|---|---|
| Normal | 150 | 135 | 10% off |
| Normal | 250 | 200 | 20% off |
| Normal | 50 | 50 | No discount |
| Boundary | 100 | 100 | Not over 100 |
| Boundary | 100.01 | 90.009 | Just over 100 |
| Boundary | 200 | 180 | Not over 200, so 10% |
| Erroneous | -10 | "Invalid amount" | Negative |
The boundary tests are the most useful: they check > against >=.
Exam tip: when asked to design an algorithm, use the exact variable names and conditions of the scenario, and make sure each case is handled. Use OCR's pseudocode style where you can, but clear code in a high-level language is accepted if it's unambiguous. Check by tracing with a boundary value.
Check yourself
What is an algorithm?
A finite sequence of unambiguous steps that solves a problem.
Name three ways of writing down an algorithm
Any three of: structured English, pseudocode, a flowchart, a high-level programming language, a hierarchy chart for the structure.
What shape is a decision in a flowchart?
A diamond, with two exits.
Why should boundary values be used when testing an algorithm?
Mistakes such as using > instead of >= happen at the edges of ranges, so values on and next to the boundary show them up.
Give three qualities of a good algorithm
Any three of: correct for all valid input; unambiguous; finite; efficient; readable; robust; general.