030 Chapter 3 Problem Solving Technique
030 Chapter 3 Problem Solving Technique
1
Faculty of Computing
@2017/2018-1
Chapter 3 Objectives
2
Chapter 3 Contents
3.1 Introduction
3.2 Development Algorithms
3.3 Control Structures
3.4 Top-Down Design
3.5 Summary
3
Chapter 3 3.1 Introduction
4
Chapter 3 3.1 Introduction
5
Chapter 3 Contents
3.1 Introduction
3.2 Development Algorithms
• Pseudo-code
• Flowchart
3.3 Control Structures
3.4 Top-Down Design
3.5 Summary
6
Chapter 3 3.2 Development Algorithms
Introduction
7
Chapter 3 3.2 Development Algorithms
Pseudo-code
8
Chapter 3 3.2 Development Algorithms
Example: Pseudo-code
1. Start
2. Read (Get) the first number, let say A
3. Read (Get) the second number, let say B
4. Multiply A * B, store the result in C.
5. Display the result, C
6. End.
9
Chapter 3 3.2 Development Algorithms
Calculate Resut
C=A*B Process
Display the
Result C Output
13
Chapter 3 3.2 Development Algorithms
Yes
No
Stop
14
Chapter 3 3.2 Development Algorithms
Start
Example: Flowchart 1
2
2 connection on the 1
Stop
15
Chapter 3 3.2 Development Algorithms
Page 1 Page 2
Start
1 2
Yes 1
No End
16
Chapter 3 3.2 Development Algorithms
The details (how the function works)
we put in another flowchart.
Example: This also known as Function-Definition
Function-Call
Page 1 Start terminal for a Page 2
Start Function is different.
Do not use “Start”
AVRG ( result,n1, n2,n3)
Note:
Module = Function Read
n1, n2 , n3
= Subroutine .
sum = n1+ n2+n3
Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart
result = sum/3
Calculate Resut
Calculate volume,
C=A*B2
V= π*h*r
Display the
Display the
Result C
result V
End
18
Chapter 3 3.2 Development Algorithms
F =(C * 9/5) + 32
19
Chapter 3 Contents
3.1 Introduction
3.2 Development Algorithms
3.3 Control Structures
• Introduction
• Sequential Structure,
• Selective Structure,
• Repetition Structure
3.4 Top-Down Design
3.5 Summary
20
Chapter 3 3.3 Control Structures
Introduction
• Programs could be written in three way of control
structures (flow of execution):
21
Chapter 3 3.3 Control Structures
(a) Sequential Structure
• A series of
statements that are
executed in the order 1. Start
they are written. 2. Statement_1
3. Statement_2
4. Statement_3
• Pseudo-code: mark
the beginning and n. Statement_n+1
end of a block of n+1. End
statements.
22
Chapter 3 3.3 Control Structures
(a) Sequential Structure
• Multiple statements considered as one statement.
23
Chapter 3 3.3 Control Structures
(a) Sequential Structure
Trace in sequential
Start
Print Output:
Area,
Perimeter Area = 15
Perimeter = 16
End
24
Chapter 3 3.3 Control Structures
(a) Sequential Structure
Calculate the total salary for Ahmad based on the number of days in
a month. Everyday he spent 8 hours with pay RM3 per hour.
25
Chapter 3 3.3 Control Structures
(b) Selection Structure
26
Chapter 3 3.3 Control Structures
Problem 2:
Determine whether a
number is even or odd.
27
Chapter 3 3.3 Control Structures
Selective Structures
(statement)
if if...else switch
28
Chapter 3 3.3 Control Structures
if selection statement
: TRUE
n. if condition condition
condition
n.1 statement FALSE
n+1. end_if
: statement
29
Chapter 3 3.3 Control Structures
• Example: if
Suppose the passing grade on an exam is 60.
The pseudo-code statement:
:
TRUE FALSE
n. if condition condition
condition
n.1 statement_1
n+1. else condition
n+1.1 statement_2 statement_1 statement_2
n+2. end_if
:
31
Chapter 3 3.3 Control Structures
• Example: if...else
Suppose the passing grade on an exam is 60.
The pseudo-code statement:
Question:
33
Chapter 3 3.3 Control Structures
c)
Solution 3.5:
Start
a) 1. Start
2. Read n
Get n
3. if n modulus 2 = 0
3.1 Print “This is an even number”
3.2 Go step 5
4. else n % 2 == 0?
4.1 Print “This is an odd number”
False
5. End if True
b) Double selection
End
34
Chapter 3 3.3 Control Structures
2) Nesting:
• a connecting control statement can be within another
control statement.
35
Chapter 3 3.3 Control Structures
Stacked if
False
Age > 21?
True Print
Print “kid/teenager”
“Adult”
Gender == True
‘F’ ?
36
Chapter 3 3.3 Control Structures
Nested if
• if within if FALSE
• Pseudo-code: test1
TRUE
• Example: Nested if
Print “Excellent” for the grades on an exam equal to 100 for
the passed students. Suppose the passing grade is 60.
TRUE
Print “Excellent”
38
Chapter 3 3.3 Control Structures
Nested if...else
• if...else within if
• The pseudo-code statement:
Algorithm: nested if
:
n.if condition
:
n.m if condition
n.m.1 statement
:
n+1. end_if
:
39
Chapter 3 3.3 Control Structures
Nested if...else
40
Chapter 3 3.3 Control Structures
41
Chapter 3 3.3 Control Structures
The pseudo-code statement:
42
Chapter 3 3.3 Control Structures
Flowchart design
of the problem.
43
Chapter 3 3.3 Control Structures
Redraw the
flowchart design
of the problem
by comparing
the smaller value
of grade.
44
Chapter 3 3.3 Control Structures
• Examples:
• Operators:
> Greater than 12 > 5 is true
< Less than
>= Greater than or equal to 7 <= 5 is false
<= Less than or equal to
if x is 10, then
== Equal to
x == 10 is true
!= Not equal to
x != 8 is true
x == 8 is false
45
Chapter 3 3.3 Control Structures
Logical Operators
• Used to create relational expressions from other relational
expressions
• Operators, meaning, and explanation:
&& AND New relational expression is true if both
expressions are true
|| OR New relational expression is true if either
expression is true
! NOT Reverses the value of an expression – true
expression becomes false, and false
becomes true
46
Chapter 3 3.3 Control Structures
47
Chapter 3 3.3 Control Structures
Read Num
Num>0? No
Print
"Category B"
Yes
Print
"Category A"
Stop
49
Chapter 3 3.3 Control Structures
(c) Repetition Structure
• Most programs involve repetition or looping.
50
Chapter 3 3.3 Control Structures
• Two means of repetition / looping:
51
Chapter 3 3.3 Control Structures
Counter-controlled repetition:
52
Chapter 3 3.3 Control Structures
x Start
initialization cnt=0
FALSE
FALSE
condition cnt<5
TRUE TRUE
increment cnt=cnt+1
y End
(a) (b)
Sentinel-controlled
repetition:
• Pseudo-code:
• Unknown in advance
exactly how many times the Algorithm: Loop control
loop will be executed. by sentinel value
1. Start
• Use sentinel values to 2. Set repeat = 1
control repetition (indicate 3. while (repeat == 1)
end of the data). 3.1 Read no1
3.2 Read no2
3.4 Print no1 + no2
3.5 Read repeat
4. end_while
5. End
54
Chapter 3 3.3 Control Structures
Pre-test loop
Pseudo code: requires the • while a set condition is true,
use of the keywords while or repeat statement (body of loop)
for
(one choice)
55
Chapter 3 3.3 Control Structures
Post-test loop
Pseudo code: requires the use of • statement (body of loop)
the keywords do...while or will be repeated while a set
repeat...until condition is true
56
Chapter 3 3.3 Control Structures
End
57
Chapter 3 3.3 Control Structures
Activity : trace output
What is the Output of the following flowchart when the input is Num= 4
Variables (in memory):
Start
Num [ ]
Read Num
Result [ ]
Count [ ]
Initialize
Result=0
Count=Num
Print Count
Result=Result + Count
Count>0? Yes
Count=Count - 1
No
Print
Result
Stop
58
Chapter 3 3.3 Control Structures
Activity : trace output
What is the Output of the following flowchart when the input is Num= 4
Start
Variables
Variables
Variables (in
(in
(in memory):
memory):
memory):
Num
Num
Num [[[ 444 ]]]
Input: Result
Result
Read Num
Num <- 4
Result [[[ 4
0710]
9 ]]] 049
7 +++ 431
2
Count
Count [[[ 4
Count 320
1 ]]] 431
2 --- 111
Initialize
Result=0
Count=Num
Print
Result
Stop
59
Chapter 3 Contents
3.1 Introduction
3.2 Development Algorithms
3.3 Control Structures
3.4 Top-Down Design
3.5 Summary
60
Chapter 3 3.4 Top-Down Design
Introduction
61
Chapter 3 3.4 Top-Down Design
• NOT a flowchart.
62
Chapter 3 3.4 Top-Down Design
63
Chapter 3 3.4 Top-Down Design
Left-right on the chart is irrelevant
Top- a box
representing the
entire problem
65
Chapter 3 3.4 Top-Down Design
3.1 Introduction
3.2 Development Algorithms
3.3 Control Structures
3.4 Top-Down Design
3.5 Summary
68
Chapter 3 3.4 Summary
69
Chapter 3 3.4 Summary
____________________________________________________________________________________________________________________________________
70 _
Chapter 3 3.4 Summary
71
Chapter 3
Self-Reviews
72
Chapter 3 Self-Review
75
Chapter 3 Self-Review
76
Chapter 3 Self-Review
Solution 3.2:
Start
c) Flowchart:
cnt=0
Total=0
Get num
FALSE
Cnt < num
Get G
Average
Total = total + G
cnt=cnt+1
End
77
Chapter 3 Self-Review
79
Chapter 3 Self-Review
80
Chapter 3 Self-Review
Solution 3.3:
Start Start
c) Flowchart:
cnt=0 cnt=0
Total=0 Total=0
Get
Get G Repeat
FALSE FALSE
G != 999 Repeat ==1
Get End
End
Repeat
81