Algorithm - Flowcharts

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

Algorithm &

Flowcharts
Durjoy Kumar Paul
Lecturer, Department of
Mech. Eng., RUET,
Rajshahi- 6204.

CSE – 1281 (Computer and


Programming Language)
What is Algorithm?
❑ Algorithm is a sequence of activities to be processed for getting
desired output from a given input.

❑ Webopedia defines an algorithm as: “A formula or set of steps for


solving a particular problem. To be an algorithm, a set of rules must
be unambiguous and have a clear stopping point”.

❑ There may be more than one way to solve a problem, so there may be
more than one algorithm for a problem.

❑ Before writing an algorithm for a problem, one should find out what
is/are the inputs to the algorithm and what is/are expected output
after running the algorithm.
Symbols
❑ While writing algorithms we will use following symbol for different
operations:

1) ‘+’ for Addition

2) ‘-’ for Subtraction

3) ‘*’ for Multiplication

4) ‘/’ for Division and

5) ‘ = ’ for assignment. For example A = X*3 means A will have a value


of X*3.
Examples

Problem 1: Find the area of a Circle of Algorithm:


radius r. Step1: Read\input the Radius r of the
Inputs to the algorithm: Circle
Radius r of the Circle. Step2: Area PI*r*r // calculation of area
Expected output: Step3: Print Area
Area of the Circle
Examples
Problem2: Write an algorithm to read two numbers Algorithm:
and find their sum. Step1: Start
Step2: Read\input the first num1.
Inputs to the algorithm: Step3: Read\input the second num2.
First num1. Step4: Sum num1+num2 // calculation of sum
Second num2. Step5: Print Sum
Expected output: Step6: End
Sum of the two numbers.
Types of Algorithm
❑ The algorithm and flowchart, classified to the three types of control structures.
They are:
1) Sequence
2) Branching (Selection)
3) Loop (Repetition)

❑ Sequence: The sequence is exemplified by sequence of statements place one


after the other – the one above or before another gets executed first. In
flowcharts, sequence of statements is usually contained in the rectangular
process box.

❑ Bracnching: The branch refers to a binary decision based on some condition.


If the condition is true, one of the two branches is explored; if the condition is
false, the other alternative is taken. This is usually represented by the ‘if-then’
construct in pseudo-codes and programs. In flowcharts, this is represented by
the diamond-shaped decision box. This structure is also known as the
selection structure.
Types of Algorithm
❑ Loop: The loop allows a statement or a sequence of statements to be
repeatedly executed based on some loop condition. It is represented
by the ‘while’ and ‘for’ constructs in most programming languages, for
unbounded loops and bounded loops respectively. (Unbounded loops
refer to those whose number of iterations depends on the eventuality
that the termination condition is satisfied; bounded loops refer to
those whose number of iterations is known before-hand.) In the
flowcharts, a back arrow hints the presence of a loop.
Examples
Problem3: A algorithm to find the largest Problem4: Design an algorithm with a natural
value of any three numbers. number, n, as its input which calculates the
following formula and writes the result in
Step1: Start the standard output:
Step2: Read/input A,B and C S = ½ + ¼ + … +1/n
Step3: If (A>=B) and (A>=C) then Max=A 1. Start
Step4: If (B>=A) and (B>=C) then Max=B 2. Read n
Step5:If (C>=A) and (C>=B) then Max=C 3. I ← 2 and S ← 0
Step6: Print Max 4. S= S + 1/I
Step7: End 5. I ← I + 2
6. If (I <= n) then go to line 4
else write S in standard output
7. End
Properties of Algorithm
❑ Donald Ervin Knuth has given a list of five properties for algorithm, these
properties are:

➢ Finiteness: An algorithm must always terminate after a finite number of


steps. It means after every step one reach closer to solution of the problem
and after a finite number of steps algorithm reaches to an end point.
➢ Definiteness: Each step of an algorithm must be precisely defined. It is done
by well thought actions to be performed at each step of the algorithm.
➢ Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation.
➢ Output: One always expects output/result (expected value/quantities) in
terms of output from an algorithm. The result may be obtained at different
stages of the algorithm. The output is expected value/quantities always have a
specified relation to the inputs.
➢ Effectiveness: Algorithms to be developed/written using basic operations.
Actually operations should be basic, so that even they can in principle be done
exactly and in a finite amount of time by a person, by using paper and pencil
only.
Flowchart
❑ The flowchart is a diagram which visually presents the flow of data
through processing systems. This means by seeing a flow chart one
can know the operations performed and the sequence of these
operations in a system. Algorithms are nothing but sequence of steps
for solving problems. So a flow chart can be used for representing an
algorithm.

❑ A flowchart is a pictorial representation of algorithm.


Flowchart Symbols
❑ There are 6 basic symbols commonly used in flowcharting of
assembly language Programs: Terminal, Process, input/output,
Decision, Connector and Predefined Process.
Flowchart Symbols
General Rules for Flowcharting
❑ All boxes of the flowchart are connected with Arrows. (Not lines)

❑ Flowchart symbols have an entry point on the top of the symbol with no other
entry points. The exit point for all flowchart symbols is on the bottom except
for the Decision symbol.

❑ The Decision symbol has two exit points; these can be on the sides or the
bottom and one side.

❑ Generally a flowchart will flow from top to bottom. However, an upward flow
can be shown as long as it does not exceed 3 symbols.

❑ Connectors are used to connect breaks in the flowchart. Examples are:


1) From one page to another page.
2) From the bottom of the page to the top of the same page.
3) An upward flow of more then 3 symbols
General Rules for Flowcharting
❑ Subroutines and Interrupt programs have their own and independent
flowcharts.

❑ All flow charts start with a Terminal or Predefined Process (for


interrupt programs or subroutines) symbol.

❑ All flowcharts end with a terminal.


Examples
Examples
Problem5: Algorithm for find the greater number between two numbers.
Examples
Problem7: Flowchart for the problem of printing odd numbers less than a given number. It should
also calculate their sum and count.
Check Answers
1. Write an algorithm and draw a flowchart to check whether the roots
of a quadratic equation are real and imaginary and also find the
roots.

2. Write an algorithm and draw a flowchart to find the solution of 2nd


order system of linear equation using Cramer’s Rule.
Advantages of Flowchart
1) Communication: A Flowchart can be used as a better way of communication
of the logic of a system and steps involve in the solution, to all concerned
particularly to the client of system.
2) Effective analysis: A flowchart of a problem can be used for effective analysis
of the problem.
3) Documentation of Program/System: Program flowcharts are a vital part of a
good program documentation. Program document is used for various
purposes like knowing the components in the program, complexity of the
program etc.
4) Efficient Program Maintenance: Once a program is developed and becomes
operational it needs time to time maintenance. With help of flowchart
maintenance become easier.
5) Coding of the Program: Any design of solution of a problem is finally
converted into computer program. Writing code becomes more easier once
flowchart is developed once.

You might also like