2.2 Programmingo
2.2 Programmingo
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]
1 extracost = 0
2 input numberpeople, numbergames
3 charge = 10 * numbergames
4 extrapeople = numberpeople – 2
5 if numberpeople< 2 then extracost = 2 * extrapeople * numbergames
6 charge = extracost
7 if numbergames> 1 then charge = charge * 0.1
8 print charge
There are three errors in the program. Locate these errors and suggest a correct piece of coding.
[6]
Page 1 of 38
Computer Science 2210
Topical Past Papers
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
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
Page 3 of 38
Computer Science 2210
Topical Past Papers
Question 01: Input and store the names and marks for 30 students who have sat three computer
science tests. Test 1 is out of 20 marks, Test 2 is out of 25 marks, Test 3 is out of 35 marks. You must
store the names in a one-dimensional array and the marks and total score for each student in
one-dimensional arrays. All the marks must be validated on entry and any invalid marks rejected.
You may assume that the students’ names are unique.
Question 02: Calculate and store the total score for each student and calculate the average
score for the whole class. Output each student’s name followed by their total score, then output
the average score for the class.
Question 03: Select the student with the highest score and output their name and score.
Your program must include appropriate prompts for the entry of data. Error messages and other
output need to be set out clearly and understandably. All variables, constants and other
identifiers must have meaningful names. Each task must be fully tested.
Question 04: Write a pseudocode using array, asks user to input 10 numbers, then calculates the
average of those numbers. Loop is used in this problem.
Question 05: Write a pseudocode to swap first and last element of an integer 1-d array.
Question 06: Write a pseudocode to find the largest and smallest element of an array.
Page 4 of 38
Computer Science 2210
Topical Past Papers
(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 5 of 38
Computer Science 2210
Topical Past Papers
(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:
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 6 of 38
Computer Science 2210
Topical Past Papers
The maximum allowed speed is 100 kilometres per hour. 500 vehicles were monitored using these
cameras over a 1 hour period.
Write an algorithm, using pseudocode, which:
• inputs the start time and end time for the 500 vehicles that were monitored
• calculate the speed for each vehicle using the formula above
• outputs the speed for each vehicle and also a message if the speed exceeded 100
km/hour
• output the highest speed of all the 500 vehicles monitored
(10)
100
𝑠𝑝𝑒𝑒𝑑 = (𝑚𝑒𝑡𝑒𝑟𝑠/𝑠𝑒𝑐𝑜𝑛𝑑)
(time at point B – time at point A)
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!!).
A discount of 10% is given if 3 or more passengers are travelling together.
Write an algorithm, using pseudocode, which:
• inputs the number of passengers travelling
• inputs the station number of the starting point and the station number of the destination
• calculates the total fare taking into account the direction of travel
• calculates any discount due
• outputs the cost of the tickets and prints the tickets
Page 7 of 38
Computer Science 2210
Topical Past Papers
Calculate the BMI for a person whose weight is 80kg and height is 2 metres. [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]
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 unique5-digit
code with the first digit identifying the type of item, i.e.
1 = CD
2 = DVD
3 = video
4 = book
For example, for the code 15642 the 1 identifies that it is a CD, and for the code 30055 the3
identifies that it is a video.
Write an algorithm, using pseudocode or otherwise, that
• Inputs the codes for all 5000 items
• Validates the input code
• Calculates how many CDs, DVDs, videos and books are in stock
• Outputs the four totals. [5]
Page 8 of 38
Computer Science 2210
Topical Past Papers
What would be the Fuel Economy of a car travelling 40 km on 10 litres 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 (litres) 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
12 Algorithms and programs use loops to control the number of times a particular procedure
isused.
Two methods are repeat … until and for … to.
(a) Write a procedure using both these loop methods to input 20 numbers into a variable called x.
(i) repeat … until [2]
(ii) for … to [2]
(b) Name another loop structure. [1]
Page 9 of 38
Computer Science 2210
Topical Past Papers
May/June 2009
18 A small airport handles 400 flights per day from three airlines:
FASTAIR (code FA)
SWIFTJET (code SJ)
KNIGHTAIR (code KA)
Each flight is identified by the airline code and 3 digits. For example FA 156.
Write an algorithm, using pseudocode or otherwise, which monitors the 400 flights into and out of
the airport each day. The following inputs, processing and outputs are all part of the monitoring
process:
• input flight identification
• calculate number of flights per day for each of the three airlines
• output the percentage of the total flights per day by each airline
• any validation checks must be included [5]
What is the final speed of a car if it takes 2 hours to get from A to B? [1]
Page 10 of 38
Computer Science 2210
Topical Past Papers
(b) Describe, with examples, two sets of test data you would use to test your algorithm.
[2]
(b) Describe, with examples, TWO sets of test data you would use to test your algorithm.
[2]
Page 12 of 38
Computer Science 2210
Topical Past Papers
Page 13 of 38
Computer Science 2210
Topical Past Papers
16 A small shop uses barcodes which represent 5 digits. The last digit is used as a check digit.
For example:
a b c d e
0 1 2 3 4
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
eightdigits. 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-digitmobile
phone calls made during the day and outputs the number of calls made on each ofthe four
networks. [6]
Page 14 of 38
Computer Science 2210
Topical Past Papers
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), 3digits
(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]
15 A survey is being carried out which involves reading and recording sound levels near a busy
road junction. Once all the data are collected, they are input manually into a computer. A sound
level of 0 decibels (0 dB) is input to indicate the end of the data.
Write an algorithm, using pseudocode or a flowchart, which:
• inputs all the sound levels
• after a sound level of 0 is input, outputs the following:
o average sound level
o highest recorded sound level.
Page 15 of 38
Computer Science 2210
Topical Past Papers
18 A school has 1500 students. It is conducting a survey on their music preferences. Each student
uses a computer and inputs their name and then chooses one of 5 options:
• rock (input value 1)
• soul (input value 2)
• pop (input value 3)
• jazz (input value 4)
• classical (input value 5)
Write an algorithm, using pseudocode or a flowchart, which:
• inputs the choice of all 1500 students (values 1 to 5)
• outputs all the names of the students who chose classical music
• outputs the percentage who chose each option
Page 16 of 38
Computer Science 2210
Topical Past Papers
For example, to move block “X” from square A7 to F7 (beginning at START) would require
the following instructions:
Left 8
Down 6
Close
Up 6
Right 5
Down 6
Open
Write a set of instructions to transfer block “Y” from C6 to G5 (beginning at START). [3]
Page 17 of 38
Computer Science 2210
Topical Past Papers
Page 18 of 38
Computer Science 2210
Topical Past Papers
3 Write a routine using a for … to loop which inputs 100 numbers and outputs how many of
the numbers were negative. [3]
Page 19 of 38
Computer Science 2210
Topical Past Papers
Page 20 of 38
Computer Science 2210
Topical Past Papers
Page 21 of 38
Computer Science 2210
Topical Past Papers
Page 22 of 38
Computer Science 2210
Topical Past Papers
Page 23 of 38
Computer Science 2210
Topical Past Papers
Page 24 of 38
Computer Science 2210
Topical Past Papers
Complete the set of instructions to draw the shape shown above (in bold lines).
Page 25 of 38
Computer Science 2210
Topical Past Papers
Page 26 of 38
Computer Science 2210
Topical Past Papers
Page 27 of 38
Computer Science 2210
Topical Past Papers
Page 28 of 38
Computer Science 2210
Topical Past Papers
Page 29 of 38
Computer Science 2210
Topical Past Papers
Page 30 of 38
Computer Science 2210
Topical Past Papers
Page 31 of 38
Computer Science 2210
Topical Past Papers
Page 32 of 38
Computer Science 2210
Topical Past Papers
Page 33 of 38
Computer Science 2210
Topical Past Papers
Page 34 of 38
Computer Science 2210
Topical Past Papers
Page 35 of 38
Computer Science 2210
Topical Past Papers
Page 36 of 38
Computer Science 2210
Topical Past Papers
Page 37 of 38
Computer Science 2210
Topical Past Papers
Page 38 of 38