You are on page 1of 34

Algorithm and

Flowchart
SHALINI BHASKAR BAJAJ
Solving Problems : Abstraction
Abstraction is the act of representing essential features without including the
background details or explanations.
Abstractions may be formed by reducing the information content of a concept or an
observable phenomenon.
E.g.: addressing a person by his/her NAME not by their individual body parts/features.
To solve any computational problem we need to represent the problem in an
abstract form. This abstract form is known as Algorithm.
Thus, the abstraction principle is used to reduce complexity and allow efficient
design and implementation of complex software systems

SHALINI BHASKAR BAJAJ 2


Solving Computational Problems
To solve any computational problem we need to do following activities in
sequence:

◦ Understand the Problem statement.


◦ Find data abstraction.
◦ Design algorithm for above problem.
◦ Draw flow chart for above designed algorithm.
◦ Write a C program for the given flowchart.
◦ Then compile the written program on available compilers

SHALINI BHASKAR BAJAJ 3


What Is An Algorithm?
The steps needed to solve a problem.

Characteristics/ Desirable features of an algorithm:


✓Each step of the algorithm should be simple.
✓Logic of the algorithm should be crisp and clear (unambiguous).
✓It must end in a finite number of steps.
✓It should be efficient and effective.
✓Language independent

SHALINI BHASKAR BAJAJ 4


What Is An Algorithm?
◦ A computer algorithm is a detailed set of instructions, when executed
sequentially, solves a problem
Market
Market
AUH
Hospital

Playground
Gym

SHALINI BHASKAR BAJAJ 5


What Is An Algorithm?
AUH Market
Hospital

Playground Gym
Algorithm:
1. Go straight.
2. Take left turn.
3. you will reach AUH.
SHALINI BHASKAR BAJAJ 6
What Is An Algorithm?
1. Go straight

SHALINI BHASKAR BAJAJ 7


What Is An Algorithm?
1. Go straight
Market
AUH

Hospital

Playground
Gym
SHALINI BHASKAR BAJAJ 8
What Is An Algorithm?
2. Take Left turn
AUH Market
Market

Hospital

Playground
Gym
SHALINI BHASKAR BAJAJ 9
What Is An Algorithm?
2. Take left turn WRONG
Market
AUH
Market
Hospital

Playground
Gym
SHALINI BHASKAR BAJAJ 10
What Is An Algorithm?
2.Take left turn WRONG
Market
Market
AUH

Hospital

Playground
Gym
SHALINI BHASKAR BAJAJ 11
What Is An Algorithm?
◦ A computer algorithm is a detailed set of instructions when executed sequentially solves a
problem
WRONG

◦ An Algorithm is a sequence of unambiguous instructions for solving a problem in a finite


amount of time.
or
◦ The set of rules (unambiguous statements) that define how a particular problem can be solved
in finite sequence of steps is known as algorithm.

Algorithm:
1. Go straight.
2. Take 1st left turn. (no ambiguity)
3. you will reach AUH.

SHALINI BHASKAR BAJAJ 12


What Is An Algorithm?
2. Take 1st Left
turn Market
AUH

Hospital

Playground
Gym
SHALINI BHASKAR BAJAJ 13
How to develop an Algorithm ?

Steps to be followed:

1. Understand the problem


2. Identify the output of the problem
3. Identify the inputs required by the problem to reach the desired output
4. Design a logic to produce the desired result
5. Test the algorithm
6. Repeat step 1 to 5 till desired results are produced

SHALINI BHASKAR BAJAJ 14


Writing an Algorithm ?
Q. Given two memory locations X and Y exchange the values assigned to them
Step 1 Step 2 X Y Step1. Read the values of X and Y
Step 2. Copy the value of X in Temp
X Y 48 23 Step 3. Copy the value of Y in X
Step 4. Copy the value of Temp in Y
48 23 Step 5. Display the exchanged values
Temp Step 6. Stop
48
Step 3

X Y Step 4 X Y Step 5

23 23 23 48 X Y

23 48
Temp Temp

48 48

SHALINI BHASKAR BAJAJ 15


Writing an Algorithm ?
Q. Given two memory locations X and Y exchange the values assigned to them
without using temporary memory location
Step 1 Step 2
Step 1. Read the values of X and Y X Y X Y
Step 2. Add the contents of X and Y and store in X
Step 3. Subtract the contents of Y from X and 40 20 60 20
store the result in Y
Step 4. Subtract the contents of Y from X and
store the result in X
Step 5. Display the exchanged values of X and Y
Step 6. Stop Step 3 Step 4
X Y X Y
60 40 20 40
This solution is not good for non numeric
values

SHALINI BHASKAR BAJAJ 16


Algorithm for Making a Mobile call
Simply picking a mobile does not help us to make a call.
We need to follow certain actions in some order.
Following are the actions in the order of their access which are mainly required to call a
person by mobile phone.
1. Pick up the Mobile.
2. Open mobile MENU
3. Select phonebook/Contacts option.
4. Search appropriate Person to whom you want to call.
5. Select the searched contact.
6. Go to Option.
7. Press call button.

SHALINI BHASKAR BAJAJ 17


Algorithm : money withdrawal from ATM

Output
◦ Money, error messages

Inputs
◦ User Identification (ATM card), password, amount

SHALINI BHASKAR BAJAJ 18


ATM for money withdrawal
1. Get the ATM card for identification and ask for password.
2. Check password.
3. If password is not valid, generate an error message and go to step number 8.
4. Get the amount from the user.
5. Check the current balance.
6. If amount is greater than current balance, generate an error message and go to step number 8.
7. Subtract the amount from the balance and give out the cash.
8. Return the ATM card.
9. Stop.

