Upload
cuongnvbkict
View
229
Download
0
Embed Size (px)
Citation preview
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 1/15
Format ExplanationThis is the where procedures,variables, types, etc., aredeclared
○ Declares the names, types,etc., of procedures , variables.
This is the area where processing is described.
●Variable←Expression Assigns the value of theexpression to the variable.
Pseudo-Code Description Format
Declarationsection
Processingsection
Format Explanation{comment} Used for comments.
● Process 1
● Process 2
Indicates selective processing.
If the conditionalexpression is true, Process1 is executed; if theconditional expression isfalse, Process 2 isexecuted.
● Process
Indicates a loop with thetermination condition attop.
The process is executedas long as the conditionalexpression is true.
Conditionalexpression
Conditionalexpression
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 2/15
Example 1 “2 Branch”When mathematics score is less than 60, the student gets imposed
report problem.
SCORE 60
Mathematics Exam.
Report Problem
Yes
No
Declaration
section
○ Integer Variable: SCORE
● SCORE ← Mathematics Mark
Processing
sectionScore 60● Report Problem
● Do Nothing
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 3/15
Example 2 “3 Branch”When mathematics score is less than 60, and English score is less
than 60, the student gets imposed report problem.
Math. SCORE 60
Mathematics Exam.
Yes
No
English Exam.
Eng. SCORE 60
ReportProblem
Yes
No
Declaration
section
○ Integer Variable: Math.SCORE,Eng.SCORE
● Math.SCORE ← Mathematics Mark
Processing
section
Math.Score 60
● Report Problem
● Do Nothing
● Eng.SCORE ← English Mark
Eng.Score 60
● Do Nothing
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 4/15
When mathematics score is less than 60, or English score is less
than 60, the student gets imposed report problem.
Example 3 “3 Branch”
Math. SCORE 60
Mathematics & English Exam.
Yes No
ReportProblem
Yes
No
Eng. SCORE 60
ReportProblem
Declaration
section
○ Integer Variable: Math.SCORE,Eng.SCORE
● Math.SCORE ← Mathematics Mark
Processing
section
Math.Score 60● Report Problem
● Do Nothing
● Eng.SCORE ← English Mark
Eng.Score 60
● Do Nothing
● Report Problem
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 5/15
Example 4 “3 Branch”When mathematics score is greater than equal 80 evaluation A,
when score is less than 80 and greater than equal 60 evaluation B,when score is less than 60 then evaluation C.
Declarati
onsection
○ Integer Variable: Math.SCORE
● Math.SCORE ← Mathematics Mark
Processing
section
Math.Score 80≧
● Evaluation A
● Evaluation C
Math.Score 60≧
● Evaluation B
Math. SCORE
Mathematics Exam.
80 Score≦ Score 60
Evaluation A
60 Score≦ 80
Evaluation B
Evaluation C
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 6/15
Example 5 “Repeat”Variable x has an initial value 0. And add 1 to x. It repeats addition
while x is not 5 (until x becomes 5).
X ≠ 5
X ← 0
No
Yes
X ← X+ 1
Declaration
section
○ Integer Variable: X
● X ← 0
Processing
section
X ≠5
● X = X + 1
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 7/15
Example 6 “Loop”Variable x has an initial value 0. And add 3 to x. It repeats addition 5
times.
Y 5
X ← 0, Y ← 0
No
Yes
X ← X + 3
Y ← Y + 1
Y becomes the Loop Counter.
Declaration
section
○ Integer Variable: X, Y
● X ← 0
Processing
section Y 5
● X = X + 3
● Y ← 0
● Y = Y + 1
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 8/15
Example 7 “Loop”Calculate the sum of odd number 1, 3, 5, 7, and 9.
Declaration
section
○ Integer Variable: X, Y
● X ← 0
Processing
section Y 9≦
● X = X + Y
● Y ← 1
● Y = Y + 2
X ← 0, Y ← 1
X ← X + Y
Y ← Y + 2
Looptill Y > 9
Loop
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 9/15
Practice 1
Sum of integer 1 to 10
i 10≦
Sum ← 0, i ← 1
No
Yes
Sum ← Sum + i
i ← i + 1
Declaration
section
○ Integer Variable: Sum, i
● Sum ← 0
Processing
section i 10≦
● Sum = Sum + i
● i ← 1
● i = i + 1
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 10/15
Example 8 “Loop Array”There are four money savings boxes. Their name are "week1",
"week2", "week3", and "week4".Get the total amount of money at the end of the month.
i 4≦
Save(1) ← 120, Save(2) ← 340Save(3) ← 230, Save(4) ← 180
Sum ← 0, i ← 1
No
Yes
Sum ← Sum + Save(i)
i ← i + 1
Declaration
section
○ Integer Variable: Sum, i
● Save(1)← 120 ~ Save(4) ← 180
Processing
section i 4≦
● Sum = Sum + Save(i)
● Sum ← 0, i ← 1
● i = i + 1
○ Integer Array: Save(4)
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 11/15
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 12/15
Example 7 “Loop”Calculate the sum of odd number 1, 3, 5, 7, and 9.
Y 9≦
X ← 0, Y ← 1
No
Yes
X ← X + Y
Y ← Y + 2
Declaration
section
○ Integer Variable: X, Y
● X ← 0
Processing
section Y 9≦
● X = X + Y
● Y ← 1
● Y = Y + 2
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 13/15
Practice 2
As for array T(n), a different inascending order value is stored.Variable "data" was given now.Look for the same value as "data"from this array by the binarysearch method.
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 14/15
Practice 2
T(1) T(2) T(3) … … … T(n-1) T(n)
low ← mid →high
Before
T(1) T(2) T(3) … T(mid) … T(n-1) T(n)
After compare data & T(mid)
low ←→high low ← →highdata < T(mid)
data > T(mid)
data = T(mid)
mid =(low + high) 2 (ignore decimal fractions.)
Target found
8/8/2019 PM Chap 6 - Pseudo-Code
http://slidepdf.com/reader/full/pm-chap-6-pseudo-code 15/15
Practice 2
data = T(mid)
idx ← 0, low ← 1, high ← n
Yes
idx ← mid
Looplow high or idx ≠ 0
Loop
mid ← (high + Low)/2
data T(mid)
Yeslow ← mid + 1
No
No
high ← mid 1
○ Integer Variable:idx, low, high, mid
● idx ← 0
low high and≦
idx = 0● mid ←
(low + high)/2
● low ← 1● high ← n
data = T(mid)
● idx ← mid
● low ← mid+2
● high←mid -1
data > T(mid)