Flow Charts

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

3

FLOW CHARTS AND FUNDAMENTALS OF


ALGORITHMS

3.1 Flow Chart


Simply said, flow chart is a block diagram, describing an algorithm. It displays
the sequence of steps that you need to do to complete a given task.
As the definition says it describes an algorithm. It is the universal method to
visualize any sequence of steps. Thus it is used in programming, engineering,
economics etc.

3.2 Flow chart usage


Every good programmer should be able to use them. Why? Because they are very
powerful and convenient. Once you have the complete algorithm on a chart it is
just a matter of writing the code in the language you want. And as we already
explained writing the code is just one of the steps in the software development
process. In my opinion it is the easiest step.

3.3 Example
Here is a very simple example:

Looking at the example above you could ask “Do I really need to create a plan for
such easy tasks? In practice – no, but when learning – yes. First of all, in practice
as a programmer you will not solve such easy tasks. These examples will be just
a very small fraction of the program. Then why do them? Because “you have to
learn to walk, before you start running”.

3.4 Flowchart In Programming


Flowchart is a diagrammatic representation of an algorithm. Flowchart are very
helpful in writing program and explaining program to others.

3.4.1 Symbols Used In Flowchart


Different symbols are used for different states in flowchart, for example: Input
/Output and decision making has different symbols. The table below describes
all the symbols that are used in making flowchart

Symbol Purpose Description

Used to indicate the flow of logic by


Flow line
connecting symbols.

Used to represent start and end of


Terminal(Stop/Start)
flowchart.

Input /Output Used for input and output operation.

Used for arithmetic operations and


Processing
data-manipulations.
Symbol Purpose Description

Used to represent the operation in


Decision which there are two alternatives, true
and false.

On-page Connector Used to join different flowline

Used to connect flowchart portion on


Off-page Connector
different page.

Used to represent a group of


Predefined
statements performing one processing
Process/Function
task.

3.5 Examples of flowcharts in programming

1) Draw a flowchart to add two numbers entered by user.


2) Draw flowchart to find the largest among three different numbers entered
by user.

3) Draw a flowchart to find all the roots of a quadratic equation ax 2+bx+c=0


4) Draw a flowchart to find the Fibonacci series till term≤1000.

Though, flowchart are useful in efficient coding, debugging and analysis of a


program, drawing flowchart in very complicated in case of complex programs and
often ignored.

3.6 Algorithm in Programming


In programming, algorithm are the set of well-defined instruction in sequence to
solve a program. An algorithm should always have a clear stopping point.

3.6.1 Qualities of a good algorithm

1. Inputs and outputs should be defined precisely.


2. Each steps in algorithm should be clear and unambiguous.
3. Algorithm should be most effective among many different ways to solve a
problem.
4. An algorithm shouldn't have computer code. Instead, the algorithm
should be written in such a way that, it can be used in similar
programming languages.
3.7 Examples of Algorithms In Programming

1) Write an algorithm to add two numbers entered by user.

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

Step 6: Stop

2) Write an algorithm to find the largest among three different numbers


entered by user.

Step 1: Start

Step 2: Declare variables a,b and c.

Step 3: Read variables a,b and c.

Step 4: If a>b

If a>c

Display a is the largest number.

Else

Display c is the largest number.


Else

If b>c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: Stop

3) Write an algorithm to find all roots of a quadratic equation ax 2+bx+c=0.

Step 1: Start

Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;

Step 3: Calculate discriminant

D←b2-4ac

Step 4: If D≥0

r1←(-b+√D)/2a

r2←(-b-√D)/2a

Display r1 and r2 as roots.

Else

Calculate real part and imaginary part

rp←b/2a
ip←√(-D)/2a

Display rp+j(ip) and rp-j(ip) as roots

Step 5: Stop

4) Write an algorithm to find the factorial of a number entered by user.

Step 1: Start

Step 2: Declare variables n,factorial and i.

Step 3: Initialize variables

factorial←1

i←1

Step 4: Read value of n

Step 5: Repeat the steps until i=n

5.1: factorial←factorial*i

5.2: i←i+1

Step 6: Display factorial

Step 7: Stop

5) Write an algorithm to check whether a number entered by user is prime


or not.

Step 1: Start
Step 2: Declare variables n,i,flag.

Step 3: Initialize variables

flag←1

i←2

Step 4: Read n from user.

Step 5: Repeat the steps until i<(n/2)

5.1 If remainder of n÷i equals 0

flag←0

Go to step 6

5.2 i←i+1

Step 6: If flag=0

Display n is not prime

else

Display n is prime

Step 7: Stop

6) Write an algorithm to find the Fibonacci series till term≤1000.

Step 1: Start
Step 2: Declare variables first_term,second_term and temp.

Step 3: Initialize variables first_term←0 second_term←1

Step 4: Display first_term and second_term

Step 5: Repeat the steps until second_term≤1000

5.1: temp←second_term

5.2: second_term←second_term+first term

5.3: first_term←temp

5.4: Display second_term

Step 6: Stop

Algorithm is not the computer code. Algorithm are just the instructions which
gives clear idea to write the computer code.

3.8 Flow of C Program

The C program follows many steps in execution. To understand the flow of C


program well, let us see a simple program first.

File: simple.c

1. #include <stdio.h>
2. {
3. void main()
4. printf("Hello ");
5. }

3.8.1 Execution Flow


Let's try to understand the flow of above program by the figure given below.
1) C program (source code) is sent to preprocessor first. The preprocessor is
responsible to convert preprocessor directives into their respective values. The
preprocessor generates an expanded source code.

C Preprocessor directives: Before a C program is compiled in a compiler,


source code is processed by a program called preprocessor. This process is
called preprocessing. Commands used in preprocessor are called preprocessor
directives and they begin with “#” symbol.

2) Expanded source code is sent to compiler which compiles the code and
converts it into assembly code.

A compiler is a computer program (or a set of programs) that transforms source


code written in a programming language (the source language) into another
computer language (the target language), with the latter often having a binary
form known as object code.

3) The assembly code is sent to assembler which assembles the code and
converts it into object code. Now a simple.obj file is generated.

An assembler is a program that takes basic computer instructions and converts


them into a pattern of bits that the computer's processor can use to perform its
basic operations. Some people call these instructions assembler language and
others use the term assembly language.

4) The object code is sent to linker which links it to the library such as header
files. Then it is converted into executable code. A simple.exe file is generated.

A linker is a computer program that takes one or more object files generated by
a compiler and combines them into one, executable program. Computer
programs are usually made up of multiple modules that span separate object
files, each being a compiled computer program.

5) The executable code is sent to loader which loads it into memory and then it
is executed. After execution, output is sent to console.

A loader is the part of an operating system that is responsible for loading


programs and libraries. It is one of the essential stages in the process of starting
a program, as it places programs into memory and prepares them for execution.

You might also like