Algorithms and flowcharts

Algorithms and flowcharts

IB MYP Maths Standard: Revision notes

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. 1

    Input

    the starting values

  2. 2

    Process

    the steps, in the right order

  3. 3

    Output

    the result

Parts of an algorithm

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. 1

    Start

    oval

  2. 2

    Input x

    parallelogram

  3. 3

    y = 2x − 3

    rectangle

  4. 4

    Is y > 10?

    diamond: yes goes to "Large", no goes to "Output y"

  5. 5

    Output "Large" or y

    parallelogram

  6. 6

    Stop

    oval

Flowchart: input x, double it and subtract 3, test y > 10

Oval

  • Start and stop

Parallelogram

  • Input and output

Rectangle

  • Process (a calculation)

Diamond

  • Decision with two exits

Using the flowchart, what is output when x=6.5x=6.5?

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 n=5n=5, total=0total=0. Add nn to totaltotal, reduce nn by 11, repeat while n>0n>0. When n=0n=0 the output is 1515.

024681012141612345Passtotal
Trace of the loop starting at n = 5, total = 0: the total after each pass ends at 15

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 n=4n=4 and total=0total=0, 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:

  1. Say what the inputs are.
  2. List the steps in order, using clear operations.
  3. Include any decision or repeat.
  4. Say what is output.

Test it with an example you know.

  1. 1

    Input p, q, r, s

  2. 2

    Work out p − r

  3. 3

    Work out s − q

  4. 4

    Divide (s − q) by (p − r)

  5. 5

    Output x

Algorithm for px + q = rx + s

Worked example

Use the algorithm for 5x+7=2x+225x+7=2x+22.

What should the algorithm do if p−r=0p-r=0?

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 u1=3u_1=3 and each term is twice the previous term plus 11, the terms are 3,7,15,31,63,127,255,511,…3,7,15,31,63,127,255,511,\ldots. You can spot a pattern: each term plus 11 is a power of 22 (4,8,16,32,…4,8,16,32,\ldots).

Other rules, such as "start at 55 and add 33", give 5,8,11,…5,8,11,\ldots and the nnth term is 5+3(n−1)5+3(n-1).

0100200300400500371512345678Term numberValue
Terms of the sequence 3, 7, 15, … (double and add 1)

Worked example

Find the first term greater than 500500.

What is the next term after 511511?

Solving equations as algorithms

Solving ax+b=cax+b=c means undoing the operations in reverse order.

Solving ax+b=cax+b=c is an algorithm: undo the operations in reverse order. Subtract bb from cc, then divide by aa.

The algorithm fails if a=0a=0, because dividing by zero is undefined and the equation has no xx term.

Working backwards through a flowchart uses the same idea.

  1. 1

    Start with ax + b = c

    a must not be 0

  2. 2

    Subtract b from both sides

    ax = c − b

  3. 3

    Divide both sides by a

    x = (c − b) ÷ a

Solving ax + b = c as an algorithm
  • Solve ax+b=cax+b=cx=c−bax=\dfrac{c-b}{a}

Worked example

Solve 3x+5=293x+5=29, then find the input to a flowchart that multiplies by 44, subtracts 66 and outputs 1010.

A flowchart multiplies by 33, then adds 22, and outputs 2020. What was the input?

Try an exam question

An algorithm starts with total=0total=0. It adds nn to totaltotal, reduces nn by 11, and repeats while n>0n>0, then outputs totaltotal. Trace it for n=3n=3 and say what it calculates.

[4 marks]

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

Revision notes