8.2 Problem Solving (Control Structure)
8.2 Problem Solving (Control Structure)
Problem Solving
Edited by awie
What will you learn?
Problem Solving
Problem Analysis
Design a solution
Implementation
Testing
Documentation
Steps in problem solving
5
Problem Analysis
Identify input, processing, output and data
component.
Refer to book and website
Ask expert
Meet with system analyst and users.
Steps in problem solving
6
Design a solution
Divide all program activities into program
modules.
Create solution algorithm (pseudocode and
flowchart) for each program modules.
Design user interface.
Steps in problem solving
7
Implementation
Coding a program involves translating the solution
algorithm into programming language usually on
paper.
Typing the code into the computer using
programming language such as Pascal, C++ and
Visual Basic.
Steps in problem solving
8
Testing
Several methods have been devised for finding and removing
logic error and syntax error.
Manually testing with sample data.
The program is running through a computer using a
translator program. Before the program runs, it must be
free of syntax errors. Such errors will be identified by the
translator.
Testing by a selected group of potential users. Potential
users try out the program and provide feedback.
Steps in problem solving
9
Documentation
Documentation is important for people who may involve
with the program in the future.
Other programmers wish to update and modify it (program
maintenance).
Activities:
Produce a description of the program, layouts out the
input and output records and program listing.
Produce a problem definition, flowchart and pseudocode.
Steps in problem solving
10
Documentation
Activities:
Write comments with the program code
(internal documentation). These comments
explain the program’s purpose of the code
statements with the program.
Problem Analysis (IPO)
To do the IPO Analysis, start with identify :
1- Output
2- Input
3- Process
Problem Analysis (IPO)
How to make a hot coffee
INPUT
Problem Analysis (IPO)
How to make a hot coffee
Process
1. Add HOT WATER +
SUGAR + COFFEE in a
cup
2. Stir to blend the
coffee and sugar
together.
Problem Analysis (IPO)
Output :
should answer the following question:
What does the user want to see printed
on the printer, displayed on the screen,
or stored in a file?
Problem Analysis (IPO)
Input :
should answer the following question:
What information will the computer need to
know to print, display, or store the output
items?
Problem Analysis (IPO)
Process :
Processing item:
An intermediate value that the algorithm
uses when processing the input into the
output
Input, Process, Output Analysis
18
INPUT :
PROCESS :
OUTPUT :
Input, Process, Output Analysis
21
Problem statement:
Calculate the area of a rectangle
width
height
Process:
Calculate the area of rectangle.
Problem statement:
Find the average of three numbers input by user
We can summarize the information contained in
the problem statement as follows:
Input, Process, Output Analysis
26
Problem statement:
Find the average of area for two different
rectangular.
width2
width1
height2
height1
Input, Process, Output Analysis
29
Problem statement:
Determine the total cost of apples, given the
number of kilos of apples purchased and the
cost of apples per kilo
Problem statement:
Calculate the area of a rectangle
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
Example 5
45
1. IPO analysis
2. (a) Pseudocode
START
1. read width and height
2. calculate area
area of rectangle = width * height
3. output area of rectangle
END
Example 5: IPO and pseudocode
Input Process Output
width Calculate the area of rectangle area of rectangle
height
Algorithm - Pseudocode:
START
END
Example 5: Flow chart
48
areaOfRectangle = width*height
Output areaOfRectangle
end
Example 6
49
Problem statement:
Find the average of three numbers input by user
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
Example 6
50
1. IPO analysis
Input:
Process:
Output:
Example 6
51
Statement (s)
END
Example 6: IPO with pseudocode
Input Process Output
number1 Calculate the average of three numbers. average
number2
number3 Algorithm – Pseudocode:
START
2. Calculate average
average = (number1 + number2 + number3) / 3
3. Output average
END
Example 6
53
Problem statement:
Determine the total cost of apples, given the number of kilos of
apples purchased and the cost of apples per kilo
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
Example 7
55
1. Problem Analysis
Input:
Process:
Output:
Example 7
Determine the total cost of apples, given the number of kilos of
apples purchased and the cost of apples per kilo
2. (a) Pseudocode
START
Statement (s)
END
Example 7
57
2. (b) Flowchart
Design a Solution: Control structure
Design a Solution
Control Structure
Sequence
Selection
Repetition
Learning Outcome
Explain the purpose of each control structure
Apply appropriate control structure in problem
solving
Control structure
Control structure is a method that allows the
programmer to control the flow of a program.
Consist of three basic control structure:
Sequence
Selection
Repetition
Control structure
These structures are referred to as “control structure”
because they control the flow of the program’s logic.
Using these structure makes a program or an algorithm
Easy to understand
Easy to debug
Easy to change
Control Structure
is a series of statements that execute one after another
Sequence
is a series of statements that execute one after another
Selection – (branch)
statement is used to determine which of two different
statements to execute depending on certain conditions
Looping – (repetition)
statement is used to repeat statements while certain conditions
are met
Control Structure: Sequence
Purpose of sequence control structure
To solve the simplest programs consist just sequence of
statements.
No loops, no selection amongst alternative actions, no use of
subroutines.
Control Structure: Sequence
Instruction are executed sequentially one by one.
The instructions execute from first to next, till the last
instruction. Then the program will terminate.
Last
1st Next … instruction
instruction instruction
Control Structure: Sequence
Wash face
Comb hair
Smile in mirror
Having breakfast
Control Structure: Sequence
Example 2 :What would you do if you want to refuel your
car?
Go to a petrol station.
Remove nozzle from the fuel pump
Insert nozzle in fuel tank
Fill tank with fuel
Replace nozzle at the fuel pump
Pay for the fuel
Leave petrol station
Control Structure: Sequence
Example 3 : Write the algorithm to get robot seated
on the chair
Control Structure: Sequence
Example 8
PSZ Corporation gives a bonus to Irfan based on his
sales. Assume your program needs to calculate Irfan’s
bonus. The bonus is 5 percents of the sales
Example 8
69
Problem statement:
Calculate Irfan’s bonus. The bonus is 5% of the sales.
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
Example 8
70
1. IPO Analysis:
Input: sales
Output: bonus
Example 8
Calculate Irfan’s bonus. The bonus is 5% of the sales.
2. (a) Pseudocode
START
1. input sales
2. calculate bonus
bonus = sales*0.05
3. output bonus
END
Example 8: IPO with pseudocode
Input Process Output
sales Calculate bonus bonus
Algorithm - Pseudocode:
START
1. input sales
2. Calculate bonus
bonus = sales*0.05
3. Output bonus
END
Example 8: Flow chart
73
Read sales
bonus = sales*0.05
Output bonus
end
Control Structure: Sequence
Example 9
Tile Limited wants a program that allow its sales clerk
to enter width and length of area (feet), and also
price of square foot of tile. The program will display
the total price of tile based on the area inserted by
user.
Example 8
75
Problem statement:
program that allow its sales clerk to enter width and length of area (feet), and
also price of square foot of tile. The program will display the total price of tile
based on the area
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
Example 9
76
START
1. input width,length,price
2. Calculate totalprice
totalprice = width*length*price
3. Output totalprice
END
Example 9: Flow chart
79
totalprice = width*length,price
Output totalprice
end
Example 10
Melissa works for Quality Builders. Melissa's current weekly
pay is RM1000 but she is scheduled to receive a 3% raise
next week. Melissa wants you to write a program that will
display on the computer screen the amount of her new
weekly pay.
Example 10
81
Problem statement:
Remember!
1. Do Problem Analysis
2. Transfer to pseudo code or flow chart
Example 10
82
1. Problem Analysis
Input:
Process:
Output:
Example 10
2. (a) Pseudocode
START
Statement (s)
END
Example 10
84
2. (b) Flowchart
Try yourself
APM Industry management asks you to create a
program that calculate the total overtime payment
for staff if overtime rate payment was given RM
5.00 per hour.
The program will receive 2 numbers as input.
Calculate the sum of the 2 numbers
Control structure: Selection
86
ANALOGY
You need to choose to make
“Coffee O” or “Milk Coffee”
SUGAR
MILK
Control structure: Selection
Purpose of selection
Allows instructions to be executed non-sequentially.
Allows the comparison of two expressions, and based
on the comparison, to select certain course of action
FOCUS
Control structure: Selection if
if structure’s form
if (condition)
statement_1
If structure’s explanation
if condition is true
Perform action (statement_1 execute)
if condition is false, action is skipped, program
continues
Control structure: Selection if
90
Pseudocode format:
start
if (condition/expression)
statement1
End
Control structure: Selection if
Flow chart format:
start
condition/expre false
ssion
true
statement1
end
Control structure: Selection if
Example 12
Print “Excellent!” when user enter marks greater than
and equal to 80.
Remember to plan your algorithm!
1. Do IPO Analysis
2. Transfer to Pseudocode or Flow Chart
Example 12
94
1. IPO Analysis
Input: mark
Output: “Excellent”
Example 12
95
2(a) Pseudocode
start
1. read mark
2. if (mark >= 80)
2.1 print “Excellent”
end
Example 12
start
2(b) Flow chart
Read mark
false
mark >= 80
true
Print “Excellent”
end 96
Control structure: Selection if else
if-else structure’s form • In this form, the condition is
if (condition/expression) first evaluated.
• If it evaluates to TRUE,
statement_1
statement_1 is executed.
else • Otherwise, statement_2 is
statement_2 executed.
• Either statement_1 or
statement_2 is executed but
not BOTH.
Control structure: Selection if else
98
Pseudocode format:
Start
……..
if (condition/expression)
statement_1
else
statement_2
……..
End
Control structure: Selection if else
Flow chart format:
start
condition/expre false
ssion
* All statements can be true statement2
input, processing or statement1
output
end
Control structure: Selection if else
100
1. IPO Analysis:
Input: mark
Process: Identify mark greater than and equal to 80 then print
statement
Output: “Excellent!” or “Sorry, try again”
Control structure: Selection if else
2(a) Pseudocode
start
1. read marks
2. if (marks >= 80)
2.1 print “Excellent”
Else
2.2 print “Sorry, try again”
end
Control structure: Selection if else
2(b) Flow chart start
Read mark
false
Mark>=80
true Print “sorry,
Print “Excellent” try again”
end
Control structure: Selection if else
103
Example 14:
A high school poetry competition is open only for
students above 15 years old. Display “Eligible” if
the students meet the requirement, else display “Not
eligible” if otherwise.
Control structure: Selection if else
Example 14
A high school poetry competition is open only for students above 15 years old.
Display “Eligible” if the students meet the requirement, else display “Not eligible”
if otherwise.
1. IPO Analysis:
Input: age
Process:
Identify either the age of students greater than 15
Output: “Eligible” or “Not eligible”
Selection if else
106
2(a)Pseudocode
start
1. read age
2. if (age > 15)
2.1 print “Eligible”
else
2.2 print “Not eligible”
end
Selection if else
107
Read age
false
age>15
true Print “Not
Print “eligible” eligible”
end 107
Try yourself
false statement2
false
* All statements can be statement1
input, processing or Last statement
output
end
Multiple selection (nested if)
Example 1:
1. IPO Analysis:
2(a) Pseudocode
start
1.read age, point
2. if (age <= 19)
2.1 if (point < 6)
2.1.1 print “ok”
2.2. else
2.2.1 print “Test”
3. Else
3.1 print “Review”
end
Multiple selection (nested if)
2(b) Flow chart format:
start
end
Multiple selection (if - else )
117
if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
else
statement_4;
Multiple selection (if else)
Flow chart format:
start
true true
expression1 statement1
false
expression2 statement2
false
* All statements can be
input, processing or expression3 statement2
output
end
Multiple selection (if - else )
119
Example 1:
if student’s grade is greater than or equal to 80
Print “A”
else if student’s grade is greater than or equal to 60
Print “B”
else if student’s grade is greater than or equal to 50
Print “C”
else
Print “Failed”
Multiple selection (if - else )
121
1. IPO Analysis:
Input: grade
Process: To compare the grade based on the condition value
Output: “A” or “B” or “C” or “Failed”
Multiple selection (if - else )
122
read grade
true
grade >= 80 print“A”
false
true
grade >= 60 print“B”
false true
grade >= 50 print“C”
false
Print “Failed”
end 123
Multiple selection (if - else )
124
Example 2:
if (x > 0)
display "x is positive"
else if (x < 0)
display "x is negative"
else
display "x is 0"
Multiple selection (if - else )
125
1. IPO Analysis:
Input: x
true
false
true
false true
false
end 127
Multiple selection (switch)
128
Example :
1. IPO Analysis:
Input: grade
start
2(b) Flow chart
read grade
grade
A B C default
D, F
end
131
Multiple selection (switch)
132
true
expression1 statement1 break
false
true
expression2 statement2 break
false
* All statements can be true
input, processing or espression3 statement2 break
output
default
end
Nested if-else (switch)
134
start
2(b) Flow chart
read grade
true
case grade A print “excellent”
false true
case grade B Print “above average”
false true
print “average”
case grade C
false true
case grade D,F print “below average”
false
Display “error” false
end 134
Try yourself
Write IPO, pseudocode and draw a flowchart to enter a
number and if the number is 0, print “number is zero”.
The program will receive 2 numbers as input. Calculate the
sum of the 2 numbers
Write the IPO, pseudocode, and draw a flowchart if score
more than 40 and mark equal to 100 print Full Mark else print
OK.
Try yourself
Write IPO, pseudocode and draw a flowchart to enter a
number and if the number is 0, print “number is zero”.
The program will receive 2 numbers as input. Calculate the
sum of the 2 numbers
Write the IPO, pseudocode, and draw a flowchart if score
more than 40 and mark equal to 100 print Full Mark else print
OK.
Try yourself
Write IPO, pseudocode and draw a flowchart to enter a
number and if the number is 0, print “number is zero”.
The program will receive 2 numbers as input. Calculate the
sum of the 2 numbers
Write the IPO, pseudocode, and draw a flowchart if score
more than 40 and mark equal to 100 print Full Mark else print
OK.
Try yourself
TOPIC: 8.2
PROBLEM SOLVING
8.2.3: Design a Solution
8.2.3.3 Control structure (Repetition)
Learning Outcome
140
1. Pseudocode
start
initialize counter
while (expression)
statement(s)
counter increment
end
Applying Algorithm in while Construct
145
initialize counter
False while
(expression)
True
end statement(s)
counter increment
The Counter Table for while Construct
146
Problem Statement:
• Calculate the area of a rectangle for 3 times.
width
height
1. IPO Analysis:
Input:width, height
Process: area = width * height
Output: area
i i <= 3 width, height area = width * height area i=i+1
1. IPO Analysis:
Input:width, height
Process: area = width * height
Output: area
i i <= 3 width, height area = width * height area i=i+1
1 T 2
2 T 3
3 T 4
4 F Loop terminate
i=1
False
(i <= 3)
True
end width, height
area i=i+1
Repetition: do while Construct
Statement
Increment
Expression TRUE
FALSE
end
Example 2: do while Construct
1. IPO analysis
Write the IPO, pseudocode and draw a flowchart to display
the numbers 1 to 3 on the screen
IPO
Input : none
Process : repeat process 3 times
Output : count
Example 2: do while Construct
2(a) Pseudocode
Pseudocode
initialize the count variable to 1
do
display the count
add 1 to the count
Display count
TRUE
Add 1 to count
(count <= 3)
FALSE
end
Repetition: for Construct
159
2(a) Pseudocode
start
start
for (initialize counter; expression; counter increment)
statement(s);
end
initialize counter
False
(expression)
True
end statement(s)
counter increment
The Counter Table for for Construct
163
Problem Statement:
• Calculate the area of a rectangle for 3 times.
width
height
1. IPO analysis:
Input: width, height
Process: area = width * height
Output: area
i i <= 3 width,height area = width * height area i=i+1
1. IPO Analysis:
Input: width, height
Process: area = width * height
Output: area
i i <= 3 width,height area = width * height area i=i+1
1 T 2
2 T 3
3 T 4
4 F Loop terminate
i=1
False (i <= 3)
True
end width, height
area i=i+1
Accumulating in Problem Solving
169
Problem Statement:
Calculate the average of three numbers.
Problem Analysis:
Accumulating
Input: num1, num2, num3
Process: total = num1 + num2 + num3
average = total / 3
or
average = (num1 + num2 + num3) / 3
Output: average
Example 5: Accumulating in looping Structure
171
Problem Statement:
• Calculate the average of three numbers.
1. IPO analysis:
Input: num
Process: total = total + num
average = total / 3
Output: average
total i i <= num total = total + i=i+1 average = total / 3 average
3 num
0 1 T 70 70 2
2 T 80 150 3
3 T 90 240 4
4 F Loop terminate average = 240 / 3 80
Using accumulating in while construct (while counter table)
Example 5: Accumulating in while Construct
173
start
1. initialize total initial value of total
total = 0 2(a) Pseudocode
2. initialize counter initial value of counter
i=1
condition to test
3. while (i <= 3)
4. input num
5. accumulate total IPO analysis
total = total + num
6. counter increment increment to
i=i+1 modify the loop
7. repeat until i > 3 Repeat step 3 – 6 until
8. calculate average expression is false
average = total / 3
9. print average IPO analysis
end
Example 5: Accumulating in while Construct
174