Академический Документы
Профессиональный Документы
Культура Документы
Algorithm Description
Second Edition
to accompany the
2 Unit (General)
2/3 Unit (Common)
3 Unit (Additional)
Computing Studies Syllabuses
Republished as a second edition with permission of the Director-General,
Department of School Education.
The original Methods of Algorithm Description document was developed at a
Computer Education Unit, Department of School Education writing workshop.
Published by
Board of Studies NSW
PO Box 460
North Sydney NSW 2059
Australia
5
Structure of the Document
The preliminary section consists of a description of the
programming process and some definitions of an algorithm
presented in the form of overhead transparency originals.
In the following section are descriptions of the main
features of two methods of algorithm description:
pseudocode and flowcharts. Descriptions and specific
examples of the programming structures of sequence,
selection, repetition and subprograms (procedures or
subroutines) are given.
The most substantial section of the document contains
sample problems and worked solutions that show the use
of each of the methods of algorithm description. The
solutions do not purport to describe the ‘ultimate’
solution, but rather one (or more) possible solutions to the
problem.
It will become obvious from reading the document that
not all solutions are directly suited to implementation on a
computer. The reason for this is that an aim of the
document is to show how solutions to problems can be
expressed through the use of sample problems to which
most people already know some solution.
There is a new section in this edition which provides
possible descriptions of the searching and sorting
algorithms required by Core Topic 2: Algorithm Design in
the 2/3 Unit (Common) Computing Studies course.
It must be noted that alternative solutions are possible. In
some cases different solutions are provided expressed in
each of the two prescribed methods. These can be used as
models for expressing the same solution in the other
method and should extend the reader’s understanding of
the topic.
The final section has a collection of problems for which no
solutions are given. These are provided to give you some
practice using the methods of algorithm description.
6
Methods of Algorithm Description
@@@@@@@@e? @@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e
@@@@@@@@e? @@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@
@@@@@@@@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
What is Programming?
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
Programming is the total creative process that @@
@@
@@ @@
@@ @@
@@
@@
@@
@@
involves these stages: @@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@
@@
@@ • clearly define the problem @@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
• analyse the problem @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@
@@
@@
@@
@@
• design a solution @@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
@@
• implement the solution @@
@@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
• test the solution @@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ • document the solution. @@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
@@
@@
In the appropriate circumstances we should also: @@
@@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
@@
• compare alternative solutions. @@
@@
@@
@@
@@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@@@@@@@ ?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@ ?@@@@@@@@
@@@@@@@@ ?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@ ?@@@@@@@@
7
Methods of Algorithm Description
@@@@@@@@e? @@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e
@@@@@@@@e? @@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@
@@@@@@@@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
@@
@@
@@
@@
What is an Algorithm? @@
@@
@@
@@
@@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
@@
An algorithm consists of a set of explicit and @@
@@
@@
@@
@@
@@
@@
@@
@@
unambiguous finite steps which, when carried out @@
@@
@@
@@
@@ @@
@@
@@
@@ for a given set of initial conditions, produce the @@
@@
@@
@@ @@
@@
@@
@@
@@
@@
corresponding output and terminate in finite time. @@
@@
@@
@@
@@
@@ @@
@@ @@
@@
@@
@@
@@
How to Solve it by Computer, RG Dromey, Prentice Hall UK, 1982 @@
@@
@@
@@
@@ @@
@@ @@
@@
@@ @@
@@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
An algorithm is a finite, definite, effective @@
@@
@@
@@ @@
@@ @@
@@
@@
@@ procedure, with some output. @@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
Computer Science, D Woodhouse et al, Jacaranda Wiley, 1984 @@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
@@
The series of steps that you develop to solve a @@
@@
@@
@@
@@
@@ @@
@@
@@
problem is known as a solution algorithm. There @@
@@
@@ @@
@@ @@
@@
@@
@@
@@
are many different algorithms for almost any @@
@@
@@
@@
@@
@@
@@
@@
problem. @@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
Understanding Information Technology, K Behan and D Holmes, @@
@@
@@ @@
@@ @@
@@
@@
@@
@@
Prentice Hall Australia, 1986 @@
@@
@@
@@
@@
@@
Reprinted with the permission of Prentice Hall Australia Pty Ltd. @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@@@@@@@ ?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@ ?@@@@@@@@
@@@@@@@@ ?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@ ?@@@@@@@@
8
Methods of Algorithm Description
@@@@@@@@e? @@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e
@@@@@@@@e? @@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@
@@@@@@@@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@h? @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
Algorithm: a step-by-step procedure for solving a @@
@@
@@ @@
@@
@@
@@
@@
@@
@@
problem; programming languages are essentially a @@
@@
@@
@@
@@
@@
@@
@@
way of expressing algorithms. @@
@@
@@
@@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@
@@
Understanding Computers: Computer Languages, @@
@@
@@ @@
@@
@@
@@
@@
by the editors of Time-Life Books, © 1988 @@
@@
@@
@@
@@ @@
@@
Time-Life Books Inc. @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@
@@
@@ In order that a task be carried out on a computer, @@
@@
@@ @@
@@ @@
@@
@@
@@
@@
@@
a method or technique for the task must be @@
@@
@@
@@
@@
@@ @@
@@
@@
described very precisely in terms of the different @@
@@
@@ @@
@@ @@
@@
@@
@@
@@
steps. An algorithm is a description of the steps of @@
@@
@@
@@
@@
@@
@@
@@
a task, using a particular technique. Writing an @@
@@
@@
@@
@@ @@
@@ @@
@@
@@ algorithm is one of the first steps taken in @@
@@
@@ @@
@@
@@
@@
@@
@@
preparing a task to be done by a computer. @@
@@
@@
@@
@@
@@
@@ @@
@@
@@ @@
@@
@@
@@
Computing Science, Peter Bishop, Thomas Nelson UK, 1982 @@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@
@@
@@
Informally, an algorithm is a collection of @@
@@
@@
@@ @@
@@
@@
@@
@@ instructions which, when performed in a specific @@
@@
@@
@@ @@
@@
@@
@@
@@
@@
sequence, produce the correct result. The study of @@
@@
@@
@@
@@ @@
@@
@@
@@
algorithms is at the heart of computer science. @@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ Problem Solving and Computer Programming, @@
@@ @@
@@
@@
@@
@@
Peter Grogono & Sharon H Nelson, @@
@@
@@
@@ @@
@@
@@
@@ © 1982 Addison-Wesley Publishing Company Inc. @@
@@
@@ @@
@@
@@
@@
Reproduced by the permission of the publisher. @@
@@
@@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@ @@
@@
@@ @@
@@ @@
@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@g ?@@
@@@@@@@@ ?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@ ?@@@@@@@@
@@@@@@@@ ?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@e?@@@@@@@@?e@@@@@@@@ ?@@@@@@@@
9
Overview of Two Methods
Pseudocode Pseudocode essentially is English with some defined
rules of structure and some keywords that make it appear
a bit like program code. Some guidelines for writing
pseudocode are as follows.
Pseudocode Guidelines
• The keywords used for pseudocode in this document
are:
for start and finish
BEGIN MAINPROGRAM, END MAINPROGRAM
for initialisation
INITIALISATION, END INITIALISATION
for subprogram
BEGIN SUBPROGRAM, END SUBPROGRAM
for selection
IF, THEN, ELSE, ENDIF
10
Methods of Algorithm Description
Flowchart Elements
Flowcharts are made up of the following box types
connected by lines with arrowheads indicating the flow. It
is common practice only to show arrowheads where the
flow is counter to that stated above.
terminator process
subprogram decision
The Structures Each of the five acceptable structures can be built from the
basic elements as shown below.
Multi-way selection
12
Methods of Algorithm Description
process 2
…
…
process n
process 1
process 2
process n
13
Methods of Algorithm Description
Pseudocode
BEGIN
fill a kettle with water
boil the water in the kettle
put the tea leaves in the pot
pour boiling water in the pot
END
Flowchart
begin
fill a kettle
with water
pour boiling
water in the pot
end
14
Methods of Algorithm Description
Binary Selection
As the name implies, binary selection allows the choice
between two possible paths. If the condition is met then
one path is taken, otherwise the second possible path is
followed. In each of the examples below, the first case
described requires a process to be completed only if the
condition is true. The process is ignored if the condition is
false. In other words there is only one path that requires
processing to be done, so the processing free path is left
out rather than included saying ‘do nothing’.
In pseudocode, binary selection is expressed in the
following ways:
1. IF condition THEN
process 1
ENDIF
2. IF condition THEN
process 1
ELSE
process 2
ENDIF
1.
False True
condition
process 1
15
Methods of Algorithm Description
2.
False True
condition
process 2 process 1
False True
A>0
No Is Yes
A>0?
16
Methods of Algorithm Description
Multi-way Selection
Multi-way selection allows for any number of possible
choices, or cases. The path taken is determined by the
selection of the choice which is true. Multi-way selection is
often referred to as a case structure.
In pseudocode, multiple selection is expressed as:
CASEWHERE expression evaluates to
choice a : process a
choice b : process b
. .
. .
. .
OTHERWISE : default process
ENDCASE
expression
17
Methods of Algorithm Description
Pseudocode
IF the telephone is ringing THEN
answer the telephone
ENDIF
Flowchart
answer the
telephone
Pseudocode
IF the signal is green THEN
proceed through the intersection
ELSE
stop the vehicle
ENDIF
Flowchart
proceed through
stop the vehicle the intersection
18
Methods of Algorithm Description
Pseudocode
CASEWHERE signal is
red : stop the vehicle
amber : stop the vehicle
green : proceed through the intersection
OTHERWISE : proceed with caution
ENDCASE
Flowchart
signal is
19
Methods of Algorithm Description
Repetition: Pre-Test
A pre-tested loop is so named because the condition has to
be met at the very beginning of the loop or the body of the
loop is not executed. This construct is often called a
guarded loop. The body of the loop is executed repeatedly
while the termination condition is true.
In pseudocode pre-test repetition is expressed as:
WHILE condition is true
process(es)
ENDWHILE
True False
process process
20
Methods of Algorithm Description
Repetition: Post-Test
A post-tested loop executes the body of the loop before
testing the termination condition. This construct is often
referred to as an unguarded loop. The body of the loop is
repeatedly executed until the termination condition is
true.
An important difference between a pre-test and post-test
loop is that the statements of a post-test loop are executed
at least once even if the condition is originally true,
whereas the body of the pre-test loop may never be
executed if the termination condition is originally true. A
close look at the representations of the two loop types
makes this point apparent.
In pseudocode, post-test is expressed as:
REPEAT
process
UNTIL condition is true
process
condition
False
True
21
Methods of Algorithm Description
Pseudocode
WHILE the train is moving
keep wholly within the carriage
ENDWHILE
Flowchart
True
keep wholly
within the car
22
Methods of Algorithm Description
Pseudocode
REPEAT
beat the egg whites
UNTIL fluffy
Flowchart
beat the
egg whites
True
23
Methods of Algorithm Description
BEGIN MAINPROGRAM
process l
process 2
process 3
process 4
END MAINPROGRAM
24
Methods of Algorithm Description
begin
process 1
process 2
process 3
process 4
end
begin process 2
do this
do that
end process 2
25
Methods of Algorithm Description
BEGIN MAINPROGRAM
read (name)
read (address)
END MAINPROGRAM
26
Methods of Algorithm Description
begin
read
(name)
read
(address)
end
begin read
(array)
set pointer to
first position
get a character
character ≠
end of data False
and room in
the array
True
store character in
array at position
given by the pointer
increment pointer
get a character
end read
27
Solved Problems
Methods of Algorithm Description
Binary Multiple
Problem Sequence Selection Selection Repetition: Repetition:
Subprogram
( if-then) ( case) ( while) ( repeat-until)
Lift
✓ ✓
Solution 1
Lift
✓ ✓
Solution 2
Temperature
✓ ✓ ✓
Control
Toll Gate ✓ ✓ ✓ ✓ ✓
Squash
✓ ✓ ✓ ✓
Scoring
Record
Separation ✓ ✓ ✓
Solution 1
Record
Separation ✓ ✓ ✓
Solution 2
Record
Separation ✓ ✓
Solution 3
Guess the
✓ ✓ ✓ ✓
Number
Income Tax ✓ ✓
Telephone
✓ ✓ ✓ ✓ ✓ ✓
Dialler
Auto-Teller ✓ ✓ ✓ ✓ ✓ ✓
30
Lift Problem
Problem A lift remains positioned at the ground floor level of a
building with the doors shut whenever it is not in use.
When a call button is pressed on any floor, the lift moves
to the required floor and the lift doors open. Write an
algorithm to express the logic of controlling the lift.
Pseudocode
An algorithm to express the logic of controlling a lift
BEGIN MAINPROGRAM
REPEAT
check all buttons
UNTIL a button is pressed
move to the required floor
open the doors
END MAINPROGRAM
Flowchart
An algorithm to express the logic of controlling a lift
begin
False button is
pressed
True
move to the
required floor
end
31
Methods of Algorithm Description
Pseudocode
An algorithm to express the logic of controlling a lift
BEGIN MAINPROGRAM
check all buttons
WHILE no button has been pressed
check all buttons
ENDWHILE
move to the required floor
open the doors
END MAINPROGRAM
Flowchart
An algorithm to express the logic of controlling a lift
begin
no button False
is pressed
True
move to the
required floor
end
32
Temperature Control Problem
Problem At a NSW coastal town the maximum annual temperature
range is typically 12–34 degrees Celsius. An air
conditioning company is installing a heating/cooling
system in a new shopping centre in that town. The system
checks the temperature every five minutes and adjusts the
air temperature by using a combination of two heating
and two cooling units. These units operate according to
these temperature ranges:
0–15 degrees C – 2 heating units
16–20 degrees C – 1 heating unit
21–28 degrees C – 1 cooling unit
> 29 degrees C – 2 cooling units
Write an algorithm that could be used to control the air
conditioning system.
Pseudocode
An algorithm to describe the control of an air conditioning
system. The input comes from sensors in the shopping
centre.
BEGIN MAINPROGRAM
read the temperature
WHILE the system is turned on
CASEWHERE temperature
< 16 : run two heating units
16 to 20 : run one heating unit
21 to 28 : run one cooling unit
OTHERWISE : run two cooling units
ENDCASE
wait five minutes
read the temperature
ENDWHILE
END MAINPROGRAM
33
Methods of Algorithm Description
Flowchart
An algorithm to describe the control of an air conditioning
system. The input comes from sensors in the shopping
centre.
begin
read the
temperature
system False
turned on
True
temperature
range is
wait five
minutes
read the
temperature
end
34
Toll Gate Problem
Problem When operational a toll gate operates by having a boom
gate obstructing the road, and a sensor detecting when a
vehicle is present. After coins to the value of $1.00 have
been deposited in the basket, the boom gate opens and
stays open until a vehicle has gone through. Amounts
greater than $1.00 are accepted but no change is given.
Individual coins less than 10 cents are ignored.
Write an algorithm to describe the control of the toll gate.
Pseudocode
An algorithm used to describe the operation of a toll gate
that has a boom gate, a vehicle sensor, and a coin
collection basket.
BEGIN MAINPROGRAM
REPEAT
REPEAT
wait
UNTIL car has arrived
get the money
open boom gate
REPEAT
wait
UNTIL car has passed
close boom gate
UNTIL toll gate is not operational
END MAINPROGRAM
35
Methods of Algorithm Description
Flowchart
An algorithm used to describe the operation of a toll gate
that has a boom gate, a vehicle sensor, and a coin collection
basket.
begin
wait
True
wait
True
True
end
36
Methods of Algorithm Description
Subprogram
begin
get the money
set money
collected to zero
money False
collected less
than $1
True
receive a coin
add value of
coin to money ignore coin
collected
end
get the money
37
‘Squash’ Scoring Problem
Problem Write an algorithm to describe how to score a ball game,
which is similar to squash. This ball game is scored as
follows: the server gets one point for winning a rally. If
the server loses the rally they lose the right to serve the
next ball, but lose no points. The receiver gains the right
to serve (but no point) if they win a rally. To win the game
a player must win nine points.
Pseudocode
An algorithm to describe the logic for scoring a ball game
similar to squash.
BEGIN MAINPROGRAM
INITIALISATION
set RequiredPoints to 9
set each player’s points to 0
END INITIALISATION
toss and decide the server
REPEAT
server serves the ball
REPEAT
play the rally
UNTIL rally is won
IF the server wins the rally THEN
increment the server’s points by 1
ELSE
swap player status
ENDIF
UNTIL a player has won RequiredPoints
declare the winner
END MAINPROGRAM
38
Methods of Algorithm Description
Flowchart
An algorithm to describe the logic for scoring a ball game
similar to squash.
begin
set RequiredPoints
to 9
toss and
decide server
server serves
the ball
play a rally
False
rally won
True
True
end
39
Methods of Algorithm Description
Subprogram
toss a coin
player 2 is player 1 is
the server the server
player 1 is player 2 is
the receiver the receiver
40
Record Separation Problem
Problem Let us assume that a particular database program
manages a simple mailing list which consists of one record
for each person on the list, and a number of fields
containing information about each person (their name,
address, etc). The program can read in data produced by a
word processor provided that data is structured in the
following way:
Each record to be read must be a single paragraph
terminated by a return character, and each field within a
record is separated by a tab character. For example:
Colin Jamesontab33 Falcon StreettabWaverlytabNSWtab2113return
41
Methods of Algorithm Description
Pseudocode
An algorithm to describe the separation of a string of
formatted data into fields and records to be used as input
to a database.
BEGIN MAINPROGRAM
INITIALISATION
set record number to 0
set field number to 0
set field to empty
END INITIALISATION
CASEWHERE character is
ENDCASE
ENDWHILE
END MAINPROGRAM
42
Methods of Algorithm Description
Flowchart
An algorithm to describe the separation of a string of
formatted data into fields and records to be used as input
to a database.
begin
read a character
from input file
character False
not a hash
True
character is
append character
output the field output the field to the field
increment the
set field to empty record number
read a character
from input file
end
43
Methods of Algorithm Description
Pseudocode
An algorithm to describe the separation of a string of
formatted data into fields and records to be used as input
to a database.
BEGIN MAINPROGRAM
INITIALISATION
record number is set to 0
field number is set to 0
field is set to empty
END INITIALISATION
REPEAT
read a character from the file
IF the character is a hash THEN
don’t do anything
ELSE
IF the character is a return THEN
output the field to the database
increment the field number
increment the record number
set the field number to 0
set the field to empty
ELSE
IF the character is a tab THEN
output the field to the database
increment the field number
set the field to empty
ELSE
append the character to the field
ENDIF
ENDIF
ENDIF
UNTIL the character is a hash
END MAINPROGRAM
44
Methods of Algorithm Description
Flowchart
An algorithm to describe the separation of a string of
formatted data into fields and records to be used as input
to a database.
begin
read a character
from input file
False character
is a hash
True
end
45
Methods of Algorithm Description
Pseudocode
An algorithm to describe the separation of a string of
formatted data into fields and records, which are to be
used as input to a database. It assumes the data are correct.
MAINPROGRAM
INITIALISATION
set record number to 0
set field number to 0
set field to empty
END INITIALISATION
read a character from the file
WHILE the character is not a hash
WHILE the character is not a return
WHILE the character is not a tab
append the character to the field
read a character from the file
ENDWHILE
output the field to the database
increment the field number
set field to empty
read a character from the file
ENDWHILE
output the field to the database
increment the record number
set field number to 0
set the field to empty
read a character from the file
ENDWHILE
report how many records were read
END MAINPROGRAM
46
Methods of Algorithm Description
Flowchart
An algorithm to describe the separation of a string of
formatted data into fields and records, which are to be
used as input to a database. It assumes the data are correct.
begin
read a character
from the file
character False
not a hash
True
character False
not a return
True
character False
not a tab
True
add the character
to the file
read a character
from the file
increment the
field number
read a character
from the file
increment the
record number
read a character
from the file
report number of
records read
end
47
Guess the Number Problem
Problem In a simple number game your opponent thinks of a secret
number between l and 100. In no more than 10 guesses
you have to try to guess the number. After each guess
your opponent tells you if your guess was too high, too
low or correct. Your opponent also keeps track of how
many guesses you have had and tells you the game is over
when you use all of your ten guesses or when you guess
the number correctly.
Describe an algorithm which takes the role of your
opponent in this game. Include in your solution a
subprogram which checks for illegal guesses (those less
than l or greater than 100). Include also the subprogram
which generates a secret number between 1 and 100. Do
not expand this but assume it is available.
48
Methods of Algorithm Description
Pseudocode
An algorithm to describe a game in which the user tries to
guess a number between 1 and 100, using no more than
ten guesses.
BEGIN MAINPROGRAM
INITIALISATION
number of guesses is set to 0
GotIt is set to false
END INITIALISATION
generate a secret number using random number generator
REPEAT
get a guess from the user
IF the guess is in range THEN
increment the number of guesses
check the guess
ELSE
tell the user the guess is out of range
ENDIF
UNTIL guess is correct (GotIt is true) or number of guesses is 10
IF the guess is incorrect (GotIt is false) THEN
tell the user they have run out of guesses (=10)
tell the user the secret number
ENDIF
END MAINPROGRAM
49
Methods of Algorithm Description
Flowchart
An algorithm to describe a game in which the user tries to
guess a number between 1 and 100, using no more than
ten guesses.
begin
generate a secret
number using
random generator
get a guess
from the user
GotIt is
False true or number
of guesses is
equal to 10
True
end
50
Methods of Algorithm Description
Subprogram
begin
check the guess
guess is
True bigger than False
secret
number
end
check the guess
51
Income Tax Problem
Problem To calculate the income tax payable on any income based
on the income tax scales shown below. The taxable income
is to be entered and the tax payable calculated.
Tax Scales
Pseudocode
An algorithm used to calculate the tax payable on any
income using taxation rates set in the given table.
BEGIN MAINPROGRAM
input income
IF income greater than or equal to 50 001 THEN
tax is 15 314 + (income – 50 000) * 0.47
ELSE
IF income greater than or equal to 36 001 THEN
tax is 8874 + (income – 36 000) * 0.46
ELSE
IF income greater than or equal to 20 701 THEN
tax is 3060 + (income – 20 700) * 0.38
ELSE
IF income greater than or equal to 5401 THEN
tax is (income – 5400) * 0.20
ELSE
tax is nil
ENDIF
ENDIF
ENDIF
ENDIF
display income and tax payable
END MAINPROGRAM
52
Methods of Algorithm Description
Flowchart
An algorithm used to calculate the tax payable on any
income using taxation rates set in the given table.
begin
tax is 15 314 +
(income – 50 000)
* 0.47 True income is False
≥
36 001
tax is 8874 +
(income – 36 000)
* 0.46 True income is False
≥
20 701
tax is 3060 +
(income – 20 700)
* 0.38 True income is False
≥
5401
tax is
(income – 5400) tax is nil
* 0.20
display income
and tax
end
53
Telephone Dialler Problem
Problem A telephone dialler is connected between a computer and
a telephone (see the diagram below). Its purpose is to dial
a telephone number entered via the computer keyboard,
establish a connection if it can and report on its progress
and degree of success. The whole telephone number is
entered via the computer keyboard at one time and is
stored in a buffer in the computer.
number pulses
Telephone
Dialler
message tones
(engaged,
answered etc)
54
Methods of Algorithm Description
Pseudocode
An algorithm to describe the control of a telephone dialler.
BEGIN MAINPROGRAM
REPEAT
try for phone line
UNTIL the response is a dial tone
send a message to the computer that a clear telephone line is available
REPEAT
REPEAT
get a character from the computer
UNTIL the character is a digit
IF the character is a 0 THEN
set the digit value of the character to 10
ENDIF
assign the digit value of the character to a counter
WHILE counter is greater than 0
send a pulse
decrement the counter
ENDWHILE
send no pulse for two seconds
UNTIL there is a response
determine outcome and send message (response)
END MAINPROGRAM
55
Methods of Algorithm Description
Flowchart
An algorithm to describe the control of a telephone dialler.
begin
try for a
telephone line
the
False response
is a
dialtone
True
send a message to
the computer that a
clear line is available
get a character
from the computer
False character
is a digit
True
counter is False
bigger
than 0
True
send a pulse
decrease the
counter by 1
send no pulse
for two seconds
False there is a
response on
the line
True
determine outcome
and send message
(response)
end
56
Methods of Algorithm Description
Subprogram
begin determine
outcome and send
message (response)
set maxtime to 60
send a message
send an error that the phone
message is ringing
set a timer
to 0
check to see
if the phone
is answered
increment timer
phone is
False answered or
timer > maxtime
True
phone
was
end determine
outcome and
send message
57
Auto Teller Problem
Problem An automatic teller machine has a console as shown in the
diagram below. The teller machine follows this sequence
to assist a customer:
1. The customer will insert their card and enter a PIN. A
customer is allowed at most three tries at their PIN. If
they get it wrong three times the whole process ends
without ejecting the card.
2. If the PIN is correct they will then select an action
button (withdraw, deposit or balance).
3. Next they will select the account type (savings or
cheque).
4. Finally, they will enter the amount in whole dollars (if
appropriate), and press OK to confirm it, and the
transaction will be processed.
The auto teller will eject the customer’s card and stop the
process if the customer presses the ‘Cancel’ button.
Describe an algorithm which the auto teller could use to
accept the details from the customer and act on them.
1 2 3 4 Withdrawal Savings
5 6 7 8 Deposit Cheque
9 0 Balance OK Cancel
58
Methods of Algorithm Description
Pseudocode
An algorithm to describe the control of an automatic teller
machine. Input comes from the buttons on the console,
output through a small video screen.
BEGIN MAINPROGRAM
INITIALISATION
set Action to an empty string
set Account to an empty string
set Amount to 0
END INITIALISATION
wait for the card to be inserted
get the PIN and check it (Action)
IF action is ‘cancel’ THEN
eject card
ELSE
IF action is not ‘keep card’ THEN
get action required (Action)
IF action is not ‘cancel’ THEN
get account to be used (Action, Account)
do the transaction
ENDIF
eject card
ENDIF
ENDIF
END MAINPROGRAM
59
Methods of Algorithm Description
60
Methods of Algorithm Description
61
Methods of Algorithm Description
62
Methods of Algorithm Description
Flowchart
An algorithm to describe the control of an automatic teller
machine. Input comes from the buttons on the console,
output through a small video screen.
begin
set Action to
an empty string
set Account to
an empty string
set Amount to 0
False Action is
‘cancel’
Action True
False is not
‘keep card’
True
get Action
required (Action)
False Action is
not
‘cancel’
eject card
True
get Account to
be used
(Action, Account)
do the transaction
(Action, Account)
eject card
end
63
Methods of Algorithm Description
Subprograms
True
wait
64
Methods of Algorithm Description
set OK to false
set number
of tries to 3
decrement
number of tries
False True
correct PIN
set OK to true
OK = true
False or
number of
tries = 0
True
False True
OK
set Action to
‘keep card’
65
Methods of Algorithm Description
get a keypress
False keypress is
action key
True
keypress is
66
Methods of Algorithm Description
get a keypress
False keypress is
acceptable
True
keypress is
67
Methods of Algorithm Description
set OK to false
get a keypress
False keypress is
acceptable
True
keypress is
OK cancel number
set Amount to 10
set OK to true set Action times the amount
to ‘cancel’ plus digit value
of number
set OK to true
False
OK is true
True
68
Methods of Algorithm Description
begin do
transaction
(Action, Account)
set Action
to ‘cancel’
Action is
True True
end
do transaction
69
Algorithms for
Searching and Sorting
Algorithms for Searching
To give some assistance with understanding the standard
algorithms for searching and sorting, as required in the 2/3
Unit (Common) Computing Studies Syllabus, the following
examples are provided.
Linear Search
1 2 3 4 5 6 7 8
72
Methods of Algorithm Description
Pseudocode
An algorithm to describe a linear (sequential) search to
find the seat number of the first person wearing a red
jumper from a set of persons sitting in a row of seats.
BEGIN MAINPROGRAM
INITIALISATION
stand in front of the first seat
set FoundIt to FALSE
set MoreSeats to TRUE
END INITIALISATION
get the description of the wanted person
WHILE FoundIt is FALSE AND MoreSeats
IF the person in front of you is not the wanted person THEN
stand in front of the next seat
ELSE
set FoundIt to TRUE
ENDIF
ENDWHILE
IF FoundIt THEN
report the seat number of the wanted person
ELSE
report that the wanted person is not present
ENDIF
END MAINPROGRAM
73
Methods of Algorithm Description
1 2 3 4 5 6 7 8
14 34 12 19 41 26 45 16
Flowchart
begin
NOT
FoundIt False
AND
This<=Last
True
Target = True
data at This
position
set FoundIt TRUE
set PositionFound to
False This position
False True
FoundIt
Increment This
report report
‘Target not found’ PositionFound
end
74
Methods of Algorithm Description
34 Target
1 2 3 4 5 6 7 8
12 14 16 19 26 34 41 45
1 2 3 4 5 6 7 8
12 14 16 19 26 34 41 45
75
Methods of Algorithm Description
Pseudocode
BEGIN MAINPROGRAM
INITIALISATION
set Lower to first position
set Upper to last position
set FoundIt to FALSE
get the Target name
END INITIALISATION
REPEAT
calculate the Middle position
IF Target = data at Middle position THEN
set FoundIt to TRUE
set PositionFound to Middle
ELSE
IF Target < data at Middle position THEN
set Upper to Middle – 1
ELSE
set Lower to Middle + 2
ENDIF
ENDIF
UNTIL FoundIt OR Lower > Upper
IF FoundIt THEN
report PositionFound
ELSE
report ‘Target not present’
ENDIF
END MAINPROGRAM
76
Algorithms for Sorting
Bubble Sort The task is to sort a set of data into either ascending order
or descending order as determined when the algorithm is
written. In this case the order chosen is ascending order.
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
77
Methods of Algorithm Description
Pseudocode
BEGIN MAINPROGRAM
INITIALISATION
set End to last position
END INITIALISATION
WHILE End > first position
set Current to first position
WHILE Current is less than End
IF data at Current > data at (Current + 1) THEN
Swap (Current, Current + 1)
ENDIF
increment Current
ENDWHILE
decrement End
ENDWHILE
END MAINPROGRAM
78
Methods of Algorithm Description
1 2 3 4 5 6
24 12 16 32 41 22
Unsorted|Sorted
1 2 3 4 5 6
24 12 16 32 22 41
Unsorted|Sorted
1 2 3 4 5 6
24 12 16 22 32 41
Unsorted|Sorted
1 2 3 4 5 6
22 12 16 24 32 41
Unsorted|Sorted
1 2 3 4 5 6
16 12 22 24 32 41
Unsorted|Sorted
1 2 3 4 5 6
12 16 22 24 32 41
Unsorted|Sorted
79
Methods of Algorithm Description
Pseudocode
BEGIN MAINPROGRAM
INITIALISATION
set EndUnsorted to last position
END INITIALISATION
WHILE EndUnsorted > first position
set Current to first position
set Largest to data at Current
set PositionOfLargest to Current
WHILE Current < EndUnsorted
increment Current
IF data at Current > Largest THEN
set Largest to data at Current
set PositionOfLargest to Current
ENDIF
ENDWHILE
Swap (PositionOfLargest, EndUnsorted)
decrement EndUnsorted
ENDWHILE
END MAINPROGRAM
80
Methods of Algorithm Description
Flowchart
begin
set EndUnsorted
to last position
EndUnsorted False
> first position
True
set Current to
first position
set Largest to
data at Current
Current False
< End Unsorted
True
increment Current
data at True
Current >
Largest
begin Swap
set Largest to
data at Current
False
set
Temp to data set PosOfLargest
at Position1 to Current
set
data at Position1 to
data at Position2 swap
(Position Of Largest,
EndUnsorted)
set decrement
data at Position2 EndUnsorted
to Temp
81
Methods of Algorithm Description
24 12 16 32 41 22
Unsorted|Sorted
1 2 3 4 5 6
24 12 16 32 22 41
Unsorted|Sorted
1 2 3 4 5 6
24 12 16 22 32 41
Unsorted|Sorted
1 2 3 4 5 6
24 12 16 22 32 41
Unsorted|Sorted
1 2 3 4 5 6
24 12 16 22 32 41
Unsorted|Sorted
1 2 3 4 5 6
12 16 22 24 32 41
Unsorted|Sorted
82
Methods of Algorithm Description
Pseudocode
BEGIN MAINPROGRAM
INITIALISATION
set First to first position
set Last to last position
set PositionOfNext to Last – 1
ENDINITIALISATION
WHILE PositionOfNext >= First
set Next to data at PositionOfNext
set Current to PositionOfNext
WHILE (Current < Last ) AND (Next > data at (Current + 1))
increment Current
set data at (Current – 1) to data at Current
ENDWHILE
set data at Current to Next
decrement PositionOfNext
ENDWHILE
END MAINPROGRAM
83
Methods of Algorithm Description
Flowchart
begin
set First
to first position
set Last
to last position
set PositionOfNext
to Last –1
False
PositionOfNext
>= First
True
set Current to
PositionOfNext
Current
< Last AND False
Next > data at
Current + 1
True
increment Current
decrement
PositionOfNext
end
84
A COLLECTION OF
PROBLEMS WITH NO
SOLUTIONS GIVEN
Methods of Algorithm Description
86
Methods of Algorithm Description
87