ALGORITHMS AND FLOW CHART
9th Lecture
ALGORITHMS AND FLOWCHARTS
▪ A typical programming task can be divided into
two phases:
▪ Problem solving phase
▪ produce an ordered sequence of steps that describe
solution of problem
▪ this sequence of steps is called an algorithm
▪ Implementation phase
▪ implement the program in some programming
language
2
STEPS IN PROBLEM SOLVING
▪ First produce a general algorithm (one can use
pseudocode)
▪ Refine the algorithm successively to get step by step
detailed algorithm that is very close to a computer
language.
▪ Pseudocode is an artificial and informal language that
helps programmers develop algorithms. Pseudocode is
very similar to everyday English.
3
PSEUDOCODE & ALGORITHM
▪ Example 1: Write an algorithm to determine a
student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as
the average of four marks.
4
PSEUDOCODE & ALGORITHM
Pseudocode:
▪ Input a set of 4 marks
▪ Calculate their average by summing and dividing by 4
▪ if average is below 50
Print “FAIL”
else
Print “PASS”
5
PSEUDOCODE & ALGORITHM
▪ Detailed Algorithm
▪ Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
6
THE FLOWCHART
▪ (Dictionary) A schematic representation of a sequence of operations,
as in a manufacturing process or computer program.
▪ (Technical) A graphical representation of the sequence of operations
in an information system or program.
▪ Information system flowcharts show how data flows from source documents
through the computer to final distribution to users.
▪ Program flowcharts show the sequence of instructions in a single program or
subroutine. Different symbols are used to draw each type of flowchart.
7
FLOWCHARTS
▪ Flowcharts is a graph used to depict or show a step by step
solution using symbols which represent a task.
▪ The symbols used consist of geometrical shapes that are
connected by flow lines.
▪ It is an alternative to pseudocoding; whereas a pseudocode
description is verbal, a flowchart is graphical in nature.
8
THE FLOWCHART
A Flowchart
▪shows logic of an algorithm
▪emphasizes individual steps and their
interconnections
▪e.g. control flow from one action to the next
9
FLOWCHART SYMBOLS
Disk storage I/O symbol -
Name Symbol Use in Flowchart indicates input from or output
to disk storage.
Oval Denotes the beginning or end of the program
Printer output symbol -
shows hardcopy printer
output.
Parallelogram Denotes an input operation
Rectangle Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Diamond Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Hybrid Denotes an output operation
10
Flow line Denotes the direction of logic flow in the program
FLOWCHART – SEQUENCE FLOWCHART – SELECTION FLOWCHART – REPETITION
CONTROL STRUCTURE CONTROL STRUCTURE CONTROL STRUCTURE
Statement 1 No Yes
Condition
ye Loop
Statement 2 Condition
s Statement(s)
else- then- no
Statement 3
statement(s) statement(s)
11
EXAMPLE
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0
PRINT PRINT
“PASS” “FAIL”
STOP
12
EXAMPLE 2
▪ Write an algorithm and draw a flowchart to convert the
length in feet to centimeter.
Pseudocode:
▪ Input the length in feet (Lft)
▪ Calculate the length in cm (Lcm) by multiplying LFT with 30
▪ Print length in cm (LCM)
13
EXAMPLE 2
Flowchart
Algorithm START
▪ Step 1: Input Lft
Input
▪ Step 2: Lcm Lft x 30 Lft
▪ Step 3: Print Lcm Lcm Lft x 30
Print
Lcm
STOP
14
EXAMPLE 3
Write an algorithm and draw a flowchart that will read
the two sides of a rectangle and calculate its area.
Pseudocode
▪ Input the width (W) and Length (L) of a rectangle
▪ Calculate the area (A) by multiplying L with W
▪ Print A
15
EXAMPLE 3
Algorithm START
▪ Step 1: Input W,L
Input
▪ Step 2: AL x W W, L
▪ Step 3: Print A
ALxW
Print
A
STOP
16
FLOWCHART – EXAMPLE
Begin
Begin
sum = 0
current_number = 1
Read birth date
NO
Calculate current_number <= 10? print sum
Age = current year – birth date
YES
End
Display sum = sum + current_number
age current_number = current_number + 1
End
17
DECISION TABLES
Decision tables are a precise yet compact way to model
complicated logic.
Decision tables, like flowcharts and if-then-else and switch-
case statements, associate conditions with actions to
perform, but in many cases do so in a more elegant way.
WHY?
▪ Decision tables are used to lay out in tabular form all
possible situations which a business decision may
encounter and to specify which action to take in each of
these situations.
▪ You can use them in your projects to clarify complex
decision making situations and should find them useful in
your work as a computer professional.
19
▪ Decision Tables
▪ A decision table is a table
composed of rows and columns, Conditions Condition
separated into four separate
quadrants. Alternatives
▪
The upper left quadrant contains Actions Action
the conditions. The upper right Entries
quadrant contains the condition
rules of alternatives. The lower
left quadrant contains the
actions to be taken and the
lower right quadrant contains
the action rules.
20
▪ Decision Table
▪ A decision table is a tabular form that presents a set of conditions and
their corresponding actions.
▪ Condition Stubs
▪ Condition stubs describe the conditions or factors that will affect the
decision or policy. They are listed in the upper section of the decision
table.
▪ Action Stubs
▪ Action stubs describe, in the form of statements, the possible policy
actions or decisions. They are listed in the lower section of the decision
table.
▪ Rules
▪ Rules describe which actions are to be taken under a specific
combination of conditions. They are specified by first inserting
different combinations of condition attribute values and then putting
X's in the appropriate columns of the action section of the table.
21
DEFINITION
Combinations ▪ Components
Causes Values 1 2 3 4 5 6 7 8 ▪ A decision table lists
Cause 1 Y, N Y Y Y Y NNNN
Cause 2 Y, N Y Y NNY Y NN causes and effects in a
Cause 3 Y, N Y NY NY NY N matrix. Each column
Effects represents a unique
Effect 1 X X X combination.
Effect 2 X X X
▪ Purpose is to structure
logic
Cause = condition
Effect = action = expected results 22
APPLICATION AREAS
▪ Business Analysis
▪ Programming
▪ Testing
▪ Hardware Design
▪ etc
23
STEPS TO CREATE A DECISION TABLE
1. List all causes in the decision
table
2. Calculate the number of
possible combinations
3. Fill columns with all possible
combinations
4. Reduce test combinations
5. Check covered combinations
24
STEP 1: LIST ALL CAUSES
Hints:
Combinations
Causes Values 1 2 3 4 5 6 7 ▪8 Write down the values the
Cause 1
Cause 2
Y, N
Y, N
Y Y Y Y NNNN
Y Y NNY Y NN
cause/condition can assume
▪ Cluster related causes
Cause 3 Y, N Y NY NY NY N
Effects
▪X Put the most dominating
Effect 1 X X X
Effect 2 X X
cause first
25
STEP 2: CALCULATE COMBINATIONS
Number of Values to ▪ If all causes are simply Y/N
the power of the values:
number of causes 2number of causes
with these values ▪ If 1 cause with 3 values
and 3 with 2:
31 * 23 = 24
26
STEP 3 REDUCE COMBINATIONS
Often there are dependencies
between causes. For example Combinations ▪ Find indifferent
cause 1 can test if a field is Causes Values 1 2 3 4 5 6 7 8
entered (Y/N) and cause 2 can Cause 1 Y, N Y Y Y Y NNNN combinations –
test if the same field is valid
(Y/N). It is no use testing if the
Cause 2
Cause 3
Y, N
Y, N
Y Y NNY Y NN
Y N - - Y NY N
place a ‘-’
field is valid if it has not been Effects
entered. Effect 1 X X X
Effect 2 X X X
Join columns: When in a column Combinations
a cause is of no interest any Causes Values 1 2 3 4 5 6 7 Join columns where
more to determine the effects, a Cause 1 Y, N Y Y YNNNN
dash ‘-’ is placed in the column Cause 2 Y, N Y YNY YNN columns are identical
for that cause. This has to be
done for both the column with a
Cause 3
Effects
Y, N YN - YNYN
Tip: ensure the effects
‘Y’ and the column with a ‘N’. Effect 1 X X are the same
This makes the 2 columns Effect 2 X X X
identical and one of them can
be deleted.
27
▪ Condition 1Y Y
▪ Condition 2Y N
▪ Action 1X X
can be expressed as:
▪ Condition 1Y
▪ Condition 2-
▪ Action 1X
▪ The dash (-) signifies that condition 2 can be either Y or N
and action will still be taken.
28
COMPLETE DECISION TABLE FOR PAYROLL SYSTEM EXAMPLE
29
A SIMPLE EXAMPLE
▪ Scenario: A marketing company wishes to construct a
decision table to decide how to treat clients according to
three characteristics: Gender, City Dweller, and age group: A
(under 30), B (between 30 and 60), C (over 60). The company
has four products (W, X,Y and Z) to test market. Product W
will appeal to male city dwellers. Product X will appeal to
young males. Product Y will appeal to Female middle aged
shoppers who do not live in cities. Product Z will appeal to all
but older males.
30
IDENTIFY CONDITIONS & VALUES
▪ The three data attributes tested by the conditions in
this problem are
▪ gender, with values M and F;
▪ city dweller, with value Y and N; and
▪ age group, with values A, B, and C
▪ The maximum number of rules is 2 x 2 x 3 = 12
31
IDENTIFY POSSIBLE ACTIONS
▪The four actions are:
▪market product W,
▪market product X,
▪market product Y,
▪market product Z.
32
ENTER ALL POSSIBLE CONDITIONS
Rules
Proc 1 2 3 4 5 6 7 8 9 10 11 12
Sex m f m f m f m f m f m f
City y y n n y y n n y Y n n
Age a a a a b b b b c c c c
33
DEFINE ACTIONS FOR EACH RULE
Actions
Market 1 2 3 4 5 6 7 8 9 10 11 12
W x x X
X x X
Y X
Z x x x x x x x x X X
34
FALL TABLE
Rules
Proc 1 2 3 4 5 6 7 8 9 10 11 12
Sex m f m f m f m f m f m f
City y y n n y y n n y Y n n
Age a a a a b b b b c c c c
Actions
Market 1 2 3 4 5 6 7 8 9 10 11 12
W x x X
X x X
Y X
Z x x x x x x x x X X
35