Upload
ezra-weatherall
View
215
Download
0
Tags:
Embed Size (px)
Citation preview
Algorithms
10 IST – Topic 6
What is an algorithm?
“a series of detailed instructions or steps that will solve a problem in a set amount of time”
Common examples of algorithms
Appliance Instructions
a logical set of steps to operate an appliance
Common examples of algorithms
Recipes
a logical set of steps to cook an item
Common examples of algorithms
Directions
a logical set of steps to arrive at a location
Common examples of algorithms
Repair Manual
a logical set of steps to repair an item
Advantages of using algorithms
solve problems in a logical step-by-step process avoiding vague “blundering-in-the-darkness”
finite – always has an end methods are standardised language is standardised
problems are described in the same way terminology is uniform
Representing algorithms
Pseudocodeprecise form of English that uses keywords and rules of structure
Flowchartsa pictorial method of describing algorithms using a set of symbols, connecting lines and arrows.
Pseudocode
Keyword Meaning
BEGINEND
to start a programto finish a program
INITIALISATIONEND INITIALISATION
to set any values at the start of a programto end the values section
BEGIN SUBPROGRAMEND SUBPROGRAM
to start a subprogramto finish a subprogram
IF <condition> THEN ELSEENDIF
to allow selection or choicefor another choice (may not be needed)to end the choice
CASEWHERE <condition> OTHERWISEENDCASE
to allow for many choicesto end the choices
WHILE <condition>ENDWHILE
to begin a loop at the start of a sequence
REPEATUNTIL <condition>
to begin a loop at the end of a sequence
Pseudocode Rules
Keywords are written in CAPITALS
Basic keywords come in pairs … for every BEGIN there is an END
Indenting is used to show structure
Names of subprograms are underlined
Flowcharts
Flowchart Rules
There is only ever ONE WAY into a flowchart structure and ONE WAY out.
A single flowchart should fit on one page
If larger than one page … use subprograms
Control Structures
there are 3 basic control structures to show the order in which statements are carried out:
sequencing (or steps)
selection (or choice)
repetition (or loops)
Sequencing
most common
each step is carried out in order of its position
each step is done only once.
Selection
Binary selection allows the choice between 2 possible paths if one condition is met then one path is taken, otherwise the other
path is taken
Selection
Case or Multi-way selection allows the choice between 3 or more possible paths if the condition is met (TRUE) then one path is taken, otherwise
one of the other paths is taken
Repetition carries out a particular action any number of times until a
condition is met a loop is created to return the program to a point where the
repetition starts and continues until the condition is met the loop must have a terminating (ending) condition that is…
tested at some time during each repetition updated during each repetition
Repetition
Pre-test Repetition (While Loop / Guarded Loop) the condition is tested at the start of the loop if the condition is false the first time the processes will NEVER be
carried out. Pre-test loops end when the condition is false. Uses WHILE … ENDWHILE
Repetition
Post-test Repetition (Repeat-Until Loop / Unguarded Loop) the condition is tested at the end of the loop after the loop has
been run through Post-test loops end when the condition is true Uses REPEAT … UNTIL
Repetition
For-Next Repetition The body of the loop is executed a set number of times. A counter variable is given a starting value which is incremented
each time the loop is executed until a given final value is reached