Flowchart and algorithm
Flowchart and algorithm
Chapter 1
Algorithms and Flowcharts
Introduction
Algorithm is a step-by-step process of solving a well-defined computational problem. In
practice, in order to solve any complex real life problems, first we have to define the
problem and then, design algorithm to solve it. Writing and executing a simple
program may be easy; however, for executing a bigger one, each part of the program
must be well organized. In short, algorithms are used to simplify the program
implementation. The next step is making the flowchart. It is a type of diagram that
represents an algorithm or process, showing the steps as „boxes‟ of various kinds and
their order by connecting them with arrows. Then, the flowchart will be converted into
program code.
Algorithm
An algorithm is an effective method expressed as a finite list of well defined
instructions for calculating a function, starting from an initial state and initial input. The
instructions describe a computation, which will eventually produce output, when
executed. We can use algorithm to solve any kind of problems. However, before writing
a program, we need to write the steps to solve the problem in simple English language.
This step-by-step procedure to solve the problem is called algorithm.
Example
Let us take one simple day-to-day example by writing algorithm for making „Maggi
Noodles‟ as a food.
71
Step 1: Start
Further, the way of execution of the program shall be categorized into three ways: (i)
sequence statements; (ii) selection statements; and (iii) iteration or looping statements.
This is also called as „control structure‟.
Sequence statements: In this program, all the instructions are executed one after
another.
Example
Step 1: Start
Step 3: Stop
Example
Step 1: Start
72
Step 4: Print area
Step 5: Stop
In the above mentioned two examples (Example II and III), all the instructions are
executed one after another. These examples are executed under sequential statement.
Selective Statements: In this program, some portion of the program is executed based
upon the conditional test. If the conditional test is true, compiler will execute some part
of the program, otherwise it will execute the other part of the program.
Example
Step 1: Start
Step 3: Check age value, if age >= 18 then go to step 4 else step 5
Step 6: Stop
Example
Step 1: Start
73
Step 7: Print “Given number is zero”
Step 8: Stop
In the above mentioned examples IV and V, all the statements are not executed, but
based upon the input, some portions of the algorithm are executed, because we have
„true‟ or „false‟ situation in the program.
Iterative statements: In some programs, certain set of statements are executed again
and again based upon conditional test. i.e. executed more than one time. This type of
execution is called „looping or iteration‟.
Example
Step 1: Start
Step 3: Store 1 in I
Step 5: Print I
Step 5: Go to step 4
Step 8: Stop
In the above example, steps 4, 5, 6 and 7 are executed more than one time.
Flowchart
In the previous section of this chapter, we have learnt to write algorithms, i.e. step-by-
step process of solving a problem. We can also show these steps in graphical form by
using some symbols. This is called flowcharting.
Flowchart Symbols
Some of the standard symbols along with respective function(s) that are used for
making flowchart are as follows:
74
Symbols Functions
1. Start/stop
2. Input/output
3. Processing
4. Decision Box
5. Flow of control
6. Connector
Example
75
Solution:
Start
Input P,R,T
SI=P*R*T/100
Print SI
Stop
Example
Solution:
Start
Input A, B
No Yes
IS
A>B
Stop
76
The following are the examples (VIII & IX) of an iterative execution.
Example
Solution:
Start
Input N
I=1
F=1
Is I<=N
No Yes
Print F F=F*I
I=I+1
Stop
Example
77
Solution:
Start
Input n
Input A
I=1
Big =A
Big=A
No I=I+1
Is
I<=n Yes
Input A
Print Big
Is Big<A
Yes
Stop No
I=I+1
78
Example
Solution:
Start
Input N
I=1
No Yes
Is I<=N
Stop Print I
In the above example “I” value is not at all incremented, so it will create endless loop.
This is also called infinite loop.
Note: Set of statements is executed again and again without any end is called infinite
loop.
79
EXERCISE
3. Set of statements is executed again and again based upon conditional test.
1. Define Algorithm.
2. Define Flowchart.
80