Algorithm, Flowchart and Pseudo Code
Algorithm, Flowchart and Pseudo Code
Algorithm and1
2 Programming Development
18
Algorithm and Programming Development 19
2.1.PROBLEM DEFINITION
This is the first step in computer problem solving. The problem solver should
thoroughly terms of the requirements, i.e., what are the
in understand the problem
The programmer should extract input and output.operations to beperformed.
from the problem statement, a set of
that can be carried out. If well-defined and precise tasks
the problem is not
results. properly interpreted, we cannot obtain the desired
2.2. ANALYSIS
The given problem must be analyzed before it is solved. This determines the
and relationship. We should specify the data items, their types
and check the
operations (Arithmetic and logical) to be
performed on them
suitability
in terms of memory execution time.
2.3. ALGORITHM
This is a problem solving technique. It can be defined as a step-by-step
problem, It consists of English.like statements. Each statement must procedure to solve a particular
be precise and well-defined to
perfom a specific operation. When these statements are carried out for a
they will produce the required results. given set of conditions,
The word
Algorithm is named after the famous ninth century Arabic author and mathematician,
Abu Jafar Mohammed lbn Musa Al Khowarizmi.
The last two terms of the name took different
pronunciations over the period such as Alkhowarism, Algorism and it became finally Algorithm.
2.3.1.Characteristics of Algorithm
Each and every algorithm is characterized the
by following five important characteristics.
1. Input
It may accept zero or more inputs.
2. Output: It should produce
at least one output (result).
3. Definiteness Each instruction must
be any ambiguity.
be clear, well-defined and precise. There should not
4. Finiteness: It should be a sequence of finite
time. It should not enter an infinite
instructions, i.e., it should end after a fixed
loop.
5. Effectiveness: This means that operations must be simple and carried out in a finite
time at
one or morelevels of complexity. It should be effective whenever traced
results. manually for the
Computer programming languages are required to check the definiteness and effectiveness of
each instruction of the
algorithm.
Algorithmic Notationss
While writing algorithms the following notations are considered.
1. Name of the Algorithm: It specifies
the problem.to
be solved.
2. Step Number: Identification tag
of an instruction and it is an unsigned positive integer
3. Explanatory Comment:
It follows the step number and describes the operation. It should
be written within a pair of square brackets.
4. Termination: It specifies the end of the algorithm. It is generally a STOP statement and the
last instruction in the algorithm.
20 Programming for Problem Solving
EXAMPLEE1
circle.
Algorithm to compute the area of
Algorithm : Area of a circle
Step 1 Read radius
Step2 [Compute the area] x radius
Area 3.142 x radius
Step 3 [Print the area]
Print Area of a circle=", Area
Step 4 [End ofalgorithm]
Stop
EXAMPLE2 subtraction, multiplication and
such as addition,
the basic arithmetic operations
Algorithm to perform
division.
Algorithm:Simple Interest
Step 1 Read the value of P, R, T]
Read P,R, T
Step 2 [Compute the simple interest]
Sl (Px R* T)/100
Step 3 [Compute the compound interest]
Cl Px[1 +(R/100)]-P
Step 4 [Print the simple interest and compound interest]
Print 'Simple interest =, SI
Print'Compound Interest =", CI
Algorithm and Programming Development 21
Step 5 [End of algorithm]
Stop
EXAMPLE 4
Algorithm to find the largest of three numbers.
Algorithm: Largest of three numbers
Step 1 [Read the value of A, B and C]
Read A, B, C
Step 2 [Compare A and B]
If (A>B) go to Step 4
Step 3 [Otherwise compare B with C]
If (B>C) then
Print B is the largest
Else
Print 'C is the largest
Goto Step5
Step 4 [Compare A and C for largest]
If (A>C) then
Print 'A is the largest
Else
Print 'C is the largest
Step 5 [End of algorithm)
Stop
EXAMPLE 5
Algorithm to compute the sum of first N natural numbers and their mean value. The
are 1,2, 3, .. N. It adds natural numbers
of them.
one
number at a time until N, the maximum limit. Then
computes the Mean
Algorithm: Sum and Mean of Natural numbers
Step 1 [Read the maximum value of N]
Read N
Step 2 [Set sum equal to 0]
sum 0
Step 3: [Compute the sum of all N numbers]
For Num = 1 to N in step of 1 do
begin
Sum Sum + Num
End
Step 4 [Compute mean value of N natural numbers
Mean Sum/N
Step 5 (Print Sum and Mean]
Print 'Sum of N natural numbers =, Sum
Print Mean of N natural number ="', Mean
Step 6 [End of algorithm]
Stop
Programming for Problem Solving
22
2.4.FLOwCHART
This is a chart showing a flow of logic involved in solving a problem. This is defined for an algorithm
The flowchart can be defined as a diagramatic representation of an algorithm. It is referred to as th
e
blue print of an algorithm. It is also defined as a visual or graphical representation of an algorithm
The flowchart is an easy way to understand and analyze the problem. It is a useful aid f
programmers and system analysts. Itis machine independent. It can be used for any type of problema
Flowcharts are classified into fwo types. They are,
1. Program flowcharts
2. System flowcharts.
Flowcharts make use of geometrical figures, to specify a
particular operation. Following table
shows different geometrical figures used in a program flowchart with their functions. While drawing a
flowchart, operations must be written inside the geometric figures.
PROGRAM FLOWCHARTS
Geometrical Figure Name Function
Oval Start and Stop
Rectangle Processing
Arrows Connections
O Circle Continuation
Hexagon
Repetition/Looping
Algorithm and Programming Development 23
Program Flowcharts
EXAMPLE1
Dray a lowchart to find the area of a triangle when its three sides are given.
Start
Read a,b, c
S= (a+b+c}/2
Print Areaa
Stop
EXAMPLE 2
Draw a flowehart to find the largest of three numbers.
Start
Read a, b, c
Y N
a
N Is
N
?
Stop
24 Programming for Problem Solving
EXAMPLE3
Draw a flowchart to compute the factorial
of a given numt
Start
Read n
Fact 1
Count 1 |
Count count+ 1
Count= count+ 1
true is
cOunt=n
false
Print Fact
Stop
Algorithm and Programming Development 25
EXAMPLE 4
Draw a flowchart to find the roots of a quadratic equation ax2 + bx + c = 0
Start
Read a, b, c
false true
d b-4ac ro
root=-C/b
= 0 0 / Print root
S
d
Stop
Stop
2.5. PSEUDOCODE
lIt consists
A pseudocode is neither an algorithm nor a program. It is an abstract form of a program.
It
perform the specific operations. Itis defined for an algorithm.
of English like statements, which
the program is represented in terms of
does not use any graphical representation. In a pseudocode,
words and phrases, but the syntax of program
a is not strictly followed.
Advantages
Easy to read
>Easy to understand
Easy to modify a flowchart.
The program can be developed easily froma pseudo code to
EXAMPLE 1
arithmetic operations.
Write pseudocode to perform the basic
a
Read n1, n2
26|Programming for Problem Solving
Sum n1 n2
Diff n1-n2
Mut n1 n2
Quot = n1/n2
2.6.CODING
The complete structure of a problem to be solved by a computer is called a program. The computer
does not process an algorithm or a flowchart, but executes
the program. A program is a set of
instructions to solve a particular problem by the and the actual
computer of
process writing program
a
is called coding. Programs are written using programming languages and are fed to the computer.
2.8. DEBUGGING
The process of detecting and correcting errors
(mistakes) in the program is known as debugging.
There is a program called debugger that takes object
program as input and executes it and helps in
eliminating the mistakes that occur in the source program. The tem 'debug' was coined after detecting
a real bug in MARK, an electro-mechanical
computer, 1945, by Admiral Gracehopper. She removed
in
the bug from the machine and wrote it in a record book The MARK-I
was debugged today.
Generally, programmers commit three types of errors. They are,
1. Syntax errors
2. Logical errors
3. Run-time erors
1. Syntax Errors These type of errors are the result of violation of grammar (rules of
programming language). On encountering these errors a computer displays error message
specifying the line. It is easy to debug these errors. For example, the syntax of assignments
statement in C is
Variable expression;
f the above statement is typed without the semicolon at the end, there will be an error
because
of a missing semicolon
2. Logical Errors: Logical errors occur during coding process. When the program-
mer codes his problem, he must take care of correct operations to be periormed.
Algorithm and Programming Development 27
The program will be executed but produce some unwanted results. It is very difficult
to debug such errors, because the computer does not display them. We can eliminate such
errors by tracing it and running for sample data. It can be checked by inserting print statements
in an appropriate location in the program or with the help of debugger.
3. Run-time Errors These errors occur when we attempt to run ambiguous instructions. For
example, an infinite loop in program sequence, which causes no output. They als0 occur due
to device errors, improper sequencing of constructs, errors in system software (translator or
operating system), Keypunch errors, incorrect data input, etc. The computer will print the
eror message. Some of the runtime errors are:
Divide by zero.
>Null pointer assignment
Data overflow
2.9. TESTING
The process of executing the program to test the correctness of the output (result) of the problem is
caled testing. Executing with iferent sets ofdat teststhe program. Logical errors are the outcome
of this process.
2.10. DOCUMENTATION
While writing programs, it is a good programming practice to.make a brief explanatory note on the
program or program segments. This explanatory note is called a comment. It explains how the
program works and how to interact with it. Thus, it helps other programmers to understand the
program.
There are two types of documentation. They are,
1. Internal Documentation
2. External Documentation
1. Internal Documentation: This documentation is a comment
statement within a program. It
describes the function of the program or program segments. These statements are not
translated to machine language. Translators simply discard these statements.
2. External Documentation:This documentation
is an executable statement in a program. It
may be message to the user to respond to the program requirement. This is
accomplished
using output statements. It makes a program more attractive and interactive. Some simple
examples are given below.
print "Input numbers one by one"
print "Input order of the matrix"
print "Do you want to continue?"