Algorithm: Algorithm Is A Step-By-Step Procedure or Sequence of Instruction To Solve A Problem
Algorithm: Algorithm Is A Step-By-Step Procedure or Sequence of Instruction To Solve A Problem
Algorithm
Definition:
to solve a problem.
Steps:
1. Start
2. Input
3. Process
4. Output
5. Stop
Step:1 Start
Step:4 Print c
Step:5 Stop
1
Characteristics of algorithms
Representation of algorithm
Steps:
Start
READ
Process
WRITE
Stop
3
Example: Pseudocode to student‟s mark details
Start
READ name, rollno, m1, m2, m3
Total = m1+m2+m3
Average = Total/3
Stop
Advantage
Disadvantage
It is not visual.
4
Flowchart
Definition:
Steps:
START
READ
PROCESS
STOP
5
Example: Flowchart to addition of two numbers
START
READ A,B
CALCULATE
C=A+B
PRINT C
STOP
Aim of flowchart
Program preparation can be simplified using the flowchart.
Flowcharts are easier to understand at glance, than the narrative description of
algorithm.
Flowcharts are easy to analyze and compare various methods.
6
Flowchart symbols
Flowlines
2 Terminal symbol
(Start / Stop)
3 Input/output symbol
4 Process symbol
5 Decision symbol
6 Connectors
7
Rules (Guidelines) for drawing a flowchart
Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.
< 0 > 0
= 0
8
Advantage of flowchart
Hard to modify
Complex logic
Alternations and modifications
Reproduction
9
Building blocks of Algorithm, Pseudocode and Flowcharts
(or)
Program control structures of Algorithm, Pseudocode and Flowcharts
The sequence logic is used for performing instruction one after another.
Process n Process n
10
Example:
Step:4 Print c
Example:
Start
Calculate c=a+b
WRITE c
Stop
11
Example:
START
READ A,B
CALCULATE
C=A+B
PRINT C
STOP
12
ii. Selection control structure (or) Decision structure
The selection control structure is the presentation of a condition and the choice
between two actions; the choice depends on whether the condition is true or false
It is usually depicted as an IF…….THEN or IF…………….THEN….ELSE.
(a)Algorithm (b)Pseudocode
Start Start
Read READ
IF condition THEN IF condition THEN
. Print process 1 . WRITE process 1
ELSE ELSE
Print process 2 WRITE Process 2
. .
END IF END IF
Stop Stop
13
(c)Flowchart
START
READ
IF
No Yes
Condition
PRINT PRINT
Process-2 Process-1
STOP
14
Example:
Step:3 IF (N % 2 = = 0) THEN
ELSE
Example:
Start
IF (N % 2 = = 0) THEN
ELSE
Step:4 Stop
15
Example:
START
READ
N
IF
No Yes
(N%2= 0)
PRINT PRINT
Process-2 Process-1
STOP
16
iii. Iteration control structure (Looping or Repetition)
The top tested loops are leading decision is evaluated always at the first statement
of the loop.
Pseudocode
Algorithm
Start
Start
.
.
.
.
WHILE condition
WHILE condition
.
.
.
.
.
.
Body of the loop
Body of the loop
.
.
.
.
.
.
ENDWHILE
ENDWHILE
Stop
Stop
17
Flowchart
While No
(condition)
Yes
18
(*) Bottom tested loop
This loop is a trailing decision loop, means that the decision is always at last
statement of the loop.
The body of the loop will be executed once before control even gets to the loop
test.
Algorithm Pseudocode
Start Start
. .
. .
Body of the loop Body of the loop
. .
. .
WHILE condition WHILE condition
Stop Stop
Flowchart
Yes While
(condition)
No
19
Example:
Example:
Start
READ n, sum=0
WHILE (n > 0)
r=n%10
sum =sum+r
r=n //10
END WHILE
WRITE sum
Stop
20
Example:
Start
Read n ,
sum =0
WHILE No
(n >0)
Yes
r=n%10
Yes
sum =sum+r
r=n //10
Print sum
Stop
21
Exercise
1. Write the Algorithm, Pseudocode , and Draw the Flowchart to Addition of two
numbers
Algorithm:
Step 1: Start
Step 3: Calculate c =a + b
Step 4: Print c
Step 5: Stop.
Pseudocode: Flowchart
Start
c= a+b
WRITE c
Stop
22
2. Write the Algorithm, Pseudocode , and Draw the Flowchart to find the sum and
product of two numbers
Algorithm:
Step 1: Start
Step 3: Calculate c =a + b ,
d =a * b
Step 4: Print c, d
Step 5: Stop.
Pseudocode: Flowchart
Start
c= a+b
d=a*b
WRITE c, d
Stop
23
4. Write the Algorithm, Pseudocode , and Draw the Flowchart to convert temperature in
Fahrenheit to Centigrade
Algorithm:
Step 1: Start
Step 4: Print c
Step 5: Stop.
Pseudocode: Flowchart
Start
READ the value of f.
c =5 / 9 (f - 32)
WRITE c
Stop
24
3. Write the Algorithm, Pseudocode , and Draw the Flowchart to find the biggest of
given two numbers
Algorithm:
Step 1: Start
Step 3: IF (a>b)
Step 4: Stop.
Pseudocode: Flowchart:
Start
ENDIF
Stop
25
4. Write the Algorithm, Pseudocode , and Draw the Flowchart to find roots of quadratic
equation
Algorithm:
begin
R1 ← (-b + d) / (2*a)
R2 ← (-b - d) / (2*a)
Print R1, R2
end
else if(d= = 0)
begin
R1 ← -b / (2*a)
R2 ← -b / (2*a)
Print R1
Print R2
end
else
26
Pseudocode:
Step 1: Start
begin
R1 ← (-b + d) / (2*a)
R2 ← (-b - d) / (2*a)
WRITE R1, R2
end
else if(d= = 0)
begin
R1 ← -b / (2*a)
R2 ← -b / (2*a)
WRITE R1
WRITE R2
end
else
Step 5: Stop
27
Flowchart:
Start
Read a,b,c
d=pow(b * b) - (4 * a * c)
False
if( d > 0 )
False True
if(d= = 0)
R1 ← (-b + d) / (2*a)
R1 ← -b / (2*a)
R2 ← -b / (2*a)
Print R1, R2
Print
Imaginary
Stop
28
ILLUSTRATIVE PROBLEMS
Tower of Hanoi
These rings are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one.
Rules
The mission is to move all the disks to some another tower without violating the
sequence of arrangement.
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
29
Algorithm
(or)
Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps.
This presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.
30
Step-1 Step-2
Step-3 Step-4
Step-5 Step-6
Step-7
31
A recursive algorithm for Tower of Hanoi can be driven as follows
START
Procedure Hanoi(disk, source, destination, auxiliary)
IF disk == 1, THEN
move disk from source to destination
ELSE
Hanoi(disk - 1, source, auxiliary, destination) // Step 1
move disk from source to destination // Step 2
Hanoi(disk - 1, auxiliary, destination, source) // Step 3
END IF
END Procedure
STOP
32
Find Minimum in a list
Problem description:
Algorithm:
33
Insert a card in a list of sorted cards
Problem description:
In general the sorting is in ascending order and sorted cards would be either
playing card with numbers or flash cards with alphabets.
In that case, a person would check the new card‟s number or alphabets with set
of card sorted .
All the least cards will be picked out and when a card with greater number /
alphabet is encountered; the new card is placed above the greater card and the
picked out card will be replaced above the new card.
In case of playing cards which have Jack, Queen and King along with one to ten
numbers; the type of card, numbers and alphabets must be considered.
The below algorithm considers the playing cards
Algorithm:
Step 6.1 : If Insert _ Card is a number card compare the numbers in the
list and place in the right order.
Step 6.2 : If Insert _ Card is a role card compare JACK, QUEEN and
Problem description:
The player B can start guessing from the starting number of range till he reaches
the correct number.
This approaches is called linear search, because the guesses all the numbers in
sequence.
But it takes several guesses and hence is not efficient.
If player A can give the player B clue on whether the number he as guessed is
lesser or greater than what he thought of it would be reduced range of numbers
into half.
Keep going, always eliminating half of the reaming numbers. This approach is
called Binary search.
For a range of 1to 3, player B can find a number in at most 5 guesses with this
technique.
Algorithm:
Step 5: Repeat Step -3 and Step- 4 until G and T are the same TCS codevita
problems
35
Steps in Problem Solving
Requirement Analysis
Design
Coding
Software Testing
Documentation
Maintenance
36
2. Waterfall Method
The first System Development Life Cycle (SDLC) method and it describes the
various phase involved in development.
Feasibility
Analysis
Design
Implementation
Testing
Maintenance
Feasibility:
The feasibility study is used to determine if the project should get the go-ahead.
Budget estimates for the future stages of development.
Implementation:
The designs are translated into code. Computer programs are written using a
conventional programming language generator.
Programming tools like compilers, interpreters, Debuggers are used to generate
the code.
37
Testing:
These subjects to separate and detailed test and the system are then tested as a
whole.
The separate modules are brought together and tested as a complete system.
Maintenance:
If you have been asked to solve programs then you cannot write the program
unless you know the logic of the problem.
So before writing any program you should know the concept of the program and
write the logical steps for the given program in correct sequence.
There are five ways to represent the logical steps for finding the solution to given
problem.
1. Algorithms
2. Flowchart
3. Pseudocode
4. Decision table
5. Program
38