Algorithms
Algorithms
Introduction
Algorithms are the building blocks of any
computer program
They are a set of rules to be followed in order to
solve problems using computers
What is an Algorithm?
Programs = Algorithm + Data
An algorithm is a part of the plan for the
computer program; an algorithm is an effective
procedure for solving a problem in a finite
number of steps.
It is effective, which means that an answer is
found and it has a finite number of steps and
also guaranteed to terminate
Features
Sequence
Sequence means that each step or process in the algorithm
is executed in the specific order
Each process must be in the proper place otherwise the
algorithm will fail
Features
The repetition constructs-repeat and while
Repetition can be implemented using constructs like the repeat loop,
while loop. And if then goto loop
Repeat Loop
The repeat loop is used to iterate or repeat a process or sequence of
processes until some condition becomes true.
General Form:
Repeat
Process1
Process2
.
.
Process N
Until proposition
Example:
Repeat
Fill eater in Kettle
Until Kettle is full
The repeat loop does some processing before testing the state of the proposition
Features
While loop
The while loop finds out whether some condition is true
before repeating a process or a sequence of processes
General Form:
while proposition
begin
Process1
Process2
.
.
Process N
End
Example:
Features
if then goto
The if then goto is also used to repeat a
process or a sequence of processes until the given
proposition is false
General Form:
Process1
Process2
.
.
Process N
If proposition then goto Process1
Example:
Variables
Variables
Variables and data types
The data used in algorithm can be different
types
Simple data types used in algorithm are:
Numeric data, e.g., 12, 11.5, etc.
Alphabetic or character data, e.g., A, z.
Logical data, i.e., propositions with true/false values
Naming of variables:
Choose meaningful names for variables in
algorithms to improve the readability of the
algorithm or program
Step-form
It is a written form that uses normal language
Pseudo-code
pseudo-code is a written forms
In pseudo-code, is in human language but tends towards more
precision by using a limited vocabulary
Flowcharts
Flowchart is graphical representation, they use symbols
and language
5.
6.
Algorithm Examples
1. STAR
2. PRINT ENTER TWO
NUMBERS
3. INPUT A, B
4. C = A + B
5. PRINT C
6. STOP
To Interchange the
numerical values of two
variables
1. START
2. PRINT ENTER THE
VALUE OF A & B
3. INPUT A, B
4. C = A
5. A = B
6. B = C
7. PRINT A, B
8. END
Algorithm Examples
To compare two numbers
1. START
2. PRINT ENTER TWO NUMBERS
3. INPUT A, B
4. IF A > B THEN
PRINT A IS GREATER THEN B
5. IF B > A THEN
PRINT B IS GREATER THEN A
6. IF B = A THEN
PRINT BOTH ARE EQUAL
7. STOP
Pseudo-code
Pseudo-code is an informal way to express the
design of a computer program or an algorithm
Pseudo-code, is a written statement of an
algorithm using a restricted and well-defined
vocabulary
There is no standard for pseudo-code
It is easy to read and use
It is like a young child putting sentences
together without any grammar
Example
dowhile kettle_empty
Add_Water_To_Kettle
End dowhile
Example
Computing SalesTax
1.getprice of item
2.getsales tax rate
3.sales tax=price of item times sales tax
rate
4final prince=price of item plus sales tax
5.displayfinal price
6.halt
Flowcharts
A flowchart provides appropriate steps to be
followed in order to arrive at the solution to a
problem
It is a program design tool which is used before
writing the actual program
A flowchart comprises a set of various standard
shaped boxes that are interconnected by flow
lines
Flow lines have arrows to indicate the direction of
flow of control between the boxes
The activity to be performed is written within the
boxes in English
There are connector symbols that are used to
indicate that the flow of control continues elsewhere,
for example, the next page
Name
Description
(Subroutine)
(Magnetic Tape)
Advantages of using
flowcharts
Communication: Flowcharts are a better way of
communicating the logic of a system to all concerned
Effective analysis: With the help of flowcharts, problems can
be analyzed more effectively
Proper documentation: Program flowcharts serve as a good
program documentation needed for various purposes
Efficient coding: Flowcharts act as a guide or blueprint during
the system analysis and program development phase
Proper debugging: Flowcharts helps in the debugging
process
Efficient program maintenance: The maintenance of an
operating program becomes easy with the help of a flowchart
Limitations of using
flowcharts
Complex logic: Sometimes, the
program logic is quite complicated. In
such a case, a flowchart becomes
complex and clumsy
Alterations and modification: If
alterations are required, the flowchart
may need to be redrawn completely
FLOWCHART EXAMPLES
To print the climatic condition
FLOWCHART EXAMPLES
To find the sum of first
50 natural numbers
FLOWCHART EXAMPLES
THE END