3_algorithm
3_algorithm
Code: 18ECSP101
Algorithms – Introduction to
Problem Solving
• Definition
• Characteristics of an Algorithm
• Notations in an Algorithm
• Different types of Algorithms
• Examples
• Exercise problems
Input
Definiteness
Effectiveness
Finiteness
Output
Eachprocedure with
instruction must be simple finite
and be carried out in a finite
amount of time. Means each operational step can at least in
Effectiveness number
principle be carried outof
minimum amount of time.
by awell defined
person using a paper and pencil in a
Start – (Beginning)
Termination
Step 5: Stop
K. L. E. Technological University, Hubli-
12/21/21 11
31.
Hands on
2) Conditional
• Set of steps in an algorithm are carried out based on a certain
condition(whether true/false).
• In programming languages, a conditional pattern is
implemented using decision making statements.
3) Iterative/Repetitive
• A task (one or more steps) is repeated n number of times.
• The number of times it repeats is tracked by a condition.
• In programming languages, an iterative pattern is
implemented using looping constructs.
K. L. E. Technological University, Hubli-
12/21/21 15
31.
Demonstration Example 5:
Write an algorithm to decide/ find whether a given
integer number is positive or negative.
Step 1: Start [Beginning of the Algorithm]
Alice uses online taxi booking service to go to the office from his home
and uses online mobile application for booking.
The online taxi booking costs Rs.X for the first N kilometres
and Rs.Y for every kilometre afterward. Alice is going to the office from
his home. Your task is to find the cost that is required to pay by Alice.
The distance from Alice’s home to the office is D kilometres.
Sohan
x(3,3)
X
Mohan and Sohan are at two different places. Mohan decided to meet Sohan.
Calculate the distance he needs to travel to reach Sohan. If he is traveling at a speed of
2km/h, what is the time needed for Mohan to reach Sohan? If Mohan wants to reach
Sohan using a Blue color Suzuki Jimny car calculate the time needed to reach.
20
REVISITING CLASS ACTIVITY-1 TO WRITE
ALGORITHM
Algorithm: To Compute Distance
Step 1: Start
Step 2: Read x1,x2,y1,y2 [ Read coordinates]
Step 3: s←(x1-x2)2+(y1-y2)2
Step 4: Distance ←√s [compute distance]
Step 5 : Display Distance
Step 6:Stop