ALGORITHMS AND
FLOWCHARTSALGORITHMS AND FLOWCHARTS
BA typical programming task can be divided into
two phases:
Problem solving phase
@ produce an ordered sequence of steps that
describe solution of problem
e this sequence of steps is called an algorithm
mlmplementation phase
e implement the program in some programming
languageSteps in Problem Solving
mFirst produce a general algorithm (one can use
pseudocode)
mRefine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
mPseudocodeis an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.ALGORITHM
mAlgorithm can be defined as: “A sequence of
activities to be processed for getting desired
output from a given input.”
BA 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”.
m There may be more than one way to solve a
problem, so there may be more than one
algorithm for a problem.Pseudocode & Algorithm
m Example 1: Write an algorithm to
determine a student’s final grade and
indicate whether it is passing or failing.
The final grade is calculated as the
average of four marks.Pseudocode & Algorithm
Pseudocode:
m@input a set of 4 marks
mCalculate their average by summing and
dividing by 4
gif average is below 50
Print “FAIL”
else
Print “PASS”Pseudocode & Algorithm
Detailed Algorithm
m Step 1: Input M1,M2,M3,M4
Step 2: GRADE = (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endifEx:2-(Find the area of a Circle of radius “r”)
m Inputs to the algorithm:
m Radius r of the Circle.
mExpected output:
mArea of the Circle
Algorithm:
mStep1: Read\input the Radius r of the Circle
mStep2: Area Pl*r*r // calculation of area
mStep3: Print Area
mStep4: EndEx:3-(Write an algorithm to read two numbers and find
their sum)
minputs to the algorithm:
mFirst num1.
mSecond num2.
mExpected output:
mSum of the two numbers.
BAlgorithm:
mStept: Start
mStep2: Read \input the first num1.
mStep3: Read\input the second num2.
mStep4:Sum numi+num2 // calculation of sum
mStep5: Print SumEx:4-(Convert temperature Fahrenheit to Celsius)
m Inputs to the algorithm:
m@ Temperature in Fahrenheit
mExpected output:
m@ Temperature in Celsius
Algorithm:
mStept: Start
mStep 2: Read Temperature in Fahrenheit F
mStep 3: C 5/9*(F32)
mStep 4: Print Temperature in Celsius: C
mStep5: EndType of Algorithms
@ The algorithm and flowchart, classification to the three types of control
structures. They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
lm These three control structures are sufficient for all purposes.
m 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.
@ 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.
me Thie eth iat ira ie alen braun ne tee eslactian etre iathiraProblem1: write algorithm to find the greater
number between two numbers
@ Step: Start
m@ Step2: Read/input A and B
m@ Steps: If A greater than B then C=A
m@ Step4: if B greater than A then C=B
m@ Stepd: Print C
m Stepé: EndProperties of algorithm
m1) Finiteness: An algorithm must always
terminate after a finite number of steps.
mit 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.
2) Definiteness: Each step of an algorithm must
be precisely defined.
git is done by well thought actions to be
performed at each step of the algorithm.
BA\Iso the actions are defined unambiguously for
each activity in the algorithm.3) Input: Any operation you perform need some
beginning value/quantities associated with different
activities in the operation.
So the value/quantities are given to the algorithm
before it begins.
m4) Output: One always expects output/result (expected
value/quantities) in terms of output from an algorithm.
m The result may be obtained at different stages of the
algorithm.
ulf some result is from the intermediate stage of the
operation then it is known as intermediate result and
result obtained at the end of algorithm is known as end
result.
m The output is expected value/quantities always have am_ 5) 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.The Flowchart
mg A graphical representation of the sequence of
operations in an information system or program.
m The flowchart is a diagram which visually presents the
flow of data through processing systems.
m This means by seeing a flow chart one can know the
operations performed and the sequence of these
operations in a system.
g Algorithms are nothing but sequence of steps for
solving problems,So a flow chart can be used for
representing an algorithm.
g A flowchart, will describe the operations (and in what
sequence) are required to solve a given problem.Flowchart SymbolsI
General Rules for flowcharting
1. All boxes of the flowchart are connected with Arrows. (Not lines)
m@ 2. 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.
@ 3. The Decision symbol has two exit points; these can be on the
sides or the bottom and one side.
@ 4. 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.
m 5. Connectors are used to connect breaks in the flowchart. Examples
are:
m+ From one page to another page.
m+ From the bottom of the page to the top of the same page.. Le
a
@ 6. Subroutines and Interrupt programs have their own and
independent flowcharts.
@ 7. All flow charts start with a Terminal or Predefined Process (for
interrupt programs or subroutines) symbol.
@ 8. All flowcharts end with a terminal or a contentious loop,
Flowcharting uses symbols that have been in use for a number of
years to represent the type of operations and/or processes being
performed. The standardized format provides a common method for
people to visualizeExample
Step 1: Input M1,M2,M3,M4
Step 2: GRADE = (M1+M2+M3+M4)/4
aw Step 3: if (GRADE <50) then
Print “FAIL”
GRADE (M1+M2+M3+M4)/4 else
Print “PASS”
is endif
a
pave ) PRINT )
“FAIL”Example 2
mg Write an algorithm and draw a flowchart
to convert the length in feet to
centimeter.
Pseudocode:
Input the length in feet (Lft)
@ Calculate the length in cm (Lem) by
multiplying LFT with 30
@ Print length in cm (LCM)Example 2
Flowchart
Algorithm =<
m@ Step 1: Input Lft oe
m Step 2: Lem Lftx 30
m@ Step 3: Print Lcm Erie
:Example 3
Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate
its area.
Pseudocode
m input the width (W) and Length (L) of a rectangle
Calculate the area (A) by multiplying L with W
m@Print AExample 3
Algorithm START
m@ Step 1: Input WL
m Step2;A LxW a:
m Step 3: Print A an
Print
AAdvantages of using Flowcharts
m 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.
m5) Coding of the Program: Any design of solution of a problem is
finally converted into computer program. Writing code referring the
flowchart of the solution become easy.Assignment 1
Flowchart for an algorithm to find out the area of a circle of radius r.
e Flowchart for an algorithm to convert temperature Fahrenheit to
Celsius.
e Flowchart for an algorithm which gets two numbers and prints sum
of their value.
e Flowchart for an Algorithm for find the greater number between two.
numbers.