Worksheet 3 Iteration
Unit 11 Programming techniques
Worksheet 3 Iteration
Task 1
1. Complete the trace table to determine the purpose of the following algorithm. Test it with
input 14 and 5.
x = input ("Enter the first integer: ")
y = input ("Enter the second integer: ")
z = 0
while x > 0
if x mod 2 == 1 then
z = z + y
endif
x = x div 2
y = y * 2
endwhile
print ("Answer =", z)
x y x mod 2 z x>0 output
14 5 0 0 True
7 10 1 10 true
3 20 1 30 true
1 40 1 70 true
0 80 0 70 false Answer=70
1
Worksheet 3 Iteration
Unit 11 Programming techniques
2. A doctor records a patient’s temperature once an hour for six hours. Any time the
temperature is > 37C, an incidence of fever is recorded.
The average temperature is calculated at the end.
(a) Calculate the expected result using test data 36, 36, 38.5, 37, 38, 36.
(b) Complete the trace table using the pseudocode below for this data.
temp = 0
fever = 0
total = 0
hour = 1
while hour < 7
temp = input(“Enter temperature: ”)
if temp > 37 then
fever = fever + 1
endif
total = total + temp
hour = hour + 1
endwhile
average = round(total/hour,1) #round to 1 decimal place
print(“Average temperature:”, average)
print(“Incidents of fever:”, fever)
temp fever total hour average Output
0 1 0 0 0
36 36 1 36
36 72 2 36
38.5 2 110.5 3 36.8
37 147.5 4 36.9
38 3 185.5 5 37.1
36 221.5 6 36.9 Avg temp is 36.9
3
(c) Is the result correct? If not, make changes to the pseudocode so that it gives the
correct result.
(d) Rewrite the pseudocode to include a range check to ensure that a temperature is
between 30 and 44. Produce an error message for invalid data. The program should
allow the user to re-enter the temperature if it is out of range.
Task 2
2
Worksheet 3 Iteration
Unit 11 Programming techniques
3. A parts supply company uses 4-digit part numbers. The last digit indicates the production
run. If the production run is 6,7 or 8 it is considered to be an old model.
Write a pseudocode algorithm that prompts the user to enter a part number.
The length of the part number should be equal to 4 digits, otherwise an error message will
be displayed and the user will be prompted to input the part number again.
The algorithm should count the total number of parts entered and the number of old model
parts and output these totals.
Data input will terminate when the user inputs 9999.
BEGIN
SET total_parts = 0
SET old_model_parts = 0
REPEAT
PROMPT user to enter a part number
READ part_number
IF part_number equals 9999 THEN
BREAK
IF part_number is not a 4-digit number THEN
DISPLAY "Error: Part number must be 4 digits. Please enter again."
ELSE
INCREMENT total_parts by 1
EXTRACT the last digit of part_number as last_digit
IF last_digit is 6 OR last_digit is 7 OR last_digit is 8 THEN
INCREMENT old_model_parts by 1
END IF
END IF
UNTIL part_number equals 9999
DISPLAY "Total number of parts entered: " total_parts
DISPLAY "Total number of old model parts: " old_model_parts
END
4. What is a common cause of an accidental infinite loop?
A common cause of an accidental infinite loop is forgetting to update the loop
condition or not having a proper exit condition. This makes the loop run
forever because the condition to stop the loop is never met.
Task 3
5. A teacher has a class of 30 pupils. Each pupil has taken 3 tests during the year. The
teacher needs to know the average class score for test1, test2 and test3. She also needs
to know the overall average test score for the year. Write an algorithm in pseudocode that
will allow the teacher to input all results and print this information.
6. A Hallowe’en display needs a computer controlled light which will flicker. Flicker the light
for a random number of seconds between 1/10 and 1/100 of a second. You can use a
pause function that takes as a parameter the number of milliseconds to pause the program.
For example pause(1000) will pause the program for 1 second. To turn the light on and off,
3
Worksheet 3 Iteration
Unit 11 Programming techniques
set the value of light to HIGH for ON and LOW for OFF. The control loop should run
continuously.