ALGORITHM
Algorithm
is defined as “a sequence of instructions designed in such a way that if the
instructions are executed in the specified sequence, the desired result will be
obtained”
It is also
defined as “any problem whose solution can be expressed in a list of executable
instruction”.
·
Set of step-by-step instructions that perform a
specific task or operation
·
Natural language NOT programming language.
·
Algorithm is the sequence of steps to be performed
in order to solve a problem by the computer.
·
Three reasons for using algorithms are efficiency,
abstraction and reusability.
·
Algorithms can be expressed in many different
notations, including natural languages, pseudocode, flowcharts and programming
languages.
·
Analysis of algorithms is the theoretical study of computer
program performance and resource usage, and is often practiced abstractly
without the use of specific programming language or implementation.
·
The practical goal of algorithm analysis is to predict
the performance of different algorithms in order to guide program design decisions.
·
Most algorithms do not perform the same in all
cases; normally an algorithm’s performance varies with the data passed to it.
·
Typically, three cases are recognized: the best case, average case
and worst case Worst case analysis of algorithms is considered to be crucial to
applications such as games, finance and robotics.
·
O-notation, also known as Big O-notation, is the most
common notation used to express an algorithm’s performance in formal manner.
1. In the algorithm each and every instruction
should be precise and unambiguous.
2.
The instruction in an algorithm should not be
repeated infinitely.
3.
Ensure that the algorithm will ultimately
terminate.
4.
The algorithm should be written in sequence.
5.
It looks like normal English.
6.
The desired result should be obtained only after
the algorithm terminates.
Time -
Lesser time required.
Memory - Less memory required.
Accuracy
- Suitable or correct solution obtained.
Sequence
- Must be sequence and some instruction may be repeated in number of times or
until particular condition is met.
Generability
- Used to solve single problem and more often algorithms are designed to handle
a range of input data.
Problem 1: Find the area of a Circle of
radius r.
Inputs to the algorithm:
Radius r
of the Circle.
Expected output:
Area of
the Circle
Algorithm:
Step 1:
Start
Step2:
Read input the Radius r of the Circle
Step3:
Area ←PI*r*r // calculation of area
Step4:
Print Area
Step 5:
Stop
Problem2: Write
an algorithm to read two numb rs and find th ir sum.
Inputs to
the algorithm:
First
num1.
Second
num2.
Expected
output:
Sum of
the two numbers.
Algorithm:
Step1:
Start
Step2:
Read\input the first num1.
Step3:
Read\input the second num2.
Step4:
Sum← num1+num2 // c lcul tion of sum
Step5:
Print Sum
Step6:
Stop
Problem 3: Convert
temperature Fahrenheit to Celsius
Inputs to
the algorithm:
Temperature
in Fahrenheit
Expected
output:
Temperature
in Celsius
Algorithm:
Step1:
Start
Step 2:
Read Temperature in Fahrenheit F
Step 3: C
← 5/9*(F-32)
Step 4:
Print Temperature in Celsius: C
Step5:
End
Problem
4: Find the largest number between A and B
Inputs to
the algorithm:
A, B
Expected
output:
Largest A
or B
Algorithm:
Step 1:
Start
Step
2:Read A, B
Step 3:
If A is less than B, then
Big=B
Small=A
Print A
is largest
Else
Big=A
else
Step 5:
Stop
Small = B
Step 4:
Write (Display) BIG, SMALL
Step 5:
Stop
Problem
5: To determine student’s average grade and indicate whether successful or
fail.
Step 1:
Start
Step 2:
Input mid-term and final
Step 3:
average=(mid-term + final)/2
Step 4:
if (average < 60) then
Print
“FAIL”
Print
“SUCCESS”
Problem 6: A
algorithm to find the l rgest v lue of any three numbers.
Step1:
Start
Step2:
Read/input A,B and C
Step3: If
(A>=B) and (A>=C) then Max=A
Step4: If
(B>=A) and (B>=C) then Max=B
Step5:If
(C>=A) and (C>=B) then Max=C
Step6:
Print Max
Step7:
End
Problem 7: An
algorithm to calculate even numbers between 0 and 99.
Step 1:
Start
Step 2:
Read n or Initialize n=99
Step 3:
Initialize i=2
Step 4:
If i<=n, then goto step 5 else goto step 7
Step 5:
If i%2=0, then goto step 5.1,5.2 else goto step 6
Step
5.1:Print i
Step
5.2:i=i+1 goto step 4
Step 6:
i=i+1 goto step4
Step 7:
Stop
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.