Algorithms and flowcharts

Algorithms and flowcharts

IB MYP Maths Extended: 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.
  • 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. 1

    Input

    the starting values

  2. 2

    Process

    the steps, in order

  3. 3

    Output

    the result

Every algorithm has these three parts

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 xx, double it and subtract 33 to get yy; if y>10y>10, output "Large", otherwise output yy.

  1. 1

    Start (oval)

    begin the algorithm

  2. 2

    Input x (parallelogram)

    read in the number

  3. 3

    y = 2x − 3 (rectangle)

    a process

  4. 4

    Is y > 10? (diamond)

    yes: output "Large"; no: output y

  5. 5

    Stop (oval)

    end

The flowchart for: input x, double it and subtract 3 to get y; if y > 10 output Large, otherwise output y.

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 xx, subtract 33 to get yy; if y>10y>10 output "Large", otherwise output yy) with x=7x=7 and then x=5x=5.

With x=6.5x=6.5, 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 n=5n=5 and total=0total=0. Add nn to totaltotal, reduce nn by 11, and repeat while n>0n>0.

A loop repeats steps until a condition stops it. When n=0n=0 the test n>0n>0 fails and the output is 1515.

  1. 1

    Pass 1

    total = 5, n = 4

  2. 2

    Pass 2

    total = 9, n = 3

  3. 3

    Pass 3

    total = 12, n = 2

  4. 4

    Pass 4

    total = 14, n = 1

  5. 5

    Pass 5

    total = 15, n = 0: stop

Trace table, n = 5

Run the same algorithm with n=4n=4 and total=0total=0. 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 px+q=rx+spx+q=rx+s: input p,q,r,sp,q,r,s; work out p−rp-r; work out s−qs-q; divide s−qs-q by p−rp-r; output xx.

  1. 1

    Inputs

    Say what the starting values are

  2. 2

    Steps

    List them in order

  3. 3

    Decisions and loops

    Include any test or repeat

  4. 4

    Output

    Say what is returned

Writing an algorithm

Worked example

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

What goes wrong with the algorithm if p=rp=r?

Algorithms and sequences

Repeating a rule generates a sequence; a term-to-term rule is an algorithm.

If u1=3u_1=3 and each term is twice the previous term plus 11: 3,7,15,31,63,127,255,511,…3,7,15,31,63,127,255,511,\ldots Each term plus 11 is a power of 22.

To find the first term greater than 500500, keep applying the rule: 255<500<511255<500<511, so it is the 88th term.

"Start at 55 and add 33" gives 5,8,11,…5,8,11,\ldots with nnth term 5+3(n−1)5+3(n-1).

010020030040050012345678Term numberTerm value
Terms of the sequence: double and add 1, starting at 3

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 ax+b=cax+b=c is an algorithm: subtract bb from cc, then divide by aa. It fails if a=0a=0.

Working backwards through a flowchart uses the same idea: undo each step, starting from the output and finishing at the input.

  1. 1

    Start with 29

    the output

  2. 2

    Subtract 5

    29 − 5 = 24

  3. 3

    Divide by 3

    24 ÷ 3 = 8

  4. 4

    x = 8

    the input

Solving 3x+5=293x+5=29 by undoing

Worked example

A flowchart multiplies the input by 44, then subtracts 66, and outputs 1010. Find the input.

A flowchart adds 7, then multiplies by 3, and outputs 30. What was the input?

Try an exam question

An algorithm starts with n=4n=4 and total=0total=0. It adds nn to totaltotal, reduces nn by 11, and repeats while n>0n>0. Trace the algorithm and state the output.

[3 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