Wednesday, October 10, 2012

What is an Algorithm?

We saw above that planning a program involves defining its logic (the correct sequence of instructions needed to solve the problem at hand).The term algorithm is often used to refer to the logic of a program. It is step-by step description of how to arrive at the solution of the given problem. It may be formally defined as a sequence of instruction, designed in a manner that, if the instruction is executed in the specified sequence, the desired results will be obtained. In order to qualify as an algorithm, a sequence of instructions must possess the following characteristics:

1.    Each and every instruction should be precise and unambiguous.
2.    Each instruction should be such that it can be performed in a finite time
3.    One or more instructions should not be repeated infinitely. This ensures that the algorithm will ultimately terminate.
4.    After performing the instruction, that is, after the algorithm terminate, the desired results must be obtained.

Sample Algorithms

What is computer algorithm
To gain insight in to algorithms, let us consider some simple examples

Examples:-

50 students in a class appeared in their final examination.Their mark sheets have been given to yo.The division column of the mark sheet contains the division(FIRST,SECOND,THIRD or FAIL) obtained by the student.Write an algorithm to calculate and print the total number of students who passed in FIRST  division.

Algorithm:-
Step:- 1  Initialize Total_First_Division and Total_Marksheets_Checked to zero.
Setp:- 2 Take the mark sheet of the next student.
Step:- 3 Check the division column of the mark sheet to see if it is FIRST.If no,go to Step 5.
Step:- 4 Add 1 to Total_First_Division
Step:- 5 Add 1 to Total_Marksheets_Checked.
Step:- 6 Is Total_Marksheets_Checked= 50? If no, go to Step2.
Step:- 7 Print Total_First_Division.
Step:- 8 Stop