11.03.2011

Algorithm

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm can be expressed in human readable language like as English. Algorithm is language depended, well structured and detailed.

Rules for constructing an Algorithm

When you are going to create an algorithms, keep following point in mind as:
  • Input: There should be zero or more values which are to be supplied.
  • Output: At least one result is to be produced.
  • Definiteness: Each step must be clear and unambiguous.
  • Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.
  • Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.
  • Comment Session: Comment is additional info of program for easily modification. In algorithm comment would be appear between two square bracket []. For example: [ this is a comment of an algorithm ]
The demonstration of for loop algorithm through calculate factorial number C program/algorithm:
Algorithm for calculate factorial number
Figure: Algorithm of demonstration of how to
write "for loop" in algorithm factorial program

Let's understand to algorithm by example:

Q. Write algorithm to calculate the sum and average of two numbers.

Ans.

[procedure for calculate sum and average of two numbers]
Step1 : Start
Step2 : Read two numbers n,m
Step3 : Calculate sum=n+m
Step4 : Calculate avg=sum/2
Step5 : Print sum,avg
Step5 : Stop
[End of procedure for calculate sum and average of two numbers]


Q. Write an algorithm to convert a decimal number into binary.

Ans.

[procedure for convert decimal to binary number]
Step1 : Start
Step2 : Read number num
Step3 : Set x=1
Step4 : B(x)= num MOD 2
Step5 : num=num/2
Step6 : If num is equal to 0 then goto step8
Step7 : x=x+1
Step8 : goto step3
Step9 : Print B(x)
Step10 : x=x-1
Step11 : If x is greater than 0 then goto step8
step12 : Stop

No comments:

Post a Comment