SHALINI BHASKAR BAJAJ 19


FLOWCHART
•A Flowchart is a pictorial representation of an algorithm.

•The first flowchart is made by John in 1945.

•It is a symbolic diagram of operation sequence, dataflow, control flow and


processing logic in information processing.

•The symbol used are simple and easy to learn.

•It is a very help full tool for programmers and beginners.

SHALINI BHASKAR BAJAJ 20


Purpose of flowchart
•Provide Communication.
•Provide an overview.
•Show all elements and its relationship.
•Quick method of showing program flow.
•Check program logic.
• Provide Program documentation.

SHALINI BHASKAR BAJAJ 21


Symbols used in flowchart
All symbols are of different shape and size.

All have specific meaning and use.

These symbols are standardized for use.

SHALINI BHASKAR BAJAJ 22


BASIC SYMBOLS

Terminal Symbol
• start and stop

INPUT and OUTPUT


• Any function of input and output data.
Processing
• An arithmetic and data movement instruction.
• Shows mathematical calculation and logic
operations.
Loops
• For any kind of looping using For, While etc.
SHALINI BHASKAR BAJAJ 23
SYMBOLS

Decision
• Diamond indicate decision point in the program
flow. It may have 2 way branch or 3 way also.
Flow Lines
• A Straight line between two boxes shows the path of logic
flow in the program.
• An arrow head on the top of the line shows the direction of
the flow, data from top to bottom and left to right.
Connectors
• 2 small circles are used to connect separated portions of a
flowchart without drawing lines between the parts.
• One connector indicated where the flow breaks off and the
other indicates where it resumes.

SHALINI BHASKAR BAJAJ 24


ATM cash withdrawal process
Get Password Stop

No Return
Is Password Generate
Correct? ATM card
Error Message

Yes

Get amount

No
Is amount < Generate
Balance Error Message

Yes

Subtract amount
from balance Dispense cash

SHALINI BHASKAR BAJAJ 25


Advantages of flowchart
•It provides an easy way of communication because any other person besides
the programmer can understand the way they are represented.

•It represents the data flow.

•It provides a clear overview of the entire program and problem and solution.

•It checks the accuracy in logic flow.

•It documents the steps followed in an algorithm.

•It provides the way of modification of running program.

SHALINI BHASKAR BAJAJ 26


Limitation of flowchart
Complex logic.
Drawing is time consuming.
Alteration and modification.
Redrawn many times.
Difficult to draw and remember.

SHALINI BHASKAR BAJAJ 27


Algorithm and flowchart for area and
circumference of circle
Start
1. Start
Input r
2. Input r
3. Area = π * r* r Area = 3.14*r*r,
4. Circumference = 2 * π *r
 Circumference = 2* 3.14*r
5. Output Area, Circumference
6. Stop Print Area, Print
Circumference

Stop

SHALINI BHASKAR BAJAJ 28


Algorithm and flowchart to find whether
a number is odd or even
Start
1. Start
2. Input number n Input n
3. Compute num = n%2
4. Check value of num, if num =0, then Print “Even” else
num=n%2
Print “Odd”
5. Stop No
If num == 0
Yes
Print “Even” Print “Odd”

Stop

SHALINI BHASKAR BAJAJ 29


Algorithm and Flowchart for Greatest of Three Numbers
Algorithm : Start

1. Start
Read three numbers
2. Read A, B, C A,B,C
3. If A > B is True, then check
whether A > C, if yes then A
is greatest otherwise C is Yes No Yes Yes
greatest Is B>C ? Is A>B ? Is A>C ?

4. If A > B is False, then No No


check whether B > C, if yes Print B
then B is greatest otherwise Print C Print A
C is greatest
5. Stop

Stop

SHALINI BHASKAR BAJAJ 30


Algorithm and flowchart for product of
first n natural numbers
Start
1. Start
Read n
2. Set Product equal to 1
3. For i = 1 to n do Product=1
4. Product = Product * i
No Yes
5. Output Product For i = 1 to n
6. Stop
Product= Product * i
Print Product

Stop

SHALINI BHASKAR BAJAJ 31


Algorithm and flowchart to find the
factorial of a given number
Start
1. Start
2. Input N Input n
3. Fact = 1
Fact = 1
4. for i = 1 to n do
5. Fact = Fact *i No Yes
For i = 1 to n
6. Output Fact
7. Stop Print Fact
Fact = Fact * i
Stop

SHALINI BHASKAR BAJAJ 32


References:
• A. K. Sharma, Foundation of Computers & Programming in C, Dhanpat Rai publications
• Yashwant Kanitkar, Let Us C, BPB Publications
• E. Balagurusamy (2008), Computing Fundamentals And C Programming, Tata McGraw-Hill
• P. K. Sinha, Fundamentals of Computers, BPB Publications
• Yashwant Kanetkar, Solutions to Let us C
• Alexis Leon and Mathews Leon (2001), Introduction to Information Technology, Tata McGraw-Hill.
• Brian W. Kernighan and Dennis M. Ritchie, The C programming Language, Prentice-Hall
• R.G. Dromey (2001), How to Solve it by Computer, Prentice Hall of India.
• Al Kelley and Ira Pohl (1998), A Book on C Programming in C, 4th Edition, Pearson Education.
• Byron Gottfried, Programming with C, Schaum's Outline
• Foundation of Computing, P.K.Sinha & Priti Sinha, BPB Publications
• Wikipedia
• WWW

SHALINI BHASKAR BAJAJ 33


End of Chapter

SHALINI BHASKAR BAJAJ 34

You might also like