C Language : Algorithm

Algorithm :

Program = Algorithm + Data
Def : Algorithm is a finite set of instructions that, it followed accomplishes a particular task.
Algorithm should satisfy the following criteria.
  1. Input : Zero or more quantities are externally supplied.
  2. Output : At least one quantity is produced.
  3. Definiteness : Each instruction is clear and unambiguous. Ex Add B or C to A.
  4. Finiteness : Algorithm should terminate after finite number of steps when traced in all cases
    Ex: Go on adding elements to an array
  5. Effectivenes :Every instruction must be basic i.e., it can be carried out, by a person using pencil and paper.

Algorithm must also be general to deal with any situation.
Example : Calculate Fibonacci Series.
There are two forms of representing an activity. They are
  1. By describing the process step - by - step - Algorithm
  2. By representing the various steps in the form of a diagram - flowchart.

Algorithm with Examples :

The same problem can be solved with different methods. So, to solve a problem different algorithm, may be accomplished. Algorithm may vary in time, space utilized. User writes algorithm in his/her own languages.
So, it can not be executed on computer. Algorithm should be in sufficient detail that it can be easily translated in to any of the languages.


Examples - 1 : Algorithm for adding two numbers.
  • Step - 1 : Start
  • Step - 2 : Read 2 numbers as A and B
  • Step - 3 : Add ;numbers A and B and Store result in C
  • Step - 4 : Display C
  • Step - 5 : Stop
Examples - 2 : Algorithm for finding Average of 3 numbers.
  • Step - 1 : Start
  • Step - 2 : Read the numbers a, b, c
  • Step - 3 : Compute the sum and divide by 3
  • Step - 4 : store the result in variable d
  • Step - 5 : Print value of d
  • Step - 6 : Stop
Examples - 3 : Algorithm for finding Average of 'n' inputted numbers.
  • Step - 1 : Start
  • Step - 2 : Read the numbers n
  • Step - 3 : Initialize i to zero
  • Step - 4 : Initialize sum to zero
  • Step - 5 : If i is greater than n
  • Step - 6 : Read a
  • Step - 7 : Add a to sum
  • Step - 8 : Go to step - 5
  • Step - 9 : Divide sum by n & store the result in avg
  • Step - 10 : Print value of avg
  • Step - 11 : Stop

Post a Comment

0 Comments