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

Tutorial 1.2 Repetition Answer

The document discusses problem solving using repetition or looping structures. It provides examples of counter-controlled and sentinel-controlled repetition, and conversions between pseudocode and flowcharts.

Uploaded by

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

Tutorial 1.2 Repetition Answer

The document discusses problem solving using repetition or looping structures. It provides examples of counter-controlled and sentinel-controlled repetition, and conversions between pseudocode and flowcharts.

Uploaded by

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

1.

0 Problem Solving [REPETITION]


1.2 Problem Analysis
1.3 Design a Solution

1.0 Problem Solving (REPETITION)


1.2 Problem Analysis
 Identity input, process, and output from a given problem
1.3 Design a Solution
 Define Algorithm (Pseudocode & Flowchart)
 Solve a given problem using Pseudocode
 Solve a given problem using Flowcharts

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:

Problem Control Structure


a. Check whether a staff is entitled to get Selection Control Structure
an overtime pay or not.
b. Get the average marks obtained by students in Repetition Control Structure
the whole matriculation batch. with counter control
c. Calculate the fuel price that needs to be paid by Sequence Control Structure
a customer at a petrol station.
d. Enter the items purchased by a customer one by Repetition Control Structure
one until the null value is entered. with sentinel control
e. Give 10%discount from the total price for a Repetition Counter control
homeless customer. with Accumulator

2. Before writing a program to solve a particular problem, it is essential to have a thorough


understanding of the problem and carefully plan the approach to solve the problem.
(a) Define algorithm.

An algorithm is a step-by-step instruction that will transform the input into


the output.

(b) Describe two (2) of the techniques that you have answered in question 2(a).

Pseudocode An artificial, informal language used to develop algorithms


which also similar to everyday English.

Flowchart A graphic representation of the algorithms

# Organ Donation : It takes lives to save lives…


77
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

3. Explain repetition or looping.

Repetition or looping allow to repeat one or more instructions until some conditions is
met.

4. Define counter controlled.

Counter-controlled repetition is often called definite repetition because the number of


repetitions is known before the loop begins executing.

5. List the 4 tasks associated with counter controlled:

i. Counter-Loop Name ii. Initial Value

iii. Condition iv. Update Value

6. Define sentinel controlled.

Sentinel controlled repetition is sometimes called indefinite repetition because it is not


known in advance how many times the loop will be executed.

7. List the 3 tasks associated with sentinel controlled:

i. Initial value of sentinel control variable ii. Condition to test sentinel control
variable
iii. Update the value of sentinel control
variable

8. Draw a symbol used to represent a repetition structure’s condition in a flowchart.

# Organ Donation : It takes lives to save lives…


78
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

9. Identify the following control structure.

10. Transfer the pseudocode to flowchart and counter control table.

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

Counter Control Table


i= 1 i<=3 Print i i = i+1
1 True 1 2=1+1
2 True 2 3=2+1
3 True 3 4=3+1
4 False ------------- end while ---------------

# Organ Donation : It takes lives to save lives…


79
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

11. The following pseudocode segment contains an error.

Start Rewrite the correct pseudocode segment.


set number = 1
repeat while (number is less than 5) Start
Print “Hello” number = 1
while (number <5)
end repeat
Print “Hello”
Stop number = number + 1
end while
Stop
(i) Identify and name the error.
No update counter

12. Convert the flowchart below to


pseudocode.

Pseudocode

Start
k=1
while ( k < = 10 )
input n
print n
k=k+1
end while
Stop

# Organ Donation : It takes lives to save lives…


80
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

13. Display numbers 1 to 100.

i. Identify the type of repetition used. Counter Controlled


ii. Name & initialize the counter i=1
iii. Condition i <= 700
iv. Update counter i=i+1
v. What is the value of counter when the loop 101
exits?
Pseudocode Flowchart
Start
set i =1 Start
repeat while ( i <100)
Print i
i=1
i = i +1
end repeat False
Stop i < 100
Stop
True

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

# Organ Donation : It takes lives to save lives…


81
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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. Identify the type of repetition used. Counter Controlled


ii. Name & initialize the counter count = 1
iii. Condition count <= 50
iv. Update counter count = count + 1
v. What is the value of count when the loop 51
exits?
Pseudocode Flowchart
Start
set count =1 Start
repeat while ( count < = 50)
Print “Do Good…”
count = 1
count = count +1
end repeat
False
Print “..it comes to you”.
count < 50
Stop Print “..it
comes to True
you”
Good..” Print “Do Good..”
Stop
count = count + 1

Counter Control Table

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

# Organ Donation : It takes lives to save lives…


82
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

15. Calculate the sum of 150 numbers that are being input interactively using a loop.

