Algorithms & Folwcharts
Algorithms & Folwcharts
Characteristics of Algorithm
Each and every instruction must be precise and unambiguous.
Each instruction execution time should be finite.
There should be a termination condition.
It has to accept 0 or more inputs and produce compulsory an output.
It can accept any type of input and produce a corresponding output.
Ex 1: Write an algorithm to add two numbers Ex 2: Write an algorithm to find simple interest
Algorithm: Addition of two numbers Algorithm: To find Simple Interest
Input: Two number a and b Input: p, t, r
Output: Sum of two numbers Output: si
Step 1: Start Step 1: Start
Step 2: [input two number] Step 2: [input a principle p, time t & rate r]
Read a and b Read p, t, r
Step 3: [compute its addition] Step 3: [compute simple interest]
Sum = a + b SI (p*t*r)/100
Step 4: print Sum Step 4: Display si
Step 5: Stop Step 5: Stop
Advantages of the Algorithm
It identifies the solution process, decision points and variables required to solve the problem.
It helps in dividing a huge problem into smaller manageable steps of the solution.
The analysis and specification of the process lead to the efficiency.
Separation of the steps divides labour and development expertise.
Disadvantages of the Algorithm
At a specific point, the algorithm terminates.
Inability to solve problems that generate non-computational results.
Consumes a lot of time.
Exercise:
Write an Algorithm for the following:
1. Addition, Subtraction, Multiplication, Subtraction of two numbers.
2. Swapping of two numbers:
a. Using temporary variable.
b. Without using temporary variable.
3. Area of Triangle, Rectangle, Square, Circle, Triangle when three sides are given.
4. Circumference of Circle.
5. Perimeter of the Rectangle.
6. Greatest of two numbers.
7. Greatest of three numbers.
8. Number is even or odd.
9. Number is Positive or Negative.
10. To find sum and average of 3 numbers.
Flow charts
A flowchart is a pictorial or graphical representation of an algorithm or a program.
It consist sequences of instructions that are carried out in an algorithm.
The shapes represents operations.
Arrows represent the sequence or flow in which these operations are carried out.
It is mainly used to help the programmer to understand the logic of the program.
Notations used in flowchart
Ex 1. Algorithm and Flowchart to Input the dimensions of a rectangle and print area and perimeter
Step 1: Start
Step 2: Read length, breadth
Step 3: area length * breadth
Step 4: Compute ‘perimeter’
Step 5: Perimeter2*(length+breadth)
Step 6: Display area, perimeter
Step 7: stop
Ex 2. Algorithm and Flowchart to Input 3 numbers and print the biggest number.
Step 1: start
Step 2: input a,b,c
Step 3: if a>b
Bigaba
else
bigabb
end if
Step 4: if c>bigab
display c
else
display bigab
end if
Step 5: stop
BASIS FOR
ALGORITHM FLOW CHART
COMPARISON
Exercise:
Write an Flowchart for the following:
1. Addition, Multiplication, Subtraction of two numbers.
2. Swapping of two numbers:
a. Using temporary variable.
b. Without using temporary variable.
3. Area of Triangle, Rectangle, Square, Circle.
4. Calculate Simple Interest.
5. Area of Triangle when three sides are given.
6. Circumference of Circle.
7. Perimeter of the Rectangle.
8. Greatest of two numbers.
9. Number is Even or Odd.
10. Number is Positive or Negative.
11. To find sum and average of 3 numbers.
12. To check the character is Alphabet or Not.
Note:
1. Need for writing algorithm and flow charts
Effective communication
Effective analysis
Effective coding
Effective debugging
Easy maintenance
Proper documentation
2. Pseudocode: A solution to Problem
It is the first step in writing a program.
It is written using a mixture of English and C language.
It is a series of steps to solve a given problem
It acts as a problem solving tool