Translate

Thursday, 4 July 2013

Pseudocode Algorithms

While it seems unnecessary in a production environment. It saves time when you can design the set of steps it will take to perform the particular operations. In this way we more than likely reduce the amount of time spent writing in the programming language and write more efficient code.

A solution to a problem is clearly defined in pseudocode the programmer can focus on the syntax and semantics of the Programming Language.

e.g. Euclid Algorithm

  1. Input a, b
  2. Initalize the value of x to a and the value of y to b
  3. If x>y then set x to x-y
  4. If x<y then set y to y-x
  5. Repeat steps 3 and 4 until x=y
  6. Output x (or y) and halt.
   C program
  main(){
   int a, b, x, y;
   
   scanf("%d%d", &a,&b);
   x=a;
   y=b;
   
   do{
     if(x>y) x=x-y;
     else if(x<y) y=y-x;
   }while(x!=y);

   printf("gcd=%d\n", x);
  }
As with the above example the Greatest Common Denominator can be found. They both relate perform the same operations. The major difference is the syntax and that the c program will compile.

No comments:

Post a Comment