Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

3_algorithm

Uploaded by

01fe21bec052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3_algorithm

Uploaded by

01fe21bec052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Course Title: Programming in C.

Code: 18ECSP101

Algorithms – Introduction to
Problem Solving

K. L. E. Technological University, Hubli-


12/21/21 1
31.
ALGORITHM DEVELOPMENT
 Activity 1 : What Steps to complete the
are the steps taken Step task
to arrive to the No
college? 1 Get up at 6.30am
2 Get ready and leave home
at 7.15am
3 Arrive at bus stop
4 Wait for bus
5 Catch a bus
6 Arrive to college

School of Computer Science and Engineering


Activity-2
Work out to choose the six correct steps from the nine total options to plant
the seed and also arrange them in sequential order.

School of Computer Science and Engineering


ALGORITHMS
CONTENTS:

• Definition
• Characteristics of an Algorithm
• Notations in an Algorithm
• Different types of Algorithms
• Examples
• Exercise problems

K. L. E. Technological University, Hubli-


12/21/21 4
31.
Definition: ALGORITHM
An effective procedure for solving a problem in a finite
number of steps.

An algorithm is a finite set of well defined instructions

Step by step procedure to solve a given problem.

K. L. E. Technological University, Hubli-


12/21/21 5
31.
Characteristics of an Algorithm

 Input
 Definiteness
 Effectiveness
 Finiteness
 Output

K. L. E. Technological University, Hubli-


12/21/21 6
31.
Redefine Algorithm:
It may accept zero or more inputs. These data elements
Input are processed in the subsequent steps or instructions in the
Algorithm is defined as
algorithm
Each instruction must be clear, well-defined and precise. There
Definiteness an
should not effective step
be any ambiguity. Means eachby step
and every
must specify clearly that, what should be done
instruction

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

steps for problem solving.


Algorithm should have finite sequence of instructions. That is, it
Finiteness should end after a fixed time. It should not enter into an infinite
loop (It should come to an end).
Output : It should produce at least one output.

K. L. E. Technological University, Hubli-


12/21/21 7
31.
Notations in an Algorithm

 Name of the algorithm: Specifies the problem to be solved.

 Start – (Beginning)

 Step number - Identification tag of an instruction or statement


and it is an unsigned positive number.

 Explanatory comment- within the square brackets

 Termination

K. L. E. Technological University, Hubli-


12/21/21 8
31.
Demonstration Example 1:
Write an algorithm to add two numbers.
Name of the Algorithm : Addition of two numbers
Step1: Start

Step2:Read the two numbers as x and y


[Input the value for numbers]

Step3: sum  x + y [Compute sum]

Step4: Display sum [Output the sum]

Step5:Stop [End of the Algorithm]

K. L. E. Technological University, Hubli-


12/21/21 9
31.
Demonstration Example 2:
Write an algorithm to compute Area of Circle.
Step 1: Start

Step 2: Read r [Input the value for radius]

Step 3: Area  3.142*r*rr*r*rr


[Compute Area using formulas]

Step 4: Display Area [Output the value of computed area]

Step 5: Stop [End of the Algorithm]

K. L. E. Technological University, Hubli-


12/21/21 10
31.
Demonstration Example 3:
Write an algorithm to convert the length given in feet to
inches.
Step 1: Start

Step 2: Read L_ft [Input the length in feet]

Step 3: L_inches  L_ft *r*r 12 [Compute the length to inches]

Step 4: Display L_inches [Output the length in inches]

Step 5: Stop
K. L. E. Technological University, Hubli-
12/21/21 11
31.
Hands on

Write an algorithm to find sum and average of 3 numbers

Write an algorithm to find area of triangle

Write an algorithm to calculate simple interest

Write an algorithm to convert the distance in kilometers to


meter.

Write an algorithm to swap two numbers


(Hint: 3 ball Jugglers game)

K. L. E. Technological University, Hubli-


12/21/21 12
31.
Activity1
Identify errors in the algorithm
Name: Algorithm to compute sum of two numbers

Step 1 : Start Step 1 : Start


Step 2 : s  a+b Step 2 : Read a,b
Step 3 : Read a,b Step 3 : s  a+b
Step 4 : Stop Step 4 : Display s
Step 5 : Stop

K. L. E. Technological University, Hubli-


12/21/21 13
31.
Activity2

Identify errors in the algorithm


Name: Algorithm to compute product of three numbers
Step 1 : Start Step 1 : Start
Step 2 : Read 1,2,3 Step 2 : Read a, b, c
Step 3 : Display s Step 3 : s  a*r*rb*r*rc
Step 4 : s  1*r*r2*r*r3 Step 4 : Display s
Step 5 : Stop Step 5 : Stop

12/21/21 K. L. E. Technological University, Hubli-31. 14


Different patterns/types of Algorithms
1) Sequential
• Sequential flow (a step by step continuous flow or execution)

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]

Step 2: read a number A [Input the integer number]

Step 3: If A is greater than 0 [compare A to zero]


display “number as positive” True block
CONDITIONAL
else BLOCK
False block
display “number as negative”
end if

Step 4: Stop [End of the Algorithm]


K. L. E. Technological University, Hubli-
12/21/21 16
31.
Practice Examples
Write an algorithm to find largest of two numbers

Write an algorithm to read a number and check number is odd


or even

Write an algorithm to read age of a person and check person


is eligible for voting.

Write an algorithm to check whether two numbers are equal


or not.

K. L. E. Technological University, Hubli-


12/21/21 17
31.
REVISITING CASE STUDY-1 TO WRITE 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.

Algorithm : To calculate Total Bill Amount


Step 1 : Start
Step 2: Read first N kilometres, Cost per kilometre for first N kms (X),
Cost per kilometre after N kms (Y), Distance (D)
Step 3: [Compute Total Bill Amount]
Total Bill Amount = X + (D-N) * Y
Step 4: Display Total Bill Amount
Step 5: Stop
REVISITING CASE STUDY-2 TO WRITE
ALGORITHM
A black colour Audi Q7 petrol car has a fuel tank capacity of
60 litres and a mileage of 14 kmpl. How far the car can
travel on Rs.1066.64 worth of petrol?

School of Computer Science and Engineering


Class Activity-1
For the given problem carryout all the steps of problem solving framework
Y Mohan
x (2,8)

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

School of Computer Science and Engineering


WRITE ALGORITHMS
1. In an university, student performance for a course is
evaluated based on weighted score, which is computed
considering the marks obtained in 3 assignments and 2
exams. The assignment and exams are conducted for a
maximum of 100 marks. The weightage of each assignment
and each exam is 10% and 35% respectively. Write an
algorithm to compute the weighted score base.
2. Write an algorithm to calculate the acceleration from an
aircraft carrier catapult, given the jet’s take off speed in km/
hr and the distance(metres) over which the catapult
accelerates the jet from rest to takeoff. Assume constant
acceleration. Also calculate the time(in seconds) for the
fighter to be accelerated to take off speed. Assume take off
speed of 278 km/hr and a distance of 94 mts.
Relevant formulas:
v= at
S=1/2 at2

You might also like