i. Identify the type of repetition used. Counter Controlled with accumulator


ii. Name & initialize the counter count = 1
iii. Condition count <= 150
iv. Update counter count = count + 1
v. What is the value of counter when the loop 151
exits?

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

sum = sum + number


count = count + 1

Counter Control Table


i = 1 sum = 0 i < 150 number sum = sum + number i = i+ 1 Print sum

1 0 TRUE 5 5=0+5 2=1+1


2 5 TRUE 7 12 = 5 + 7 3=2+1
3 12 TRUE 10 22 = 12 + 10 4=3+1
.. 22 TRUE XX … …
… … TRUE XX …. ….
… …. TRUE XX …. …
… …. TRUE XX …. …
149 …. TRUE XX …. 150=149+1
150 …. TRUE XX XXX== XX + XX 151=150+1
151 XXX FALSE Loop Terminates XXX

# Organ Donation : It takes lives to save lives…


83
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

16. Find the average marks from five (5) tests that have been taken by a student.

i. Identify the type of repetition used. Counter Controlled with accumulator


ii. Name & initialize the counter test = 1
iii. Condition test <= 5
iv. Update counter test = test + 1
v. What is the value of counter when the loop 6
exits?

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

Counter Control Table


test = total = test < marks total=total+marks Test=test+1 Average
1 0 5 Marks
1 0 TRUE 80 80 = 0 + 80 2=1+1
2 80 TRUE 90 170 = 80 + 90 3=2+1
3 170 TRUE 75 245 = 170 + 75 4=3+1
4 245 TRUE 55 300 = 245 + 55 5=4+1
5 300 TRUE 100 400 = 300 + 100 6=5+1
6 400 FALSE Loop Terminates 80 = 400/5

# Organ Donation : It takes lives to save lives…


84
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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.

Input Process Output


item code, Determine to calculate the total price based total price
price, on the item code, price and quantity as long
quantity as (while) item code not equal to 0.

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

Read item code

# Organ Donation : It takes lives to save lives…


85
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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)

Read Start KM, End KM, Start Litre, End Litre

KM = End KM – Start KM

TotalKM = TotalKM + KM

Litre = Start Litre – End Litre

TotalLitre= TotalLitre + Litre

journey = journey +1

End while

Average fuel consumption = TotalLitre/TotalKM

Print Average fuel consumption

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

Average = TotalKM/ TotalLitre

KM = End KM – Start KM
Litre = Start Litre – End Litre

Print Average
TotalKM = TotalKM + KM
TotalLitre= Total Litre+ Litre

Stop

i=i+1

# Organ Donation : It takes lives to save lives…


87
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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.

Input Process Output

number of night Calculate total payment based on number total payment


of night entered by a user.

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

Print Total Payment

Stop

# Organ Donation : It takes lives to save lives…


88
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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

service charge = 0.15 x total


night <= n
total payment = service charge + total

True

Print Total Payment total = total + RM250


night = night + 1

Stop

# Organ Donation : It takes lives to save lives…


89
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

20. Display even numbers from 100 to 700 and add the total of the numbers. Display the total
sum.

Input Process Output


No input Determine to calculate total of even Total
numbers from 100 to 700.
Pseudocode
Start
number = 100
total = 0
while (number <= 700)
if (number % 2 = 0)
total = total + number
end if
number = number + 1
end while
Print total
Stop

Flowchart

Start

number = 100
total = 0

Print total number < 700?


False
True

Stop number % 2=0? number = number + 1

False
True

Total = total + number

21. Convert the following pseudocode to the appropriate flowchart.

# Organ Donation : It takes lives to save lives…


90
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure:
Control Structure:

Flowchart

Start

counter = 0

Read number of staff

Stop counter < number of staff?

False
True

Read year of service, salary

False
year of service < 10?

Bonus = 2.0 x salary True

Bonus = 1.5 x salary

Print bonus

counter = counter + 1

# Organ Donation : It takes lives to save lives…


91
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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.

Input Process Output

number of days, Determine maximum temperature for maximum temperature


temperature certain number of days based on
temperature entered by a user.

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

# Organ Donation : It takes lives to save lives…


92
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Flowchart

Start

i=1
max = -99

Read n

Print max i < n?


False
True

Read temp i=i+1


Stop

False
temp > max?

True

max = temp

# Organ Donation : It takes lives to save lives…


93
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Nested Selection

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.

Input Process Output

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

# Organ Donation : It takes lives to save lives…


94
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Start

Read n, week, room type


if (week = “Weekdays” & type = ‘S’)
hotel bill = 0.9 x (n x RM300)
else if (week = “Weekdays” & type= ‘D’)
hotel bill = 0.9 x (n x RM220)
else if (week = “Weekdays” & type= ‘B’)
hotel bill = 0.9 x (n x RM180)
else if (week = “Weekend” & type= ‘S’)
hotel bill = n x RM300
else if (week = “Weekend” & type= ‘D’)
hotel bill = n x RM220
else
hotel bill = n x RM180
end if
Print hotel bill
Stop

Flowchart

Start

Read n, week, type

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

Bill = n x RM180 Bill = 0.9 x (n x RM180)

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:

24. Consider a flowchart given in FIGURE 1.

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

0 0 5 True 5=0+5 2=0+2 10 = 5 + 5

2 5 10 True 15 = 5 + 10 4=2+2 15 = 10 + 5

4 15 15 True 30 = 15+ 15 6=4+2 20 = 15 + 5

6 30 20 False ---------- loop terminate --------------- 6, 30, 20

# Organ Donation : It takes lives to save lives…


96
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection

25. Write the IPO analysis and pseudocode for e following flow chart

Input

No input

Process Calculate number add to 3 based


on number while number less or
equal than 10.

Output Number

Pseudocode

Number = 1

While (Number <=10)

Number = Number + 3

Print Number

End While

# Organ Donation : It takes lives to save lives…


97
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection

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.

Input Process Output


Determine to calculate an average of a
number series of positive number while number is average
positive value.
Pseudocode
Start
bil = 0
total = 0
Read number
while (number > 0)
total = total + number
bil = bil + 1
Read number
end while
average = total / bil
Print average
Stop

Flowchart

Start

bil = 0
total = 0

Read number

False
average = total / bil number > 0?

True

Print average Total = total + number Read number

bil = bil + 1
Stop

# Organ Donation : It takes lives to save lives…


98
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection

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

Input Process Output

Year of service Determine to calculate total bonus based Total bonus


on year of service for 1500 staff.

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

# Organ Donation : It takes lives to save lives…


99
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection

Flowchart

Start

staff = 0
total bonus = 0

staff <= 1500?


Print total bonus False
True

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

Total bonus = total bonus + bonus

bil = bil + 1

# Organ Donation : It takes lives to save lives…


100
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection


28. Employees at Syarikat ABC Sdn. Bhd.
Get bonus at the end of the year as shown in the pseudocode in FIGURE 1. There are N
numbers of employees at the company. Bonus is 50% of the salary.

FIGURE 1

Input Process Output

N, Employee’s name, bonus


Employee’s name, Calculate bonus based on salary entered by
salary a user for N times.

Flowchart

Read N

i < N?
False
True

Read employee’s name, salary


i=i+1

bonus = 50% x salary

Print bonus

# Organ Donation : It takes lives to save lives…


101
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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

Input Process Output

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

# Organ Donation : It takes lives to save lives…


102
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection

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

Bonus= 0.6 x salary

# Organ Donation : It takes lives to save lives…


103
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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

# Organ Donation : It takes lives to save lives…


104
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Control Structure: Selection

Flowcharts

Start

children = 1

Read n

children = children + 1

Stop children > n?

True
Print charge
Read name, age

age < 2 years? Charge = RM220

age < 4 years? Charge = RM180

Charge = RM150

# Organ Donation : It takes lives to save lives…


105
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

31. Draw a flowchart based on the given problem analysis.

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

False Print “Not


Number%7=0?
Divisible by 7”

True

Print “Divisible
by 7”

Stop

# Organ Donation : It takes lives to save lives…


106
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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.

Input Process Output

Pseudocode

Flowchart

# Organ Donation : It takes lives to save lives…


107
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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.

Input Process Output

Determine to calculate the total number of


marks students who have passed in the Total number of students who
examination based on marks for 98 have passed
students.

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

# Organ Donation : It takes lives to save lives…


108
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

Flowchart

Start

student = 1,
Total Pass = 0

False
Print Total Pass Student < 98

True
Stop Read marks

Marks > 40
False
True

Total Pass = Total Pass + 1

Student = student + 1

# Organ Donation : It takes lives to save lives…


109
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

ControlStructure:
Control Structure:

34. A former secretary had left her


job and decided to sell textbooks. Assume the book is sold at different price on each day.
Calculate how much money that will be earned after a week.

Input Process Output

price, Calculate total price book sold based on total price


number of book price and number of books for 7 days.

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

day = 1, total price = 0

False
Print total price day < 7

True

Stop Read price, number of book

total price = total price + (price x number of book)

day = day + 1

# Organ Donation : It takes lives to save lives…


110
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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

Input Process Output

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

# Organ Donation : It takes lives to save lives…


111
1.0 Problem Solving [REPETITION]
1.2 Problem Analysis
1.3 Design a Solution

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

# Organ Donation : It takes lives to save lives…


112

You might also like