Algorithms and flowcharts
Studying this course? Keep your progress in one place.
Key facts
- An algorithm is a precise list of steps with an input, a process and an output.
- A flowchart uses ovals, parallelograms, rectangles and diamonds joined by arrows.
- To trace an algorithm, follow it with real numbers in a trace table.
- A loop repeats steps until a condition stops it.
- Undo operations in reverse order to work backwards.
What is an algorithm?
An algorithm is a precise list of steps that takes an input, processes it and gives an output, and always finishes.
An algorithm is a precise list of steps to solve a problem or complete a task.
It has an input (the starting values), a process (the steps) and an output (the result). The steps must be clear, in the right order, and always finish.
Everyday examples are a recipe or the method for long division. In maths, algorithms are used to calculate values, generate sequences and solve equations.
- 1
Input
the starting values
- 2
Process
the steps, in the right order
- 3
Output
the result
In a recipe, the list of ingredients is the
Flowcharts
A flowchart shows an algorithm as a diagram, with a standard shape for each kind of step and arrows showing the order.
A flowchart shows an algorithm as a diagram. Follow the arrows from the start. In a written description, each box is a numbered step. A decision is a yes/no question with two exits.
- 1
Start
oval
- 2
Input x
parallelogram
- 3
y = 2x − 3
rectangle
- 4
Is y > 10?
diamond: yes goes to "Large", no goes to "Output y"
- 5
Output "Large" or y
parallelogram
- 6
Stop
oval
Oval
- Start and stop
Parallelogram
- Input and output
Rectangle
- Process (a calculation)
Diamond
- Decision with two exits
Using the flowchart, what is output when ?
Tracing an algorithm
Follow the steps with real numbers, recording the values in a trace table with one row per pass.
To trace an algorithm, record the values in a trace table. A loop repeats steps until a condition stops it, so count the passes carefully.
Start with , . Add to , reduce by , repeat while . When the output is .
| total | n | |
|---|---|---|
| Pass 1 | 5 | 4 |
| Pass 2 | 9 | 3 |
| Pass 3 | 12 | 2 |
| Pass 4 | 14 | 1 |
| Pass 5 | 15 | 0 |
total
- Pass 1:
- 5
- Pass 2:
- 9
- Pass 3:
- 12
- Pass 4:
- 14
- Pass 5:
- 15
n
- Pass 1:
- 4
- Pass 2:
- 3
- Pass 3:
- 2
- Pass 4:
- 1
- Pass 5:
- 0
Start with and , using the same algorithm. What is the output?
Writing an algorithm
State the inputs, list the steps in order, include any decision or repeat, say what is output, and test it.
To write an algorithm:
- Say what the inputs are.
- List the steps in order, using clear operations.
- Include any decision or repeat.
- Say what is output.
Test it with an example you know.
- 1
Input p, q, r, s
- 2
Work out p − r
- 3
Work out s − q
- 4
Divide (s − q) by (p − r)
- 5
Output x
Worked example
Use the algorithm for .
- 1
.
- 2
.
- 3
.
- 4
Check: and .
What should the algorithm do if ?
Algorithms and sequences
Repeating a term-to-term rule is an algorithm, and it generates a sequence.
An algorithm that repeats a rule generates a sequence. A term-to-term rule (what to do to the previous term) is an algorithm.
If and each term is twice the previous term plus , the terms are . You can spot a pattern: each term plus is a power of ().
Other rules, such as "start at and add ", give and the th term is .
Worked example
Find the first term greater than .
- 1
Keep applying the rule: .
- 2
.
What is the next term after ?
Solving equations as algorithms
Solving means undoing the operations in reverse order.
Solving is an algorithm: undo the operations in reverse order. Subtract from , then divide by .
The algorithm fails if , because dividing by zero is undefined and the equation has no term.
Working backwards through a flowchart uses the same idea.
- 1
Start with ax + b = c
a must not be 0
- 2
Subtract b from both sides
ax = c − b
- 3
Divide both sides by a
x = (c − b) ÷ a
- Solve
Worked example
Solve , then find the input to a flowchart that multiplies by , subtracts and outputs .
- 1
, then .
- 2
Undo in reverse order: .
- 3
Then .
A flowchart multiplies by , then adds , and outputs . What was the input?
Try an exam question
An algorithm starts with . It adds to , reduces by , and repeats while , then outputs . Trace it for and say what it calculates.
[4 marks]
- [1]Pass 1: , .
- [1]Pass 2: , .
- [1]Pass 3: , ; the test fails and the output is .
- [1]It adds the whole numbers from down to .
That's the notes covered.
Carry on to the next subtopic.
Try a question
What does the algorithm output?
1 mark, marked against the mark scheme
What does the algorithm output?
1 mark, marked against the mark scheme