Pseudocode For O Level Computer Science
Pseudocode For O Level Computer Science
2.1.2 Pseudocode
The following five examples use the above pseudocode terms. These are the same problems discussed in
section 3.1 using flow charts – both methods are acceptable ways of representing an algorithm.
2.1 Example 1
A town contains 5000 houses. Each house owner must pay tax based on the value of the house. Houses over
$200 000 pay 2% of their value in tax, houses over $100 000 pay 1.5% of their value in tax and houses over
$50 000 pay 1% of their value in tax. All others pay no tax.
Write an algorithm to solve the problem using pseudocode.
Notes:
(1) a while loop or a repeat loop would have worked just as well
(2) the use of endif isn’t essential in the pseudocode
For example,
count 0
while count < 5001
input house
if house > 50000 then tax house * 0.010
else if house > 100 000 then tax house * 0.015
else if house > 200 000 then tax house * 0.020
else tax 0
endif
endif
endif
print tax
count count + 1
endwhile
EXERCISE: Re-write the above algorithm using a repeat loop and modify the if … then … else statements
to include both parts of the house price range.
(e.g. if house > 50000 and house <= 100000 then tax = house * 0.01)
Page 10 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
2.2 Example 2
NOTE: It is much easier in this example to input x first and then loop round doing the calculation until
eventually x = 0. Because of this, it would be necessary to input x twice (i.e. inside the loop and outside the
loop). If input x occurred only once it would lead to a more complicated algorithm.
(Also note in the algorithm that <> is used to represent ≠ “not equals to” ).
A while loop is used here, but a repeat loop would work just as well.
input x
while x <> 0 do
if x = 1 then print “error”
else n (x * x)/(1 – x)
print n, x
endif
input x
endwhile
2.3 Example 3
Write an algorithm using pseudocode which takes temperatures input over a 100 day period (once per day)
and output the number of days when the temperature was below 20C and the number of days when the
temperature was 20C or above.
(NOTE: since the number of inputs is known, a for … to loop can be used. However, a while loop or a repeat
loop would work just as well).
total1 0 : total2 0
for days 1 to 100
input temperature
if temperature < 20 then total1 total1 + 1
else total2 total2 + 1
endif
next
This is a good example of an algorithm that could be written using the case construct rather than if … then …
else. The following section of code replaces the statements if temperature < 20 then …… endif:
case temperature of
1: total1 = total1 + 1
2: total2 = total2 + 1
endcase
Page 11 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
2.4 Example 4
Write an algorithm using pseudocode which:
(NOTE: Again since the actual number of data items to be input is known any one of the three loop structures
could be used. It is necessary to set values for the fastest (usually set at zero) and the slowest (usually set at an
unusually high value) so that each input can be compared. Every time a value is input which > the value stored
in fastest then this input value replaces the existing value in fastest; and similarly for slowest).
fastest 0: count 0
slowest 1000
repeat
input top_speed
total total + top_speed
if top_speed > fastest then fastest top_speed
if top_speed < slowest then slowest top_speed
endif
endif
count count + 1
until count = 5000
average total * 100/5000
print fastest, slowest, average
Page 12 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
2.5 Example 5
A shop sells books, maps and magazines. Each item is identified by a unique 4 – digit code. All books have a
code starting with a 1, all maps have a code starting with a 2 and all magazines have a code beginning with a
3. The code 9999 is used to end the program.
Write an algorithm using pseudocode which input the codes for all items in stock and outputs the number of
books, maps and magazine in stock. Include any validation checks necessary.
(NOTE: A 4-digit code implies all books have a code lying between 1000 and 1999, all maps have a code
lying between 2000 and 2999 and all magazines a code lying between 3000 and 3999. Anything outside this
range is an error)
(NOTE: A function called INT(X) is useful in questions like this. This returns the integer (whole number) part
of X e.g. if X = 1.657 then INT(X) = 1; if X = 6.014 then INT(X) = 6 etc. Using this function allows us to use
the case statement to answer this question:
(This is probably a more elegant but more complex solution to the problem)
Page 13 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
Calculate the BMI for a person whose weight is 80kg and height is 2 meters. [1]
(b) Using pseudocode or otherwise, write an algorithm that will input the ID, weight (kg) and height (m) of 30
students, calculate their body mass index (BMI) and output their ID, BMI and a comment as follows:
A BMI greater than 25 will get the comment ‘OVER WEIGHT’, a BMI between 25 and 19 (inclusive) will
get ‘NORMAL’ and a BMI less than 19 will get ‘UNDER WEIGHT’. [6]
Oct/Nov 2006
9. A computer program is required which inputs 10 numbers, multiplies them together and finally outputs the
answer (the product). The following algorithm has been written to do this.
1 count = 0
2 product = 0
3 while count <= 10 do
4 input number
5 product = product * number
6 count = count + 1
7 print product
8 endwhile
(a) There are three errors in the algorithm. Locate and describe these errors. [3]
(b) A while … do loop has been used in the algorithm. State another type of loop that could have been used.
[1]
20. Temperatures (°C) are being collected in an experiment every hour over a 200 hour period. Write an
algorithm, using pseudocode or otherwise, which inputs each temperature and outputs
how many of the temperatures were above 20°C
how many of the temperatures were below 10°C
the lowest temperature that was input [5]
May/June 2007
19. A company has 5000 CDs, DVDs, videos and books in stock. Each item has a unique 5-digit code with the
first digit identifying the type of item, i.e.
1 = CD
2 = DVD
Page 14 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
3 = Video
4 = Book
For example, for the code 15642 the 1 identifies that it is a CD, and for the code 30055 the 3 identifies that it
is a video.
Write an algorithm, using pseudocode or otherwise, that
Oct/Nov 2007
16. (a) Fuel economy for a car is found using the formula:
What would be the Fuel Economy of a car travelling 40 km on 10 liters of fuel? [1]
(b) The Fuel Economy for 1000 cars is to be calculated using the formula in Question 16(a).
Write an algorithm, using pseudocode or otherwise, which inputs the Distance Travelled (km) and the Fuel
Used (liters) for 1000 cars. The Fuel Economy for each car is then calculated and the following outputs
produced:
Fuel Economy for each car
average (mean) Fuel Economy for all of the cars input
the best Fuel Economy (i.e. highest value)
the worst Fuel Economy (i.e. lowest value) [6]
May/June 2008
19. Customers can withdraw cash from an Automatic Teller Machine (ATM).
withdrawal is refused if amount entered > current balance
withdrawal is refused if amount entered > daily limit
if current balance < $100, then a charge of 2% is made
if current balance $100, no charge is made
Write an algorithm which inputs a request for a sum of money, decides if a withdrawal can be made and
calculates any charges. Appropriate output messages should be included.
[5]
Oct/Nov 2008
19. The manufacturing cost of producing an item depends on its complexity. A company manufactures three
different types of item, with costs based on the following calculations:
Page 15 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
What is the final speed of a car if it takes 2 hours to get from A to B? [1]
Page 16 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
(b) Write an algorithm, using pseudocode or otherwise, which inputs the times for 500 cars, calculates the
final speed of each car using the formula in part (a), and then outputs:
the final speed for ALL 500 cars
the slowest (lowest) final speed
the fastest (highest) final speed
the average final speed for all the cars. [6]
1 negative = 1
2 positive = 1
3 for count = 1 to 20 do
4 input number
5 if number < 0 then negative = negative + 1
6 if number > 0 then positive = positive + 1
7 count = count + 1
8 print negative, positive
9 next count
Page 17 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
17. Daniel lives in Italy and travels to Mexico, India and New Zealand. The times differences are:
(b) Describe, with examples, two sets of test data you would use to test your algorithm.
[2]
Page 18 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
(b) Describe, with examples, TWO sets of test data you would use to test your algorithm.
[2]
Oct/Nov 2011. P11
17. (a) Write an algorithm, using pseudocode or flowchart only, which:
inputs three numbers
outputs the largest of the three numbers
16. The weather conditions in a town are being monitored over a year (365 days). The values recorded per day
are weather type and temperature (e.g. CLOUDY, 25).
Write an algorithm, using pseudocode or flowchart only, which:
inputs the weather type and temperature for each day
outputs the number of days that were CLOUDY, RAINING, SUNNY or FOGGY
outputs the highest recorded temperature for the year
outputs the lowest recorded temperature for the year
Page 19 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
17. (a) Write an algorithm, using pseudocode or a program flowchart only, that:
inputs a series of positive numbers (-1 is used to terminate the input),
outputs how many numbers were less than 1000 and
outputs how many numbers were greater than 1000. [4]
Page 20 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
16. A small shop uses barcodes which represent 5 digits. The last digit is used as a check digit.
For example:
abcde
01234
The check digit (e) is found by:
multiplying the first and third digits (i.e. a and c) by 3
multiplying the second and fourth digits (i.e. b and d) by 2
adding these four results together to give a total
dividing this total by 10
remainder is check digit (e)
Write an algorithm, using pseudocode or flowchart only, which
inputs 100 five-digit barcodes in the form a, b, c, d, e
re-calculates the check digit for each number and checks whether the input check digit (e) is correct
outputs the number of barcodes which were entered correctly [5]
17. A country has four mobile phone network operators. Each mobile phone number has eight digits. The first
three digits identify the network operator:
444 Yodafone
555 N2 network
666 Kofee mobile
777 Satsuma mobile
Write an algorithm, using pseudocode or flowchart only, which reads 50 000 eight-digit mobile phone calls
made during the day and outputs the number of calls made on each of the four networks.
[6]
Page 21 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
8. A piece of pseudocode was written to input 1000 positive numbers and then output the highest and lowest
numbers.
10 highest = 0
20 lowest = 0
30 for count = 1 to 100
40 input number
50 if number > highest then number = highest
60 if number < lowest then number = lowest
70 count = count + 1
80 next count
90 print highest, lowest
16. (a) A greenhouse is being monitored by a computer using 2 sensors. SENSOR1 measures the temperature
and SENSOR2 measures oxygen levels.
If the temperature exceeds 45°C or oxygen levels fall below 0.19, then an error message is output by the
computer.
Write an algorithm, using pseudocode or flowchart only, which
inputs both sensor readings
checks the sensor input values and outputs a warning message if either are out of range
continues monitoring until the <ESCAPE> key is pressed
(You may assume that READ SENSORn will take a reading from SENSORn and that
READ KEY inputs a key press from the keyboard). [5]
15. 5000 numbers are being input which should have either 1 digit (e.g. 5), 2 digits (e.g. 36), 3 digits (e.g.
149) or 4 digits (e.g. 8567).
Write an algorithm, using pseudocode or flowchart only, which
inputs 5000 numbers
outputs how many numbers had 1 digit, 2 digits, 3 digits and 4 digits
outputs the % of numbers input which were outside the range [6]
Page 22 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
Other Questions:
Questions 1 to 3 contain sections of pseudocode which contain errors. Locate the errors and suggest the
correct coding. Questions 4 to 10 are problems which require an algorithm to be written in pseudocode – there
is “no right answer” here; as long as the pseudocode works then the solution is acceptable.
(1) The following section of pseudocode inputs 1000 numbers and then outputs how many were negative, how
many were positive and how many were zero.
Locate the 3 errors and suggest a corrected piece of code.
1 negative 1: positive 1
2 for x 0 to 1000
3 input number
4 if number < 0 then negative negative + 1
5 if number > 0 then positive positive + 1
6 endif
7 endif
8 next
9 print negative, positive
(2) The following section of pseudocode inputs rainfall (in cm) for 500 days and outputs the average rainfall
and the highest rainfall input.
Locate the 3 errors and suggest a corrected piece of code.
1 highest 1000
2 days 1
3 while days > 0
4 input rainfall
5 if rainfall > highest then highest rainfall
6 endif
7 total total + rainfall
8 days days + 1
9 average total/500
10 endwhile
11 print average, highest
(3) The following section of pseudocode inputs a number, n, multiplies together 1 x 2 x 3 x ……. x n,
calculates input number/sum and outputs result of the calculation.
Locate the 3 errors and suggest a corrected piece of code.
1 input n
2 for mult 1 to n
3 sum 0
4 sum sum * mult
5 result n/sum
6 next
7 print result
Page 23 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
(4) Regis lives in Brazil and often travels to USA, Europe and Japan. He wants to be able to convert Brazilian
Reais into US dollars, European euros and Japanese yen. The conversion formula is:
For example, if Regis is going to USA and wants to take 1000 Reais (and the exchange rate is 0.48) then he
would input USA, 1000 and 0.48 and the output would be: 480 US dollars.
Write an algorithm, using pseudocode, which inputs the country he is visiting, the exchange rate and the
amount in Brazilian Reais he is taking. The output will be value in foreign currency and the name of the
currency.
(5) As part of an experiment, a school measured the heights (in metres) of all its 500 students.
Write an algorithm, using pseudocode, which inputs the heights of all 500 students and outputs the height of
the tallest person and the shortest person in the school.
(6) A geography class decide to measure daily temperatures and hours of sunshine per day over a 12 month
period (365 days)
Write an algorithm, using pseudocode, which inputs the temperatures and hours of sunshine for all 365 days,
and finally outputs the average (mean) temperature for the year and the average (mean) number of hours per
day over the year.
(7) A small shop sells 280 different items. Each item is identified by a 3 – digit code. All items that start with
a zero (0) are cards, all items that start with a one (1) are sweets, all items that start with a two (2) are
stationery and all items that start with a three (3) are toys.
Write an algorithm, using pseudocode, which inputs the 3 – digit code for all 280 items and outputs the
number of cards, sweets, stationery and toys.
(8) A company are carrying out a survey by observing traffic at a road junction. Each time a car, bus, lorry or
other vehicle passed by the road junction it was noted down.
10 000 vehicles were counted during the survey.
Write an algorithm, using pseudocode, which:
inputs all 10000 responses
outputs the number of cars, buses and lorries that passed by the junction during the survey
outputs the number of vehicles that weren’t cars, buses or lorries during the survey
Page 24 of 25
harris_akhtar@yahoo.com 0333-5154649
2.1 Algorithm Design & Problem – Solving
2.1.2 Pseudocode
(9) Speed cameras read the time a vehicle passes a point (A) on the road and then reads the time it passes a
second point (B) on the same road (points A and B are 100 metres apart). The speed of the vehicle is
calculated using:
The maximum allowed speed is 100 kilometres per hour. 500 vehicles were monitored using these cameras
over a 1 hour period.
The train travels in both directions (i.e. from 1 to 10 and then from 10 to 1). The fare between each station is
$2.
A passenger inputs the number of the station at the start of his journey and the number of the destination
station and the fare is calculated (e.g if a passenger gets on a station 3 and his destination is station 9 his fare
will be $12). The calculation must take into account the direction of the train (e.g. a passenger getting on at
station 7 and getting off at station 1 will also pay $12 and not a negative value!!).
The maximum allowed speed is 100 kilometres per hour. 500 vehicles were monitored using these cameras
over a 1 hour period.
Page 25 of 25
harris_akhtar@yahoo.com 0333-5154649