Chapter 1.1 Problem Solving, Pseudocode Flowcharts
Chapter 1.1 Problem Solving, Pseudocode Flowcharts
1
Problem Solving
3
C++ Programming: From Problem Analysis to Program Design, Third Edition
Example
Design algorithm to find the perimeter and area of a
rectangle.
Analyze the problem:
• To find the perimeter and area of a rectangle, we
need to know the rectangle’s length and width.
The perimeter and area of rectangle is given by the
following formula:
Perimeter = 2 . (length + width)
Area = length . width
4
Example (continues)
Design algorithm:
1. Get length of Rectangle.
2. Get width of Rectangle.
3. Find the perimeter using the following equation:
Perimeter = 2 . (length + width)
4. Find the area using the following equation:
Area = length . width
5
Levels of Program Development
6
11
End Start Oval -> Start/End
Input start
Salary
Overtime Read Salary
Processing
Total_Sal = Salary +Overtime
Read Overtime
Output
Total_Sal =
Total_Sal
Salary +Overtime
Print Total_Sal
End
Example 3
15
Pesudocode:
◼ Start the program
◼ Create a variable to hold a counter from 2 to 30.
◼ Initialize the counter to 2.
◼ Create a variable to hold the sum.
◼ Initialize the sum to zero.
◼ Loop While the counter is less-than-or-equal to 30
◼ add the counter to the sum
◼ add two to the counter.
◼ repeat until the counter reach 30
◼ Print the sum.
◼ End of program
Solution
17
Start
Counter=2, Sum=0
yes
no
Counter≤30
yes
Sum= Sum + Counter
Counter=Counter+2
Print Sum
End
Example 4
18
Input
bank balance.
transactions
Processing
balance = balance + transaction.
Output
balance
Solution
22
Start
Read balance
Read transaction
Transaction !=0
yes
no
Print balance
End
Example 6
23
Input
amount.
Processing
Check if amount is below 1000 → Zakat =0.
Check if amount is above 1000→ Zakat =(2.5/100) *
amount
Output
Zakat
Solution
25
Start
Read amount
yes
no
Amount > 1000
Print Zakat
End