1.3. ALGORITHMS- DEFINITION, CHARACTERISTICS, EXAMPLES, ADVANTAGES AND LIMITATIONS:
1.3.1 Definition
· Algorithms are step by step procedures to solve problems in our real life.
· For example, cooking recipes for a particular dish can be designated as an algorithm.
· However, in Computer Science Algorithm has special meaning. The term Algorithm is used to refer to the logic of the program.
1.3.2 Characteristics of an Algorithm
· There are some characteristics of a good algorithm.
i) Input: There should be one or more input.
ii) Output: There should be one or more output.
iii) Definiteness: It means that every algorithm should have a definite procedure to be followed i.e. it should be unambiguous.
iv) Finiteness: It means that every algorithm should have a finite number of steps or algorithm should terminate after finite steps.
v) Efficient: It should memory and time efficient
· Steps to be followed to write an algorithm
1 Start
2 Identify the input and output variables
3 Calculate /Process according to the problem
4 Output the result
5 Stop/End
1.3.3 Examples
Example 1 Write an algorithm for addition of two numbers
1 Start
2 Read / Input a, b and store the result in c
3 c=a+b
4 Output/Print c
5 Stop
Example 2 Write an algorithm for finding average of three numbers
1 Start
2 Input/Read three numbers a,b,c
3 Compute sum=a+b+c
4 Compute average=sum/3
5 Print average value
6 Stop
Example 3 Write an algorithm to interchange two numbers
1 Start
2 Input/ Read a,b
3 Let temp=a
4 Let a=b
5 Let b=temp
6 Stop
Example 4Write an algorithm to find greatest of three numbers
1 Start
2 Read/ Input a, b, c
3 if a>b and a>c then greatest=a
else if b>c then greatest=b
else greatest= c
4 Print greatest
5 Stop
Example 5Write an algorithm to find roots of quadratic equation. (Consider all three cases)
1 Read/ Input coefficients a,b,c
2 Calculate d=((b*b)-4*a*c)
3 if d=< 0 then Print “roots are imaginary” and goto step 12
4 if d=0 then goto step 6
5 if d>0 then goto step 9
6 r1= (-b/2*a)
7 r1=r2
8 Print “roots are real and equal”, r1, r2 and goto step 12
9 r1=(-b+sqrt(d))/(2*a)
10 r2= (-b-sqrt (d))/ (2*a)
11 Print “roots are real”, r1, r2
12 Stop
Example 6 Write an algorithm to find number is odd or even
1 Start
2 Input n
3 if (n mod 2) =0 then Print “n is even”
else Print “n is odd”
4 Stop
Example 7Write an algorithm to find the factorial of a number (4!= 1*2*3*4=24)
1 Start
2 Input n
3 if n=0 then Print factorial=1 and goto step 8
4 Initialize factorial=1
5 Initialize i =1
6 while (i<n) do the following
a) factorial=factorial*i
b) increment i by 1
7 Print factorial
8 Stop
Example 8 Write an algorithm to find sum of digits of a number (2020)
1 Start
2 Input number
3 Initialize sum = 0
4 While number is greater than zero do the following two steps
Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum i.e. sum= sum +number % 10
Divide number by 10 i.e. number/10
5 Print sum
6 Stop
Example 9Write an algorithm to find the average of n numbers (avg= (1+2+3+....+n)/n)
1 Start
2 Input n
3 initialize i=1,sum=0
4 while(i<=n) do the following
a)sum=sum + i
b)increment i by 1
5 avg=sum/n
6 Print avg
7 Stop
1.3.4 Advantages and Limitations
Advantages
1 Algorithm can be easily written.
2 Easily understood by anyone.
3 Easily can be converted into a program.
4 If a program is written on a based algorithm then it has less error and reediting can be minimized.
5 While writing Algorithms we can only concentrate on logic or solution rather than syntax.
6 Algorithms are the same for any language.
Limitations
1 It is not executable.
2 Errors cannot be detected.
3 There is no syntax to write an algorithm.
4 If an algorithm is written incorrect then the program based on it will also be wrong or incorrect.
0 Comments:
Post a Comment