Problem Solving and Programming 2019
Algorithms and Flowcharts
Algorithm
It is a step by step procedure to solve a problem
A sequential solution of any problem that is written in human language.
Using the algorithm the programmer can easily writes the actual program.
The main use of algorithm is to help us to translate English into C.
Algorithm consists of combination of statements of English and C, but not exactly C. It can then be
converted to C program.
It is an outline or basic structure or logic of the problem.
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.
General Way of Writing the Algorithm
Name of the algorithm must be specified.
Beginning of algorithm must be specified as Start
Input and output description may be included
Step number has to be included for identification
Each step may have explanatory note provided with in square bracket followed by operation.
Completion of algorithm must be specified as end or Stop.
General Steps for Writing the Algorithm
Algorithm: Name of the Algorithm
Input: Input Values / Values to be read
Output: Output corresponding to the input / Result
Step 1: Start
Step 2: Read
Step 3: Logic / Formula
Step 4: Display / Result / Output
Step 5: Stop
1 Mr. Rajesh S M, Assistant Professor
Problem Solving and Programming 2019
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.
2 Mr. Rajesh S M, Assistant Professor
Problem Solving and Programming 2019
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
Shape Name Meaning
Start and Stop Used to denote start and stop of flowchart
Parallelogram used to read input and display
Input and Output
output
Rectangle used to include instructions or
Processing
processing statements
To include decision making statements rhombus
Decision/ Condition
is used
Connector Join different flow chart or flow lines
Repetition/ Loop Hexagon is used for looping constructs
To include functions in a program, the name of
Functions
function should be enclosed within the figure
Control Flow Lines Arrows denoting flow of control in flow chart
For including the comments, definition or
Comment box
explanation
3 Mr. Rajesh S M, Assistant Professor
Problem Solving and Programming 2019
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
4 Mr. Rajesh S M, Assistant Professor
Problem Solving and Programming 2019
Advantages of the Flow Chart
The flowchart is a good way of conveying the logic of the system.
Facilitates the analysis of the problem.
Provides a proper documentation.
Easy identification of the errors and bugs.
It directs the program development.
Maintenance of the program becomes easy.
Disadvantages of the Flow Chart
The complex logic could result in the complex flow chart.
A flowchart must be recreated to employ modification and alterations.
Differences between Algorithm and Flowchart:
BASIS FOR
ALGORITHM FLOW CHART
COMPARISON
Basic Includes sequence of steps which An information diagram made up
depicts the procedure of the of different shapes shows the
solution. data flow.
Comprehensibility Hard to understand Easily interpreted
Uses Text Symbols
Implements No rules are employed. Predefined rules are
implemented.
Debugging Easier Difficult
Ease of construction Perplexing Simple
Key Differences between Algorithm and Flowchart
[1]. An algorithm involves a combination of sequential steps to interpret the logic of the solution. In
contrast, a flowchart is the pictorial illustration of the algorithm.
[2]. A flow chart is more understandable as compared to the algorithm.
[3]. The algorithm is written in a language that can be perceived by humans. On the other hand, the
flowchart is made up using different shapes and symbols.
[4]. There are no stringent rules are implemented in the algorithms while the flowchart is abode by
predefined rules.
[5]. Errors and bugs are easily detected in the algorithm as compared to the flow charts.
[6]. Flow charts are simple to create. On the contrary, the construction of the algorithm is complex.
5 Mr. Rajesh S M, Assistant Professor
Problem Solving and Programming 2019
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.
6 Mr. Rajesh S M, Assistant Professor
Problem Solving and Programming 2019
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
Ex 1: Addition of two numbers Ex 2: Area of Circle
Get the numbers[a,b] Get the radius[r]
Compute addition [Sum= a + b] Compute area[Area = 3.141*r*r]
Print the results [Sum] Print the results [Area]
Advantage:
Easy to write and understand
It is relatively easy to convert English description solution of small programs to C program.
Disadvantage:
It is very difficult to translate the solution of lengthy and complex problem in English to C
7 Mr. Rajesh S M, Assistant Professor