Tutorial 1.2 Repetition Answer
Tutorial 1.2 Repetition Answer
1. The use of appropriate control structure is very important when writing a computer
program.
Identify the suitable control structure to solve the following problems:
(b) Describe two (2) of the techniques that you have answered in question 2(a).
Repetition or looping allow to repeat one or more instructions until some conditions is
met.
i. Initial value of sentinel control variable ii. Condition to test sentinel control
variable
iii. Update the value of sentinel control
variable
Pseudocode Flowchart
Start Start
initialize i to 1
repeat while (i ≤ 3)
Print i i=1
i=i+1
end repeat False
Stop i<3
Stop
True
Print i
i=1+1
Pseudocode
Start
k=1
while ( k < = 10 )
input n
print n
k=k+1
end while
Stop
Print i
i=i+1
Counter Control Table
i = 100 i <=700 Print i i=i+1
1 True 1 2=1+1
2 True 2 3=2+1
3 True 3 4=3+1
…. True …. ….
…. True ….. …..
….. True ….. …..
… True ….. …..
99 True 99 100 = 99 + 1
100 True 100 101 = 100 + 1
101 FALSE Loop Terminates
14. Write a loop that displays “Do Good…” 50 times. Assume that you will use a counter
variable named count. Finally print “..it comes to you”.
i=1 i < 50 Print “Do Good..” i=i+1 Print “..it comes to you”
1 True Do Good 2=1+1
2 True Do Good 3=2+1
3 True Do Good 4=1+1
… ….. …… …….
…. ….. …….. …….
…. ….. …….. …….
… ….. ……. ……..
49 True Do Good 50 = 49 + 1
50 True Do Good 51 = 50 + 1
51 False Loop Terminates … it comes to you
15. Calculate the sum of 150 numbers that are being input interactively using a loop.
Pseudocode Flowchart
Start Start
set i =1
sum = 0 i=1
while ( i < 150) sum = 0
Read number
False
sum = sum + number
i = i +1 Print sum i < 150
end while True
Print sum
Stop Read number
Stop
16. Find the average marks from five (5) tests that have been taken by a student.
Pseudocode Flowchart
Start
test =1 Start
total = 0
while ( test <6) test = 1
Read marks total = 0
total = total + marks
False
test = test +1 average = total/5
test < 5
end while
average = total / 5 True
Print average Print average
Read marks
Stop
Stop
total = total + marks
test = test + 1
Control Structure:
17. Puan Rose is planning to prepare a healthy breakfast for her family. She needs to buy
few items and needs to calculate the total price based on the price and quantity of each
item that she wants to buy. For items with the item code of 5000 and above, 6% of Good
and Service Tax (GST) will be imposed. The calculation process is finished when she
enters 0 for the value of item code.
Pseudocode Flowchart
Start
Start
total price = 0 total price = 0
Read item code
While (item code ≠ 0 )
Read price , quantity
Read item code
if ( item code < 5000 )
Print
total = price x quantity
total
else False
price
total = 1.06 x (price x quantity)
Item code ≠ 0?
end if
total price = total price + total True
Read item code
End While Stop Read price, quantity
Print total price
Stop
False
Item code <5000?
True
total = price x quantity
total = 1.06 x
(price x quantity
total price = total price
+ total
Control Structure:
18. With the escalating price of fuel, the need to understand the car fuel consumption has
become increasingly important. In April 2017, you have made 10 journeys .You want to
know the average fuel consumption for month of April 2017. Assume that your car can
read the mileage, in kilometer (km) and also the fuel tank level, in litre (L) at the
beginning and at the end of journey.
Input Process Output
Start KM, End KM, Calculate the average fuel consumption average fuel consumption
Start Litre, End Litre based on Start KM, End KM, Start Litre and
End Litre for 10 journeys.
Pseudocode
Start
journey =1
TotalKM = 0
TotalKM = TotalKM + (End KM – Start KM)
TotalLitre = 0 TotalLitre= TotalLitre + (Start Litre – End Litre)
While ( journey<10)
KM = End KM – Start KM
TotalKM = TotalKM + KM
journey = journey +1
End while
Stop
Flowchart
# Organ Donation : It takes lives to save lives…
86
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution
Start
i=1
TotalKM =0
TotalLitre = 0
True
i < 10 Read Start Litre, End Litre,
False Start KM, End KM
KM = End KM – Start KM
Litre = Start Litre – End Litre
Print Average
TotalKM = TotalKM + KM
TotalLitre= Total Litre+ Litre
Stop
i=i+1
Control Structure:
19. Mr. Rahman plans to make a reservation for a hotel room in Langkawi Island. The basic
rate is RM250.00 per night and the sales and service charge is 15% of the room rate.
Determine the total payment if he books the hotel room for n nights. List the input,
processes involved to calculate the total payment and the output.
Pseudocode
Start
Read number of night Start
Total = number of night x RM250
Service charge = 0.15 x Total
Total Payment = Total + service charge Read night
Print Total Payment
Stop
Total = night x RM250
Charge = 0.15 x Total
Total Payment = Total + Charge
Stop
Control Structure:
Flowchart
Start
night = 1
total = 0
Read n
while ( night <= n)
total = total + RM250 Start
night = night + 1
end while
service charge = 0.15 x total night = 1
total payment = service charge + total total = 0
Print total payment
Stop
Read n
True
Stop
20. Display even numbers from 100 to 700 and add the total of the numbers. Display the total
sum.
Flowchart
Start
number = 100
total = 0
False
True
Control Structure:
Control Structure:
Flowchart
Start
counter = 0
False
True
False
year of service < 10?
Print bonus
counter = counter + 1
22. Given the list of daily temperatures for a certain number of days, design an algorithm to
determine the maximum temperature. The first input is the number of days.
Pseudocode
Start
days = 1
max = -99
Read number of days
while (days <= number of days)
Read temperature
if ( temperature > max)
max = temperature
end if
days = days + 1
end while
Print max
Stop
Flowchart
Start
i=1
max = -99
Read n
False
temp > max?
True
max = temp
23. Seaview Chalet provides three types of room for accommodation. TABLE 1 shows the
rate for each type of room. If the guests check-in during the weekdays, 10% discount will
be given. A guest can make a reservation for more than one night. Calculate and display a
bill that a guest has to pay if he/she books a room for a number of days.
Number of nights, Determine and calculate hotel bills based hotel bills
week, type on number of nights, week and type given
by a user.
Pseudocode
Start
Read n, week, room
if (week = “Weekdays”)
if (type = ‘S’)
hotel bill = 0.9 x (n x RM300)
else if (type= ‘D’)
hotel bill = 0.9 x (n x RM220)
else
hotel bill = 0.9 x (n x RM180)
end if
else
if (type = ‘S’)
hotel bill = n x RM300
else if (type= ‘D’)
hotel bill = n x RM220
else
hotel bill = n x RM180
end if
Print hotel bill
end if
Stop
Start
Flowchart
Start
True True
week = “WD”? Type = ‘S’? Bill = 0.9 x (n x RM300)
False
True False
Bill = n x RM300 Type = ‘S’?
True False True
Type = ‘D’? Bill = 0.9 x (n x RM220)
Bill = n x RM220 Type = ‘D’? False
Print Bill
Stop
# Organ Donation : It takes lives to save lives…
95
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution
Control Structure:
FIGURE 1
Assume initial value of Nom is 5, trace the flowchart by filling the table below.
Count=0 Total=0 Read Nom≤15 Total =Total + Count = Nom= Nom Print
Nom Nom Count +2 +5 Count,
Total,
Nom
2 5 10 True 15 = 5 + 10 4=2+2 15 = 10 + 5
25. Write the IPO analysis and pseudocode for e following flow chart
Input
No input
Output Number
Pseudocode
Number = 1
Number = Number + 3
Print Number
End While
26. Lily wants to calculate an average of a series of positive numbers. The process will
stop if the number entered has a negative value.
Flowchart
Start
bil = 0
total = 0
Read number
False
average = total / bil number > 0?
True
bil = bil + 1
Stop
27. Harmony Ltd would like to calculate the total bonus that will be given to their
1,500 staff. The bonus given will be based on the year of service as shown in the
table below. Year of Service Bonus
Above 10 years RM3,000
5-10 years RM2,000
Below 5 years RM1,000
Pseudocode
Start
staff = 1
total bonus= 0
while (staff <= 1500)
Read year of service
if (year of service > 10)
bonus = 3000
else if (year of service >= 5)
bonus = 2000
else
bonus = 1000
end if
total bonus = total bonus + bonus
staff = staff + 1
end while
Print total bonus
Stop
Flowchart
Start
staff = 0
total bonus = 0
Stop
Read year of services
True
bonus = 3000 year of service >10
<= 1500?
False
True
bonus = 2000 year of service >= 5
<= 1500?
False
bonus = 1000
bil = bil + 1
FIGURE 1
Flowchart
Read N
i < N?
False
True
Print bonus
29. Modify the question 28 so that the bonus calculation is based on the salary’s grade given
in TABLE 1.
Salary Grade Bonus Rate
A 20%
B 40%
C 60%
Table 1
N,
Employee’s name, Calculate bonus based on salary and salary Employee’s name, bonus
salary, salary grade grade entered by a user for N times.
Pseudocode
Start
i=1
Read N
Repeat while ( i < N )
Read employee’s name, salary, salary grade
if (salary grade = ‘A’)
Bonus = 0.2 x salary OUTPUT:
else if ( salary grade = ‘B’)
Bonus = 0.4 x salary ALI RM200
else
Bonus = 0.6 x salary SITI RM400
end if
Print employee’s name, Bonus ABU RM600
i=i+1
End repeat
Stop
Flowchart
Start
i=1
Read N
False
Stop i > =N ? i=i+1
True
Print employee’s
Read employee’s name, name, Bonus
salary, grade
False
True
Grade = ‘A’? Bonus= 0.2 x salary
False
True
Grade = ‘B’? Bonus= 0.4 x salary
False
30. An Nur Care Centre charges for monthly caring services based on a child’s age. Charges
for a child less than 24 months in RM 220 per month, for a child from 2 to 4 years old is
RM 180 per month and for child above 4 years old is RM 150 per month respectively.
Assume they are n children currently at An Nur Care Center. Given n children names and
ages, determine the monthly charge for each child.
Input Process Output
n children, Determine the monthly charge based on Monthly Charge
name and age for n children.
name, age
Pseudocode
Start
children = 1
Read n children
While ( children < n children)
Read name, age
if ( age< 24 months ) OR if ( age< 2 years)
Monthly Charge = RM220
else if ( age< 48 months) OR ( age < 4 years )
Monthly Charge = RM180
else
Monthly Charge = RM150
end if
Print Monthly Charge
children = children + 1
End while
Stop
Flowcharts
Start
children = 1
Read n
children = children + 1
True
Print charge
Read name, age
Charge = RM150
Input : number
Process : Determine whether the number is divisible by 7
Output : Either the message “Divisible by 7” or “Not divisible by 7” based on
number
Pseudocode Flowchart
Start
Read number Start
if (number%7 = 0 )
Print “Divisible by 7”
else
Print “Not divisible by 7” Read number
end if
Stop
True
Print “Divisible
by 7”
Stop
Control Structure:
32. A listening test is one of the
components in the MUET competency test that can be taken maximum 3 times by each
student. Students will have to redo the test if the marks obtained is less than 70. The final
mark obtained by students are calculated based on the average marks from all the
listening tests taken.
Pseudocode
Flowchart
Control Structure:
33. Puan Marina is a programming lecturer at Kolej Teknologi Kedah. Due to the high failure
rate in her class, she intends to find the total number of students who have passed in the
examination. The passing mark is 40 marks and above. There are 98 students in her class.
Pseudocode
Start
student = 1
Total Pass = 0
while (student <= 98)
Read marks
if (marks >= 40)
Total Pass = Total Pass + 1
end if
student = student + 1
end While
Print Total Pass
Stop
Flowchart
Start
student = 1,
Total Pass = 0
False
Print Total Pass Student < 98
True
Stop Read marks
Marks > 40
False
True
Student = student + 1
ControlStructure:
Control Structure:
Pseudocode
Start
day = 1
total price = 0 total = price x qtty
while (day <= 7) total price = total price + price
Read price, number of book
total price = total price + (price x number of book)
day = day + 1
end while
Print total price
Stop
Flowcharts
Start
False
Print total price day < 7
True
day = day + 1
35. The sales manager at MN Company wants to display the price based on a product ID she
enters. Assume there are ‘n’ items. The valid product IDs and their corresponding prices
are shown here. For each item, if the product ID is not valid, the program should display
the “Invalid product ID” message.
Product ID Price
4 12.00
8 50.55
n,
product ID Determine the price based on product ID Price or
entered by a user for n times. message “Invalid product ID”
Pseudocode
Start
counter = 1
Read n
while (counter <= n)
Read product ID
if (product ID = 4)
Price = RM12.00 If (product Id = 4)
Print Price Print “ Price is RM12.00”
else if (product ID = 8)
Price = RM50.55
Print Price
else
Print “Invalid product ID”
end if
counter = counter + 1
end while
Stop
Flowcharts
Start
counter = 1
Read n
False
Stop counter < n
True
Read product ID
True
Product ID = 4 Price = RM12.00
False
True
Product ID = 8 Price = RM50.55
False
Print price
Print “Invalid product ID”
counter = counter +1