FLOW CHARTING
Nihar Ranjan Roy
What is a Flowchart?
A flowchart is a diagram that depicts the flow of a program. The figure shown here is a flowchart for the addition of two numbers.
START Display message Please enter two numbers Read two Numbers Calculate the Sum Display Sum END
Nihar Ranjan Roy 2
Basic Flowchart Symbols
Notice there are three types of symbols in this flowchart: rounded rectangles parallelograms a rectangle Each symbol represents a different type of operation.
Name ovals or rounded rectangles (terminals) Arrows parallelograms Rectangle Meaning Start or end of a process Flow of control Processing (indicates a process such as a mathematical computation or variable assignment)
Nihar Ranjan Roy 3
Symbol
Input/output
Flow of control in flow chart
Please enter two numbers START
Output operation
Display message Please enter two numbers Read two Numbers
Calculate the Sum
Display Sum
END
Nihar Ranjan Roy
Flow of control in flow chart
Please enter two numbers 12 13 START
Display message Please enter two numbers
Read operation
Read two Numbers
Calculate the Sum
Accept the value and store them into variables Fno Sno
Display Sum
END
Nihar Ranjan Roy
Flow of control in flow chart
Please enter two numbers 12 13 START
Display message Please enter two numbers Read two Numbers
Processing /calculation
Processing/calculate Sum=Fno+Sno
Calculate the Sum
Display Sum
END
Nihar Ranjan Roy
Flow of control in flow chart
Please enter two numbers 12 13 25 START
Display message Please enter two numbers Read two Numbers
Calculate the Sum
Output
Display Sum
END
Nihar Ranjan Roy
Four Flowchart Structures
Sequence Decision Repetition Case
Nihar Ranjan Roy
Sequence Structure
A series of actions are performed in sequence The addition of two numbers was a sequential activity
Nihar Ranjan Roy
Decision Structure
One of two possible actions is taken, depending on a condition. A new symbol, the diamond, indicates a yes/no question. If the answer to the question is yes, the flow follows one path. If the answer is no, the flow follows another path
NO
YES
Nihar Ranjan Roy
10
PROBLEM
Accept two numbers and find the square of the biggest no
Nihar Ranjan Roy
11
Decision Structure
In the flowchart segment below, the question is x < y? is asked. If the answer is no, then process A is performed. If the answer is yes, then process B is performed.
NO x < y?
YES
Code in C
If (x<y) Square=y*y; Else Square=x*x;
Square=y*y Square=x*x
Nihar Ranjan Roy
12
Repetition Structure A repetition structure represents part of the program that repeats. This type of structure is commonly known as a loop.
Nihar Ranjan Roy
13
Repetition Structure
Notice the use of the diamond symbol. A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists.
YES Condition?
No
Nihar Ranjan Roy
14
Repetition Structure
In the flowchart segment, the question is x < y? is asked. If the answer is yes, then Process A is performed. The question is x < y? is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited.
YES x < y?
Do some Processing
Nihar Ranjan Roy
15
PROBLEM
Print your name 5 times
Nihar Ranjan Roy
16
Repetition Structure
The flowchart segment below shows a repetition structure expressed in C as a while loop.
i=0
Flowchart
YES
Print name i=i+1
C Code
i=0; while (i < 5) { puts(nihar Ranjan roy); i=i+1; }
Nihar Ranjan Roy 17
i< 5?
Caution: Controlling a Repetition Structure
The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created. In this flowchart if i is never changed. Once the loop starts, it will never end. QUESTION: How can this flowchart be modified so it is no longer an infinite loop? i=0;
C Code
while (i < 5) { puts(nihar Ranjan roy); }
YES i< 5?
Print name
Nihar Ranjan Roy
18
Controlling a Repetition Structure
ANSWER: By adding an action within the repetition that changes the value of i.
i < 5?
YES
Print name
i=i+1
Nihar Ranjan Roy
19
Case Structure
One of several possible actions is taken, depending on the contents of a variable.
Nihar Ranjan Roy
20
Case Structure
The structure below indicates actions to perform depending on the value in years_employed.
CASE years_employed
1
bonus = 100
2
bonus = 200
3
bonus = 400
Other
bonus = 800
Nihar Ranjan Roy
21
Case Structure
If years_employed = 2, bonus is set to 200 If years_employed = 1, bonus is set to 100
If years_employed = 3, bonus is set to 400
CASE years_employed
If years_employed is any other value, bonus is set to 800
1
bonus = 100
2
bonus = 200
3
bonus = 400
Other
bonus = 800
Nihar Ranjan Roy
22
Connectors
Sometimes a flowchart will not fit on one page. A connector (represented by a small circle) allows you to connect two flowchart segments.
A
Nihar Ranjan Roy 23
Connectors
The A connector indicates that the second flowchart segment begins where the first segment ends.
START
END A
Nihar Ranjan Roy
24
Modules
A program module (such as a function in C) is represented by a special symbol.
Nihar Ranjan Roy
25
Modules
The position of the module symbol indicates the point the module is executed. A separate flowchart can be constructed for the module.
START
Read Input.
Call calc_pay function.
Display results.
END
Nihar Ranjan Roy
26
Problem
Design an algorithm and flow chart for converting an integer number to its binary equivalent.
Nihar Ranjan Roy
27