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.
- In a flowchart: oval = start/stop, parallelogram = input/output, rectangle = process, diamond = decision.
- To trace an algorithm, follow it with real numbers and record each pass in a trace table.
- A loop repeats steps until a condition stops it.
- To solve a problem backwards, undo each step in reverse order.
What is an algorithm?
An algorithm is a precise list of steps with an input, a process and an output; the steps must be in order and must finish.
Everyday examples are a recipe or the method for long division. In maths, algorithms calculate values, generate sequences and solve equations.
- 1
Input
the starting values
- 2
Process
the steps, in order
- 3
Output
the result
Which of these is the output of the algorithm "input a number, double it, add 1"?
Flowcharts
A flowchart shows an algorithm as a diagram with arrows giving the order; each shape has a job.
Follow the arrows from the start. A question can describe a flowchart in words: input , double it and subtract to get ; if , output "Large", otherwise output .
- 1
Start (oval)
begin the algorithm
- 2
Input x (parallelogram)
read in the number
- 3
y = 2x − 3 (rectangle)
a process
- 4
Is y > 10? (diamond)
yes: output "Large"; no: output y
- 5
Stop (oval)
end
Oval
- Start and stop
Parallelogram
- Input and output
Rectangle
- A process (a calculation)
Diamond
- A decision: yes/no with two exits
Worked example
Use the algorithm above (double , subtract to get ; if output "Large", otherwise output ) with and then .
- 1
: .
- 2
Is ? is true, so the output is "Large".
- 3
: .
- 4
Is ? is false, so the output is .
With , what does the algorithm output?
Tracing an algorithm
Follow the steps with real numbers and record the values after each pass in a trace table.
Take and . Add to , reduce by , and repeat while .
A loop repeats steps until a condition stops it. When the test fails and the output is .
- 1
Pass 1
total = 5, n = 4
- 2
Pass 2
total = 9, n = 3
- 3
Pass 3
total = 12, n = 2
- 4
Pass 4
total = 14, n = 1
- 5
Pass 5
total = 15, n = 0: stop
Run the same algorithm with and . What is the output?
Writing an algorithm
Say the inputs, list the steps in order, include any decision or repeat, and say the output; then test it.
For : input ; work out ; work out ; divide by ; output .
- 1
Inputs
Say what the starting values are
- 2
Steps
List them in order
- 3
Decisions and loops
Include any test or repeat
- 4
Output
Say what is returned
Worked example
Use the algorithm to solve .
- 1
Inputs: , , , .
- 2
.
- 3
.
- 4
.
- 5
Check by substituting back: and .
What goes wrong with the algorithm if ?
Algorithms and sequences
Repeating a rule generates a sequence; a term-to-term rule is an algorithm.
If and each term is twice the previous term plus : Each term plus is a power of .
To find the first term greater than , keep applying the rule: , so it is the th term.
"Start at and add " gives with th term .
A sequence starts at 5 and adds 3 each time. What is the 10th term?
Algorithms and solving equations
Undo the operations in reverse order, starting from the output.
Solving is an algorithm: subtract from , then divide by . It fails if .
Working backwards through a flowchart uses the same idea: undo each step, starting from the output and finishing at the input.
- 1
Start with 29
the output
- 2
Subtract 5
29 − 5 = 24
- 3
Divide by 3
24 ÷ 3 = 8
- 4
x = 8
the input
Worked example
A flowchart multiplies the input by , then subtracts , and outputs . Find the input.
- 1
Start from the output, .
- 2
Undo "subtract " by adding : .
- 3
Undo "multiply by " by dividing by : .
A flowchart adds 7, then multiplies by 3, and outputs 30. What was the input?
Try an exam question
An algorithm starts with and . It adds to , reduces by , and repeats while . Trace the algorithm and state the output.
[3 marks]
- [1]The loop runs for n = 4, 3, 2 and 1 (4 passes).
- [1]total after each pass: 4, 7, 9, 10.
- [1]The output is 10.
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