Monday, July 29, 2013

Algorithms


Algorithm is simply a set of rules used to perform some tasks. In computer terminology algorithm is collection of instructions occurring in some specific sequence and such algorithm defines decision making, flow of data etc.
Algorithm is step done before implementing program in computer, because algorithm helps to design code for programm. After implementation of programm it is compiled by computer compiler and then programm will run. Also important point about algorithm is when program is run it may ask for input data and perform desired action. Following figure 1 shows process of writing programm code.
Figure: steps of implementing code
Algorithm section is divided int two parts:
  • Algorithm Heading:- It consist of title of algorithm, problem description and input and output.
  • Algorithm Body:-It consist of logical body of algorithm i.e conditional statement, assignments statements and others statement.
There are some standered way to write algorithms we are discussing in following section:

  1. The complete algorithm comes under one main procedure body. The title of algorithm must be specified after keyword “Algorithm” and then the parameters of the method.

    Ex:

      Algorithm name_of _procedure(p1,p2,p3------Pn)

  2. Then heading section include problem description,input and output.
  3. The compound statement should included in {} brackets.
  4. For assignments operators use <-
  5. There other various types of operators such as ” <,>,<=,>=,=, != ”.
  6. The input and output can written by using menthods : read(“Message”) and write(“Message”).
  7. The conditional statement written as :

    if(condition) then statement else statement
  8. The while loop can be written as:

    while(condition)do

    {

    Statement 1

    Statement 2
       :
       :
    Statement n
    }
  1. The for loop can be written as:
       for var<-val1 to val2 do
       {
       Statement 1
    Statement 2
       :
       :
       Statement n
       }

Lets take an example for writing algorithm for multiplication of two numbers:

Algorithm mul (5,2)
//problem Description:Algorithm for finding multiplication of no 5 //and 2.
//Input:Two numbers
//Output:Multiplication of 5 and 2
result<- (5*2)