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

Programming techniques Iteration worksheet 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Programming techniques Iteration worksheet 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Homework 3 Iteration

Unit 11 Programming techniques

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]

word = input(“Enter a word: ”)

length = word.length()

for i in lengh then


if word[i+1] > word[i] then
biggest = word[i+1]
else then
biggest = word[i]

next i

print(“The biggest letter of the word is: ”, biggest)

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

enter = input(“Enter the password: ”)


While count <= 3 and enter != password then
print(“Password rejected”)
count += 1
enter = input(“Enter the password: ”)
endwhile
print(“Password accepted”)

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

(b) What is the problem with the algorithm above? [2]

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]

You might also like