Algorithm and Flowchart
204112 Structured Programming 1
Programming Methodology
Problem solving
• Problem statement and analysis
• Develop a high-level algorithm
• Detail out a low-level algorithm
Coding
• Choose a programming language
• Code the program using the selected algorithm
• Test the program and correct the errors
204112 Structured Programming 2
Algorithm
Definition – Solution to a computer programming
problem.
Algorithm can be written in 2 different ways
Pseudo-code – English-like steps that
describes the solution
Flowcharts – Picture with specific blocks
detailing out the logical flow of the solution
204112 Structured Programming 3
Flowchart Building Blocks
CONTROL FLOW
TERMINAL POINT - Start / End
PROCESS - Initializing, Calculation ...
INPUT / OUTPUT - Keyboard, Display ...
DECISION
CONNECTOR - used for big diagram across pages
PRINTOUT
STORAGE - Read or Write from CDs, Disks, Tapes
`
SUB-ROUTINE
204112 Structured Programming 4
Start
Example 1
Switch on the TV
Problem Statement Switch on the UBC
Watch a movie at home
Change to the
Algorithm
movie channel
1. Switch on the TV and
UBC sets
2. Change to the required
movie channel
Sit down
Watch movie
3. Sit down and watch the
movie
End
204112 Structured Programming 5
Start
Example 2 Go to the ATM
Insert your card into
Problem Statement the ATM machine
Withdraw cash from ATM
Press in your code
Algorithm
1. Go to the ATM
2. Insert your card into the Choose Withdraw
machine Enter the amount
3. Press in your code
4. Choose “Withdraw” and
enter Amount required Take the cash
Take the slip
5. Take the cash, slip and Take the card
card.
204112 Structured Programming End 6
Start
Example 3 Input Amount
Problem Statement Input Years
Calculate the interest of a
bank deposit. You are to
read the amount, years
and interest rate from the Input Rate
keyboard and print the
interest amount.
Compute Interest <-
Amount * Years * Rate
Algorithm / 100
1. Read Amount
2. Read Years
3. Read Rate
Output Interest
4. Set Interest as Amount *
Rate * Years / 100
5. Print Interest
End
204112 Structured Programming 7
Example 3 – Input/Output Samples
Inputs Outputs
Amount = 5000 Interest = 200
Years = 2
Rate = 2
Amount = 1000 Interest = 37.50
Years = 1.5
Rate = 2.5
204112 Structured Programming 8
Example 4
Start
Problem Statement
Print what to do when Input Signal
driving to a traffic signal
Yes No
Signal = GREEN ?
Algorithm
1. Read traffic signal
2. If signal is GREEN then Action <- GO Action <- STOP
Set Action as GO
Else
Set Action as STOP Output Action
3. Print Action
End
204112 Structured Programming 9
Example 4 – Input/Output Samples
Inputs Outputs
Signal = GREEN Action = GO
Signal = RED Action = STOP
Signal = YELLOW Action = STOP
Check what happens if Action =
Signal = BLUE
204112 Structured Programming 10
Example 5
Start
Problem Statement
Read a number from the keyboard. Input N
Check and output if a given
number N is ODD or EVEN Remainder <- N modulo 2
Algorithm Yes No
Remainder = 0 ?
1. Read N
2. Set Remainder as N modulo 2 Answer <- EVEN Answer <- ODD
3. If Remainder is equal to 0 then
Set Answer as EVEN
Else Output Answer
Set Answer as ODD
4. Print Answer
End
204112 Structured Programming 11
Example 5 – Input/Output Samples
Inputs Outputs
N=5 Answer = ODD
N=8 Answer = EVEN
N=0 Answer = EVEN
N = -1 Answer = ODD
204112 Structured Programming 12
Start
Example 6 Input Gender
No
Problem Statement Gender = Male ? Input Status
Print Title for a person (Either Mr. or
Miss. or Mrs.). You are to read the Yes
gender (and status if needed).
Status = Married ?
Title <- Mr.
Algorithm
1. Read Gender Yes No
2. If Gender is MALE then
Title is Mr.
Else Title <- Mrs. Title <- Miss
Read Status
If Status is MARRIED then
Title is Mrs.
Else
Title is Miss.
Output Title
3. Print Title
End
204112 Structured Programming 13
Example 6 – Input/Output Samples
Inputs Outputs
Gender = Male Title = Mr.
Gender = Female Title = Mrs.
Status = Married
Gender = Female Title = Miss.
Status = Single
Check what happens if Title =
Gender = Boy
Status = Intelligent
204112 Structured Programming 14
Example 7 Start
Initialize X <- 0
Problem Statement
Print 1 to 20 Increment X by 1
Print X
Algorithm
1. Initialize X as 0
2. Increment X by 1 Yes
` X < 20
3. Print X
4. If X is less than 20 then
go back to Step 2 No
End
204112 Structured Programming 15
Start
Example 8 Retrieve computer time
ExtractHOURS
Problem Statement Extract MINUTES
Given computer time is stored in 24
hours format, you are to print the No
time in AM/PM format HOURS = 0 ? 1 <= HOURS <= 12
Algorithm Yes Yes No
1. Retrieve computer time
2. Extract Hours and Minutes Output 12 Output HOURS Output (Hours - 12)
3. If Hours is equal to 0 then
Print 12
Else Output ":"
If Hours is between 1 and 12
then
Print Hours Output MINUTES
Else
Print Hours – 12 Yes No
4. Print „:‟ HOURS < 12
5. Print Minutes
6. If Hours is less than 12 then Output AM Output PM
Print AM
Else
Print PM
End
204112 Structured Programming 16
Example 8 – Input/Output Samples
Inputs Outputs
Computer time = 8:30 Printed time – 8:30 AM
Computer time = 20:30 Printed time – 8:30 PM
Computer time = 0:15 Printed time – 12:15 AM
Computer time = 12:15 Printed time – 12:15 PM
204112 Structured Programming 17
Start
Example 9 Input MONTH
YES NO
MONTH = 2 ?
Problem Statement
Read the Month (and Year, if needed) Input YEAR
and print the number of days in
that month
Set LEAP <- YEAR modulo 4 MONTH = 4 or
Algorithm YES MONTH = 6 or NO
1. Read MONTH MONTH = 9 or
MONTH = 11?
2. If MONTH is equal to 2 then
Read YEAR
NO YES
LEAP = 0 ?
If YEAR is a leap year then
Set DAYS as 29 Set DAYS <- 30 Set DAYS <- 31
Else
Set DAYS as 28
Else Set DAYS <- 28 Set DAYS <- 29
If MONTH is either 4 or 6 or 9 or 11
then
Set DAYS as 30
Else
Set DAYS as 31
3. Print DAYS
Output DAYS
204112 Structured Programming End 18
Example 9 – Input/Output Samples
Inputs Outputs
Month = 2 Days = 29
Year = 2004
Month = 2 Days = 28
Year = 2005
Month = 10 Days = 31
Month = 4 Days = 30
Check what happens if Days =
Month = -1
204112 Structured Programming 19
Example 10
Problem Statement Low-level Algorithm
Prepare sandwiches 1.1 Take the car keys and wallet from the counter
1.2 Drive the car to the supermarket
1.3 Park the car
1.4 Take the lift to the supermarket floor
High-level Algorithm
2.1 Take an empty cart and walk around the floor
1. Go to the nearest supermarket
2.2 Put the needed groceries into the cart
2. Pick the groceries you need
2.3 Take the cart to the cashier
3. Pay at the cashier
3.1 Give the credit card to the cashier
4. Bring the groceries home
3.2 Sign on the credit card slip
5. Prepare the sandwiches
4.1Take the cart with the plastic bags to the car
4.2 Put the plastic bags to the car
4.3 Drive the car home
4.4 Remove the plastic bags from the car
5.1 Cut the bread into half
5.2 Prepare the bacon and salad
5.3 Put the ingredients between 2 slices of bread
204112 Structured Programming 20
Example 11
Problem Statement Low-level Algorithm
Make an urgent call to your 1.1 Walk to the next phone booth
friend from the airport 1.2 If phone booth is not working, then repeat
from step 1.1
2.1 Retrieve the number from pocket diary
High-level Algorithm 2.2 Put some coins into the slot.
1. Go to a public booth 2.3 Dial the number
2. Dial your friend‟s number 2.4 If the line is busy, hang up, then take
back the coins and repeat from step 2.2
3. Give the message to your
friend 3.1 If your friend can come to the phone, then
talk to your friend.
3.2 If your friend cannot come to the phone,
then leave a message for your friend.
3.2 Hang up the phone.
3.4 Retrieve any coins not used.
204112 Structured Programming 21
Start
Example 11 Walk to next phone booth
NO
Phone working ?
YES
Retrieve Number from Pocket Diary
Put some coins into the slot
Dial the number
YES
Line is busy ?
NO
YES NO
Friend found ?
Talk to friend Leave message
Hang up the phone
Retrieve the unused coins
204112 Structured Programming End 22
Example 12
Problem Statement Low-level Algorithm
Automatically return change for a 1.1 Read N
purchase of N baht when given 1.2 If NOT (1 <= N <= 20) then
a 20 baht note. Check that N is Print Error Message
between 1 and 20. Go back to Step 1.1
2.1 Initialize CHANGE as 20
High-level Algorithm 2.2 Deduct N from CHANGE
1. Read and Validate N 3.1 If CHANGE is less than 10 then
2. Calculate Change Number of 10 baht coin is 0.
3. Decide how many 10 baht Else
coins, 5 baht coins and 1 baht Number of 10 baht coin is 1.
coins to return Deduct 10 from CHANGE
3.2 If CHANGE is less than 5 then
Number of 5 baht coin is 0.
What happens if customer can pay by any Else
kinds of banknotes: 1000, 500, 100, 20, and Number of 5 baht coin is 1.
10. and any kinds of coins: 10, 5, 2, and 1. Deduct 5 from CHANGE
That means N is not be fixed. 3.3 Number of 1 baht coin is CHANGE
204112 Structured Programming 23
Start
Example 12 Input N
Output Error
Check what happens if N = 20 N >= 1 and N <= 20
NO
Check what happens if N = 0 YES
Check what happens if N = 21
Set CHANGE <- 20 - N
YES NO
CHANGE < 10 ?
Set COIN10 <- 1
Set COIN10 <- 0
Set CHANGE <- CHANGE - 10
YES NO
CHANGE < 5 ?
Set COIN05 <- 1
SET COIN05 <- 0
SET CHANGE <- CHANGE - 5
Set COIN01 <- CHANGE
204112 Structured Programming End 24
Example 12 – Input/Output Samples
Inputs Outputs
N = 17 Number of 10 B coin – 0
Number of 5 B coin – 0
Number of 1 B coin – 3
N=6 Number of 10 B coin – 1
Number of 5 B coin – 0
Number of 1 B coin – 4
N = 13 Number of 10 B coin – 0
Number of 5 B coin – 1
Number of 1 B coin – 2
Check what happens if N = 20
Check what happens if N = 0
Check what happens if N = 21
204112 Structured Programming 25
Example 13
Problem Statement Low-level Algorithm
Find the average of a given 1. Initialize SUM as 0 and COUNT as 0
list of numbers 2. If there are no more numbers
remaining to be processed, then go
to step 7.
3. Set ITEM as next number in the list
High-level Algorithm 4. Add ITEM to SUM
1. Find the SUM of the 5. Increment COUNT by 1
given numbers
6. Go back to step 2
2. Find the COUNT of the
7. If COUNT is equal to 0, then
given numbers
AVERAGE is “undefined”
3. AVERAGE is SUM †
COUNT Else
AVERAGE is SUM † COUNT
204112 Structured Programming 26
Start
Example 13 Set SUM <- 0
Set COUNT <- 0
NO
Any more unprocessed numbers ?
YES
Set ITEM <- Next number in list
Set SUM <- SUM + ITEM
Set COUNT <- COUNT + 1
YES NO
COUNT = 0 ?
Set AVERAGE = SUM / COUNT
Output "Undefined" Error Output AVERAGE
204112 Structured Programming End 27
Example 13 – Input/Output Samples
Inputs Outputs
List = 20, 2, 5, -3 Average = 6
List = 2, 5, -3, -8, -1 Average = -1
List = 2, 7, 5, 3, 6 Average = 4.60
List = 4 Average = 4
List = Average = “undefined”
204112 Structured Programming 28
Example 14
Problem Statement
Given a 2-D polygon with N sides (and N vertices). Find the smallest
rectangular box required to cover the polygon completely
Algorithm
1. Initialize MINX, MINY, MAXX, MAXY using the 1st Vertex
2. Retrieve the next unevaluated vertex (X, Y)
3. If X < MINX, then set MINX as X
4. If X > MAXX, then set MAXX as X
5. If Y < MINY, then set MINY as Y
6. If Y > MAXY, then set MAXY as Y
7. If all vertices have not been evaluated then go back to step 2
8. Set LENGTH as MAXX – MINX
9. Set HEIGHT as MAXY – MINY
204112 Structured Programming 29
Start
Get the 1st Vertex (X,Y)
Example 14 Set MINX <- X
Set MAXX <- X
Set MINY <- Y
Set MAXY <- Y
Get the next Vertex (X,Y) A
YES
X < MINX Set MINX <- X
NO
B
YES
X > MAXX Set MAXX <- X
NO
Set LENGTH <- MAXX - MINX
Set HEIGHT <- MAXY - MINY
YES
Y < MINY Set MINY <- Y
End NO
YES
Y > MAXY Set MAXY <- Y
NO
NO YES
B Any unevaluated vertex left ? A
204112 Structured Programming 30
Example 14 – Input/Output Samples
Inputs Outputs
4 sides (2,2) (5,3) (3,5) (6,2) Length = 4
Height = 3
3 sides (1,2) (5,3) (8, -2) Length = 7
Height = 5
5 sides (2,5) (7,1) (3,2) (-3, -5) Length = 10
(4,1) Height = 10
204112 Structured Programming 31