0% found this document useful (0 votes)
43 views

Programming techniques T3 Worksheet 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Programming techniques T3 Worksheet 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 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.
(36+36+38.5+37+38+36)/6 = 36.92

(b) Complete the trace table using the pseudocode below for this data.

temp = 0
fever = 0
total = 0
hour = 1 should be hour = 0
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 0 0 1 0
36 0 36 2 0
36 0 72 3 0
38.5 1 110.5 4 0
37 0 147.5 5 0
38 2 185.5 6 0
36 2 221.5 7 0
Average temperature:
2 36.9 36.9
Incidents of fever: 2

(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.
temp = input(“Enter temperature: ”
2
Worksheet 3 Iteration
Unit 11 Programming techniques
while temp > 44 and temp < 36 then
print(“Please enter a value between 30 and 44”)
temp = input(“Enter temperature: ”
endwhile

Task 2

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.
Totalparts = 0
Oldparts = 0

part = int(input(“Please enter the part number: ”))


while part != 9999 then
while part.length() > 4 or part.length < 4 then
print(“Please enter a part number of 4 digits”)
part = int(input(“Please enter the part number: ”))
endwhile
if part[3] == 6 or part[3] == 7 or part[3] == 8 then
Oldparts += 1
else then
Totalparts += 1
endif
print(“The total number of parts entered is:”, Totalparts, “, out of which”, Oldparts , “are
oldparts.”)

4. What is a common cause of an accidental infinite loop?


Not correctly setting the condition, for example putting == instead of !=

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.
3
Worksheet 3 Iteration
Unit 11 Programming techniques

pupils = 0
totaltest1 = 0
totaltest2 = 0
totaltest3 = 0
while pupils != 30 then
score_t1 = int(input(“enter the score for test 1:”))
score_t2 = int(input(“enter the score for test 2:”))
score_t3 = int(input(“enter the score for test 3:”))
pupils += 1
totaltest1 += totaltest1
totaltest2 += totaltest2
totaltest3 += totaltest3
endwhile

avtest1 = totaltest1/30
avtest2 = totaltest2/30
avtest3 = totaltest3/30
total = totaltest1 + totaltest2 + totaltest3
avtotal = total/3

print(“The average for test 1 is:”, avtest1, “The average for test 2 is:”, avtest2, “The average
for test 3 is:”, avtest3, “The average for the three tests overall is:”, avtotal)

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, set the value of light to HIGH for ON and LOW for OFF. The control loop should run
continuously.

on = True
while light then
light = HIGH
pause(10,100)
light = LOW
pause(10,100)
endwhile

4
Worksheet 3 Iteration
Unit 11 Programming techniques

You might also like