Upload
dangbao
View
219
Download
0
Embed Size (px)
Citation preview
GCSE Computing A451 Unit 7.1 – Algorithms www.mrfraser.org
1
Unit 7.1 – Algorithms
What is an Algorithm?
An algorithm is a sequence of instructions (operations) that can be executed (carried out) to
perform a particular task and then end.
There are 2 main categories of algorithmic operations:
· Sequential Operations – instructions that are executed in order.
· Control Operations – instructions do not have to be carried out in order.
These can be divided further into:
o Conditional Operations - a control structure that checks if a condition is TRUE
or FALSE and then executes an instruction based on the answer (IF...ELSE).
o Iterative Operations - a control structure that repeats a set of instructions
until a condition is met (loops).
Representing Algorithms
To write an algorithm in everyday language would be time consuming, and possibly
inaccurate due to being open to interpretation in different ways.
To write an algorithm in a specific programming language would require specific knowledge
of the syntax of that language and would be too complicated.
There are 2 main ways to represent algorithms so that they are accurate, succinct and easy
to understand for everyone (including non-programmers!)...
Candidates should be able to:
a. Understand algorithms (written in pseudo-code or as flow diagrams), explain what
they do, and correct or complete them
b. Produce algorithms in pseudo-code or flow diagrams to solve problems.
GCSE Computing A451 Unit 7.1 – Algorithms www.mrfraser.org
2
System Flow Charts / Flow Diagrams
· Visual representation of a system
· Small number of symbols used to represent individual steps
· Arrows show the flow of control within the algorithm (the order of the instructions).
· Easier to trace a sequence of steps & locate logic errors than in pseudo-code.
Symbol Name Symbol Picture Comments
Start / Stop Symbol
· Used at start and end of flowchart
· Only one start point!
· Multiple stop points possible
Process Symbol
· Instructions to be executed
· No decisions required
· Name of process written in box
Input / Output
Symbol
· IO operation to be performed
· Text inside symbol state if I/O
e.g. INPUT subTotal
Decision Symbol
· Point at which decision is needed
· One entry point
· Minimum of 2 exit points
· Text should be a question
· Yes/No or True/False
Case Symbol
· Represents Switch / Case statement
· One entry point
· Multiple exit points
· Text shows name of variable name
being used in case statement
Subroutine Symbol
· Shows when a subroutine is called
· Text shows name of subroutine
· Subroutine has its own flowchart
· Parameters should also be shown
STOP
START
Process
Name
I/O Name
Name
CASE
x > 3? Yes No
GCSE Computing A451 Unit 7.1 – Algorithms www.mrfraser.org
3
Pseudo Code
Pseudo-code represents an algorithm by using adapted everyday language and common keywords
from programming languages. Instructions are easy for us to read and understand, but still look
something like the statements available in many programming languages. Pseudo-code is not a strict
list of commands since it is not meant to be understood by a computer.
There are therefore no universal standards for pseudo-code, but it should follow these guidelines:
- Sequential operations arranged on separate lines with first instruction at the top
- Indentation should be used with blocks of instructions that are part of control
operations (such as conditional and iterative operations)
- Keywords should be in UPPERCASE and the rest of the pseudo-code should be in lower
case
- Pseudo-code should not be ambiguous
- Pseudo-code must actually be computable! (possible for it to be carried out)
- Descriptions/comments can be included but should be as brief as possible
- It must be complete, with nothing is left out.
- It must have an end, so it does not continue forever!
· Pseudo-code will often end with a list of all the variables used in the algorithm.
· Once algorithms are written (in pseudo-code or flow charts) they should be tested using dry-
runs. This involves tracing through the entire algorithm from start to finish with sample test
data.
· Once a programmer is happy with the pseudo-code it can then be developed as a working
program using the source code of the programming language in use.
Keywords Description
WHILE / ENDWHILE Iterative operation (loop) where condition is tested at start of loop.
Instructions within loop are repeated until condition becomes FALSE.
REPEAT / UNTIL Iterative operation (loop) where condition is tested at end of loop.
Instructions within loop will be repeated until condition becomes FALSE.
CASE OF Multiple decision (selection) branch based on the value of an expression.
READ, OBTAIN, GET Used to input data
PRINT, DISPLAY, SHOW Used to output data
COMPUTE,
CALCULATE,
DETERMINE
Used to carry out calculations
SET, INIT Used to initialise variables
INCREMENT,
++ / +=
Used to increment a variable
DECREMENT,
-- / -=
Used to decrement a variable
IF / THEN / ELSE / ENDIF Used to select different instructions based on result of Boolean test
FOR / NEXT Iterative Operation (loop) based on a count, rather than a condition.
Instructions within loop are executed a specific number of times as
determined by count index.
GC
SE
Co
mp
uti
ng
A4
51
U
nit
7.1
– A
lgo
rith
ms
ww
w.m
rfra
ser.
org
4
Ex
am
ple
s o
f P
seu
do
-Co
de
& F
low
Ch
art
s
Ex
am
ple
1 -
Alg
ori
thm
to
ca
lcu
late
th
e S
ale
s T
ax
(VA
T)
of
an
ite
m a
nd
th
en
usi
ng
it t
o w
ork
ou
t th
e f
ina
l pri
ce.
Alg
ori
thm
use
s se
qu
en
tia
l in
stru
ctio
ns:
GE
T &
DIS
PLA
Y
GET item_price
GET vat_rate
vat_amount = item_price x vat_rate
final_price = item_price + vat_amount
DISPLAY final_price
END
Va
ria
ble
s:
·
ite
m_
pri
ce
[sin
gle
]
·
va
t_ra
te
[sin
gle
]
·
va
t_a
mo
un
t [s
ing
le]
·
fin
al_
pri
ce
[sin
gle
]
ST
OP
ST
AR
T
INP
UT
ite
m_
pri
ce
INP
UT
va
t_ra
te
PR
OC
ES
S
va
t_a
mo
un
t =
ite
m_
pri
ce x
va
t_ra
te
PR
OC
ES
S
fin
al_
pri
ce =
ite
m_
pri
ce +
va
t_a
mo
un
t
OU
TP
UT
fin
al_
pri
ce
GC
SE
Co
mp
uti
ng
A4
51
U
nit
7.1
– A
lgo
rith
ms
ww
w.m
rfra
ser.
org
5
Ex
am
ple
2 -
Alg
ori
thm
to
ca
lcu
late
we
ek
ly w
ag
es
wh
en
gro
ss p
ay
de
pe
nd
s o
n t
he
pa
y r
ate
an
d t
he
nu
mb
er
of
ho
urs
wo
rke
d p
er
we
ek
. If
mo
re t
ha
n 3
5
ho
urs
are
wo
rke
d,
tim
e-a
nd
-a-h
alf
is
pa
id f
or
the
ext
ra h
ou
rs.
Alg
ori
thm
use
s se
qu
en
tia
l in
stru
ctio
ns:
GE
T &
DIS
PLA
Y
an
d c
on
dit
ion
al
con
tro
l o
pe
rati
on
s IF
/ T
HE
N /
ELS
E /
EN
DIF
.
GET hours_worked
GET pay_rate
IF hours_worked ≤ 35 THEN
gross_pay = pay_rate x hours_worked
ELSE
gross_pay = (pay_rate x 35) +
(1.5 x pay_rate x (hours_worked - 35))
ENDIF
DISPLAY gross_pay
END
Va
ria
ble
s:
·
ho
urs
_w
ork
ed
[s
ing
le]
·
pa
y_
rate
[sin
gle
]
·
gro
ss_
pa
y
[sin
gle
]
ST
OP
ST
AR
T
INP
UT
ho
urs
_w
ork
ed
INP
UT
pa
y_
rate
OU
TP
UT
gro
ss_
pa
y
ho
urs
_w
ork
ed
<=
35
?
Ye
s N
o
PR
OC
ES
S
gro
ss_
pa
y =
(p
ay
_ra
te *
35
) +
(1.5
* p
ay
_ra
te *
(h
ou
rs_
wo
rke
d -
35
))
PR
OC
ES
S
gro
ss_
pa
y =
pa
y_
rate
* h
ou
rs_
wo
rke
d
GC
SE
Co
mp
uti
ng
A4
51
U
nit
7.1
– A
lgo
rith
ms
ww
w.m
rfra
ser.
org
6
Ex
am
ple
3 -
Alg
ori
thm
to
ca
lcu
late
th
e a
ve
rag
e p
rice
fo
r a
nu
mb
er
of
ite
ms.
Alg
ori
thm
use
s se
qu
en
tia
l in
stru
ctio
ns:
GE
T,
SE
T &
DIS
PLA
Y &
ite
rati
ve
co
ntr
ol
WH
ILE
/ E
ND
WH
ILE
.
GET total_items
SET total_cost = 0
SET count = 0
WHILE count < total_items
GET item_price
total_cost = total_cost + item_price
count = count + 1
ENDWHILE
average_price = total_cost / total_items
DISPLAY average_price
END
Va
ria
ble
s:
·
tota
l_it
em
s
[in
t]
·
tota
l_co
st
[s
ing
le]
·
cou
nt
[in
t]
·
ite
m_
pri
ce
[s
ing
le]
·
ave
rag
e_
pri
ce
[sin
gle
]
PR
OC
ES
S
cou
nt
= 0
ST
OP
ST
AR
T
INP
UT
tota
l_it
em
s
OU
TP
UT
av
era
ge
_p
rice
cou
nt
<
tota
l_it
em
s?
TR
UE
FA
LSE
PR
OC
ES
S
tota
l_co
st =
0
PR
OC
ES
S
tota
l_co
st =
tota
l_co
st +
ite
m_
pri
ce
INP
UT
ite
m_
pri
ce
PR
OC
ES
S
cou
nt
=co
un
t +
1
PR
OC
ES
S
av
era
ge
_p
rice
=
tota
l_co
st /
to
tal_
ite
ms