mathematics for computing lecture 4: algorithms and flowcharts dr andrew purkiss-trew cancer...

13
Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK [email protected]

Upload: ira-arnold

Post on 16-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Mathematics for Computing

Lecture 4:Algorithms and flowcharts

Dr Andrew Purkiss-TrewCancer Research UK

[email protected]

Page 2: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Algorithms

An algorithm is a step-by-step set of instructions for solving a particular problem

We will be using flowcharts to explore simple algorithms

Page 3: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart

Graphical representation of an algorithm

Show the ‘layout’ of a program

Form is a series of symbols linked with arrows

Page 4: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart symbols 1

Terminator

Data Entry (or Input/Output)

Process

Page 5: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart Symbols 2

Decision

Preparation

Connector 1

Page 6: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart example 1

Find the meanof two numbers

Start

Read X,Y

Sum = X+Y

Mean = Sum / 2

Print Mean

Stop

Page 7: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart example 2:1

Solve a quadratic equation ax2+bx+c = 0

Discriminant, D = b2-4ac, tells us how many real solutions. D <0 means no real solution, D=1 means 1 solution: x = -b/2aD>0 means 2 solns: x = -b ± √(b2 – 4ac) 2a

Page 8: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart example 2:2

Start

Read A,B,C

D = B2-4xAxC

Sign ofD?

1 2 3

negative positive

zero

1 2 3

Print ‘Noreal solution

X = -B 2xA

Print ‘solution’, X

Stop

X1 = -B + √D 2xA

X2 = -B - √D 2xA

PrintX1, X2

Page 9: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Pseudocode

Algorithms can also be represented as ‘English language’ instructions.

These are then converted to the relevant program code.

Page 10: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk
Page 11: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk
Page 12: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Variables and Constants

Variable

ConstantsNumerical: Integer or exponentialNon-numerical: StringsLogical: True or False

Page 13: Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK a.purkiss@mail.cryst.bbk.ac.uk

Flowchart Symbols

Input / Output

Process

Decision

Connector

Preparation

Terminal