3GL Pseudocode algorithms

Exercise 1

 Given a classroom full of people: All measurements should be in metres.

This is quite an interesting little problem which highlights the need to initialise variables.

What processes do we have?

It is easy to think that you need to know how many people are in the class before you start. This isn't really necessary since we can use a decision - Is this the last person? - to control the overall process of getting each person's height.

What decisions do we have to make?

What loops do we have? It looks like one main loop - in this we get a height, increment the number of persons, calculate  the average so far and determine tallest and smallest.

What variables do we have?

My first attempt at an algorithm is:

DOWHILE NOT Last_Person
 INPUT Current_Height
 Number_Of_Persons = Number_Of_Persons + 1
 Sum_Of_Heights = Sum_Of_Heights + Average_Height
 Average_Height = Sum_Of_Heights / Number_Of_Persons
 IF Current_Height > Tallest   THEN Tallest = Current_Height
 IF Current_Height < Smallest  THEN Smallest = Current_Height
END DOWHILE

You can see that I "discovered" anothe variable - Sum_Of_Heights. This is needed so that Average_Height can be calculated.

You can also see that a number of variables have been used that are not set to an initial state, that is a known state. This can be quite a dangerous thing to do in a program. The variables Number_Of_Persons and Sum_Of_Heights are both used on the left-hand side and the right-hand side of an expression. This is quite safe if the contents of these variables is known accurately but what about when the while loop executes for the first time? What values would Number_Of_Persons and Sum_Of_Heights contain before the while loop starts? If the answer to this question "Don't know" then the variables must be initialised. The same is true of Tallest and Smallest. There is no real problem with uninitialised variables in any given program design or algorithm but if you don't specify initialisation in the design you can't blame the programmer for not doing it when the program is written. Don't trust the programming language to take care of initialising variables for you.

What happens in the first execution of the statements:

 IF Current_Height > Tallest   THEN Tallest = Current_Height
 IF Current_Height < Smallest  THEN Smallest = Current_Height

For all we know Tallest might be set randomly at 1000 metres and Smallest at 0.5 metres. Again the variables must be initialised.

Here is the algorithm with initialisation:

Number_Of_Persons = 0
Sum_Of_Heights = 0
Tallest = 0
Smallest = 5
DOWHILE NOT Last_Person
 INPUT Current_Height
 Number_Of_Persons = Number_Of_Persons + 1
 Sum_Of_Heights = Sum_Of_Heights + Average_Height
 Average_Height = Sum_Of_Heights / Number_Of_Persons
 IF Current_Height > Tallest   THEN Tallest = Current_Height
 IF Current_Height < Smallest  THEN Smallest = Current_Height
END DOWHILE

Tallest is initialised to 0 metres because you know every one is taller than 0 metres, Smallest is set to 5 metres because you know no one is taller than 5 metres.
 


Return to the lesson


and may not be reproduced by any means without the written
 

 

This publication is copyright David Beech and Learning Systems 1997-2002
and may not be reproduced by any means without the written permission of
David Beech.
9 Wyndella Street, Tasmania, Australia


db@codelearn.com