Programming techniques Iteration worksheet 2
Programming techniques Iteration worksheet 2
Homework 3 Iteration
1. Write a pseudocode algorithm using a for … next loop to read five lowercase letters and
output the largest and smallest. (a is less than b). [6]
length = word.length()
next i
2. Write a pseudocode algorithm that asks a user for a password. They are allowed three
attempts to type the correct password, which is “Tues1212”.
If they type the correct password, output “Password accepted ”, otherwise output “Password
rejected”. [6]
password = “Tues1212”
count = 0
1
Homework 3 Iteration
Unit 11 Programming techniques
3. (a) Complete the trace table below with the values supplied.
sunshine = 0
maxHours = 0
minHours = 100
totalSunshine = 0
repeat
sunshine = input(“Input hours of sunshine: ”)
if sunshine > maxHours then
maxHours = sunshine
endif
if sunshine < minHours then
minHours = sunshine
endif
totalSunshine = totalSunshine + sunshine
until sunshine = -1
print(“Max sunshine hours: ”, maxHours)
print(“Min sunshine hours: ”, minHours)
print(“Total sunshine hours: ”, totalSunshine)
Test Data: 2 7 3 8 -1
sunshin totalSunshin
maxHours minHours Output
e e
0 0 100 0
2 2 2 2
7 7 9
3 12
8 8 20
-1 8 2 20
Max
sunshine
hours: 8
Min
sunshine
hours: 2
Total
sunshine
hours: 20
2
Homework 3 Iteration
Unit 11 Programming techniques
The variable sunshine is not cast to an integer.
(c)
This time the algorithm uses an entry condition WHILE loop. Complete the trace table to see
the difference between the two.
sunshine = 0
maxHours = 0
minHours = 100
totalSunshine = 0
3
Homework 3 Iteration
Unit 11 Programming techniques
sunshine = input(“Input hours of sunshine: ”)
while sunshine <> -1
if sunshine > maxHours then
maxHours =sunshine
endif
if sunshine < minHours then
minHours = sunshine
endif
totalSunshine =sunshine + totalSunshine
sunshine =INPUT
endwhile
print(“Max sunshine hours: ”, maxHours)
print(“Min sunshine hours: ”, minHours)
print(“Total sunshine hours: ”, totalSunshine)
Input data: 2 7 3 8 -1
sunshin totalSunshin
maxHours minHours Output
e e
0 0 100 0
3
8
-1 -1 -1
Max
sunshine
hours: 0
Min
sunshine
hours: -1
Total
sunshine
hours: -1
[Total 20 Marks]