Algorithm
Algorithm
Answer:
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
Qno.2
An algorithm has been written to check that code numbers are valid on
input. They must be in the range 1000 to 9999.
Five hundred codes are being entered and the percentage of entered
codes which are incorrect is output.
There is a flowchart on the opposite page. It has some statements missing.
Complete the flowchart. Use statement numbers only, chosen from the list
below.
Qno.3
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)
Qno.4
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:
- average sound level
- highest recorded sound level.
ANSWER:
Qno.5
(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 45C 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).
ANSWER:
(b) Describe why a DAC (digital to analogue converter) would be needed if the
computer is
used to control the greenhouse environment.
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
______________________________________________________
__________
Qno.6 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
Qno.7
1 (a) All variables, constants and other identifiers should have meaningful
names.
(i) In Task 1, you had to store the midday temperatures and midnight
temperatures in arrays.
Write suitable declarations for these two arrays.
ANSWER:
(ii) It has been decided to record the temperatures for one week rather than one
month.
Write the new array declarations that you would use.
ANSWER:
(iii) Declare two other variables that you have used and state what you used
each one for.
Variable 1
...........................................................................................................................
Use
................................................................................................................................
.....
Variable 2
...........................................................................................................................
Use
................................................................................................................................
(b) Write an algorithm to complete Task 2, using either pseudocode,
programming statements
or a flowchart. Use temperatures for one week only. You should assume that
Task 1 has
already been completed.
ANSWER:
(c) Give a set of midday temperature data, for a week, that could be used to
check your validation rules for Task 1. Explain why you chose this data set.
Data set
................................................................................................................................
.....
................................................................................................................................
...................
Reason for choice
......................................................................................................................
................................................................................................................................
...................
................................................................................................................................
............
(d) Explain how you select the day with the highest midday temperature
(part of Task 3). You may include pseudocode or programming statements
as part of your explanation.
ANSWER:
Qno.8 Read this section of program code that should input 10 positive
numbers and then output the smallest number input.
1 Small = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Num = Small
6 Counter = Counter + 1
7 PRINT Small
8 UNTIL Counter < 10
There are four errors in this code.
Locate these errors and suggest a corrected piece of code for each error.
Answer:
Qno.9
The flowchart below inputs the weight of a number of parcels in kilograms.
Parcels weighing more than 25 kilograms are rejected. A value of 1 stops
the input.
The following information is output: the total weight of the parcels
accepted and number of parcels rejected.
Qno.10 Five data types and five data samples are shown below.
Draw a line to link each data type to the correct data sample.
Qno.11. (a) All variables, constants and other identifiers should have
meaningful names.
(i) Declare the array to store the pupils names.
................................................................................................................................
.......[1]
(ii) Declare the array to store the pupils weights.
................................................................................................................................
.......[1]
(iii) It has been decided to record the weights for the whole school of 600 pupils
rather than
one class. Write suitable new declarations for these two arrays.
................................................................................................................................
...........
................................................................................................................................
.......[1]
(b) Write an algorithm to complete Task 2, using either pseudocode,
rogramming statements or a flowchart. Use weights for the whole school.
You should assume that Task 1 has already been completed.
ANSWER:
................................................................................................................................
...........
Weight 2
............................................................................................................................
Reason for choice
.............................................................................................................
................................................................................................................................
...
(d) Explain how you select the pupils with a fall in weight of more than 2.5
kilograms (part of
Task 3). You may include pseudocode or programming statements as part of
your explanation.
ANSWER:
Qno.13 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 this problem in the form of a flowchart.
Qno.16
Write an algorithm in the form of a flowchart which:
Qno.17 A shop sells books, maps and magazines. Each item is identified
by a unique 4 digit code. All books have a code starting with 1, all maps
have a code starting with 2 and all magazines have a code starting with
4. The code 9999 is used to end the algorithm.
Write an algorithm in the form of a flowchart which inputs the codes for all
items in stock and outputs the number of books, number of maps and the
number of magazines in stock. Include any validation checks needed.
Qno.18 This algorithm inputs 3 numbers, each number goes through successive
division by 10 until its value is less than 1. An output is produced which contains
the number input and a value generated by the flowchart processing.
Data to be used: X = 85, 3190, -40
Output T, N
Qno.19 This algorithm inputs 5 values and outputs how many input numbers were
negative and how many were positive.
Data to be used: N = 1, -5, 2, -8, -7
Trace Table
C
neg
pos
Qno.20 This algorithm inputs the number of hours of sunshine recorded each day
for a week (7 days). The output is the highest value for hours of sunshine and the
average (mean) value for the numbers of hours of sunshine per day.
Data to be used: hours = 9.0, 7.8, 1.2, 4.5, 10.0, 6.4, 3.1
Trace Table
C
hours
high
total
avge
Qno.21 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:
currency value = number of Reais X conversion rate
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 a flowchart, 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.
Qno.22 As part of an experiment, a school measured the heights (in metres) of all its
500 students. Write an algorithm, using a flowchart, which inputs the heights of all 500
students and outputs the height of the tallest person and the shortest person in the
school.
Qno.24 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 a flowchart, which inputs the 3 digit code for all 280 items
and outputs the number of cards, sweets, stationery and toys.
Qno.25 A company are carrying out a survey by observing traffic at a road junction.
Each time a car, bus or lorry passed by the road junction it was noted down. 10 000
vehicles were counted during the survey.
\Write an algorithm, using an algorithm, which:
Qno.26 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:
100
speed = (metres/sec) (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.
Write an algorithm, using a flowchart, 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
Qno.28 Draw the trace table and determine the output from the following flowchart
using the following data:
number = 45, -2, 20.5
Qno. 29 Draw the trace table and determine the output from the following flowchart
using the following data (NOTE: input of the word end stops the program and outputs
results of the survey):
vehicle = car, car, lorry, bus, van, van, car, car, bus, car, end
Qno.30 Draw the trace table and determine the output from the following flowchart
using the following data:
X = 5, -3, 0, -3, 7, 0, 6, -11, -7, 12
Qno.31 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
2
3
4
5
6
7
8
9
negative = 1: positive = 1
for x = 0 to 1000
input number
if number < 0 then negative = negative + 1
if number > 0 then positive = positive + 1
endif
endif
next
print negative, positive
Qno.32 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
2
3
4
5
6
7
8
9
10
11
highest = 1000
days = 1
while days > 0
input rainfall
if rainfall > highest then highest = rainfall
Endif
total = total + rainfall
days = days + 1
average = total/500
endwhile
print average, highest
input n
for mult = 1 to n
sum = 0
sum = sum * mult
result = n/sum
next
print result
Qno.34 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:
currency value = number of Reais X conversion rate
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.
Qno.35 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.
Qno.37 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.
Qno.38 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:
27
Qno.39 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:
100
speed = (metres/sec) (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.
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