Professional Documents
Culture Documents
Lec 5 - Problem Solving I DR El Shimaa
Lec 5 - Problem Solving I DR El Shimaa
Lec 5 - Problem Solving I DR El Shimaa
-2-
Grading and Assessment
Assessment Marks
Final Written Exam 50
Midterm 15
Lab Quizzes 15
Attendance & Activities 5
Practical Exam 15
Lecture Slides/Notes.
Lab Slides
Text Book:
Problem Solving with C++ (9th
Edition). Walter Savitch, Pearson.
Download PDF
-4-
Contact Information
Office Hours:
Monday: 10 am – 11 am
Tuesday: 9 am – 10 am
Thursday: 10 am – 11 am
E-mail: a_alian@cis.asu.edu.eg
-5-
Lecture 5
Problem Solving
Problem Solving
-7-
What is an algorithm?
-8-
Designing Algorithms
-9-
Designing Algorithms (Cont.)
Flowchart Pseudocode
BEGIN
INPUT Number1, Number2
Sum = Number1 + Number2
PRINT Sum
END
-10-
Flowchart
-11-
Flowchart Symbols
-12-
Flowchart: General Rules
-14-
5
-15-
6
Algorithm
-16-
7
Flowchart Example
-17-
Pseudo-code
Keywords:
Input: Read/Get/Accept
Initialize: Set/Init
Processing: Compute/Calculate/Set/Increment
Output: Print/Display/Show
-18-
Pseudo-code Writing Rules
-19-
0
Pseudo-code Example
BEGIN
READ sheets
stamps = sheets/5
ROUND stamps to next integer
DISPLAY stamps
END
-20-
Pseudo-code: Example 2
Previous examples
followed sequence
structure
Sequence
Selection/Decision
Repetition
Read
BEGIN x, y, z
READ x, y, z
avg = (x+y+z)/3 avg = (x+y+z)/3
PRINT avg
END Print
avg
STOP
-24-
Sequence Structure: Example 2
Read
BEGIN width, length
READ width, length
area=length*width area = length * width
PRINT area
END Print
area
STOP
-25-
2. Selection Structure
IF-THEN STRUCTURE
The IF-THEN statement allows us to choose whether
to execute a set of program statements based on a
test.
-26-
Selection Structure
IF–THEN–ELSE STRUCTURE
IF condition THEN
True alternative
ELSE
False alternative
False True
alternative alternative
ENDIF
Operator Description
> Greater than
< Less than
= Equal to
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
-28-
Selection Structure : Example 1
STOP -29-
Selection Structure: Example 2
BEGIN Read
READ m1, m2, m3, m4 m1,m2,m3,m4
STOP -30-
Selection Structure: Example 3
END -31-
IF – ELSE IF Ladder
-32-
IF – ELSE IF Ladder
-33-
IF – ELSE IF Ladder
BEGIN
READ temp
IF temp <= 0 THEN
A program that provides WRITE “Frozen”
weather prediction ELSE IF temp <= 12 THEN
WRITE “Cold”
according to expected ELSE IF temp <= 25 THEN
WRITE “Warm”
temperature. ELSE IF temp <= 75 THEN
WRITE “Hot”
ELSE IF temp <= 100 THEN
WRITE “Very hot”
ELSE
WRITE “ Burning”
ENDIF
END
-34-
Nested IF
Multiple decisions
(if statement inside
another).
-35-
Nested IF: Example
-36-
Nested IF: Example
-37-
Exercise
LARGEST OF 3 NUMBERS.
True: The Largest
is Num1
True Num1>Num3?
False: The Largest
Num1>Num2 is Num3
? True: The Largest
False is Num2
Num2>Num3?
(Num2>=Num1) False: The Largest
is Num3
Solution - Pseudocode
IF Num1>Num2 THEN
IF Num1>Num3 THEN
Big = Num1
ELSE
Big = Num3
ENDIF
ELSE
IF Num2>Num3THEN
Big =Num2
ELSE
Big =Num3
ENDIF
ENDIF
-39-
Solution - Flowchart
-40-
Another Solution
BEGIN
READ Num1, Num2, Num3
Big = Num1
IF Num2 > Big THEN
Big = Num2
ENDIF
IF Num3 > Big THEN
Big = Num3
ENDIF
WRITE Big
END
-41-
Logical Operators
-42-
Problem: Largest of three numbers
BEGIN
READ Num1, Num2, Num3
IF Num1>Num2 AND Num1>Num3 THEN
Big = Num1
ELSE IF Num2>Num1 AND Num2>Num3 THEN
Big = Num2
ELSE
Big = Num3
ENDIF
WRITE Big
END
-43-
Case Selection
-44-
Case Selection
1 2 3 4
-45-
Case Selection
CASE OF VARIABLE
= constant1 :
actions for VARIABLE = constant1
= constants2 :
actions for VARIABLE = constant2
…
OTHERWISE:
Actions for VARIABLE = anything else
END-OF-CASE
-46-
Case Selection: Example 1
An algorithm that provides recommendations for
hotels by interpreting the hotel stars ratings made by
users.
CASE OF rate
= 1:
Display “very bad, not recommended”
= 2:
Display “bad choice”
= 3:
Display “good but could be better”
= 4:
Display “very good”
= 5:
Display “excellent, highly recommended”
OTHERWISE
Display “invalid entry”
END-OF-CASE
-47-
Case Selection: Example 1 Cont.
CASE OF
rate
=1 =2 =3 =4 =5 OTHERWISE
-48-
Another Solution: rate = 1
True Display
Using IF- ELSE IF “very bad”
False
IF rate = 1 THEN
True
Display “very bad, not recommended” Display “bad
rate = 2
ELSE IF rate = 2 THEN choice”
Display “bad choice”
ELSE IF rate = 3 THEN False
Display “good but could be better” True
Display
ELSE IF rate = 4 THEN rate = 3 “good”
Display “very good”
ELSE IF rate = 5THEN False
Display “excellent, highly recommended True
Display “very
ELSE rate = 4
good”
Display “invalid entry”
ENDIF False
True
rate = 5 Display
“excellent”
False
Display
“Invalid” -49-
Exercise: Complete
-50-
Practice – Design An algorithm for: