Thursday, November 29, 2012

Inline Function, Macros and Recursion.



Inline function is the optimization  technique used by the compilers.  Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration. While invoking an inline method does not generate a function call, but rather expands the body at the point of call. Use inline definitions only for methods that are short.  Use of too many inline function may cause thrashing in the memory. It also save overhead of variables on the stack.

The inline functions are similar to macros but inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor.




Example of Macros : Largest of two no.




#include <iostream.h>

#define max(a,b) (a>b?a:b)  //Macro defined
  void main()
      {
           int x,y;
           cout<<"\n\tEnter two no ";
           cin>>x>>y;
           cout<<"\n\tLarges of - "<<x<<" and " <<y<<" is "<<max(x,y);
      }





Example of Inline Fuction : Largest of two no.




#include <iostream.h>

  inline int max(int x,int y)
      {
           return x>y?x:y;
      }
  void main()
      {
           int x,y;
           cout<<"\n\tEnter two no ";
           cin>>x>>y;
           cout<<"\n\tLarges of - "<<x<<" and " <<y<<" is "<<max(x,y);
      }



When function called itself to perform an finite iteration its called recursion. The Recursive functions call themselves to work towards a solution to a problem. While a recursive function perform, it is keep creating a new stack frame on top of the current, a stack is a special area of memory.  Whenever a method is called, an item is placed on the stack for each local variable that we passing to the method.

Tail recursive is method has the recursive call as the last statement in the method and the recursive methods that are not tail recursive are known as non-tail recursive. Tail Recursive functions are very easy to convert to an iterative one. Recursive functions are occupy more space and slow than normal iterative process but sometime when we need multiple loop use of recursion is very handy.



Example of Non-tail Recursive function : Bubble sort to call two recursive function for two loops.



//Bubble Sort

#include <iostream.h>

class rec_sort
{
 private: int Arr[100],n;
 public:
  rec_sort()
      {
           n = 0;
      }
   void accept()
      {
           int i;
           cout<<"\n\tEnter the value of n:";
           cin>>n;
           cout<<"\n\tEnter "<<n<<" Values \n";
           for(i=0;i<n;i++)
              {
                   cin>>Arr[i];
              }
      }
   void disp()
      {
           int i;
           for(i=0;i<n;i++)
              {
                   cout<<Arr[i]<<"  ";
              }
      }
  int sort(int i)
      {
           if(i>=n)
              {
                   return 0;
              }
           else
              {
                   chks(i,i+1);
                   sort(i+1);
              }
      }

  int chks(int i,int j)
      {
           int tmp;
              if(j>=n)
                   {
                        return 0;
                   }
              else
                {
                        if(Arr[i]>Arr[j])
                            {
                                tmp=Arr[i];
                                Arr[i]=Arr[j];
                                Arr[j]=tmp;
                            }
                        chks(i,j+1);
                }
      }
 };

  void main()
      {
          rec_sort RS;
          int tmp;
          RS.accept();
          cout<<"\nOriginal No : ";
          RS.disp();
          cout<<"\nSorted No   : ";
          tmp=RS.sort(0);
          RS.disp();
      }





Example of Tail Recursion – Sum of ‘N’ natural number.




#include <iostream.h>

//Sum of first N natural No.
  int sum(int n)
      {
           if(n<=1)
              {
                   return n;
              }
           else
              {
                   return n+sum(n-1);
              }
      }
  void main()
      {
           int s=0,n;
           cout<<"\n\tEnter the value of n  ";
           cin>>n;
           s=sum(n);
           cout<<"\n\tSum = "<<s;
      }




Example of Tail Recursion – GCD of two No.




#include <iostream.h>
  //GCD of two No.

  int gcd(int,int);
  inline int max(int x,int y)
      {
           return x>y?x:y;
      }

  inline int min(int x,int y)
      {
           return x<y?x:y;
      }

  void main()
      {
           int m,n,hcf;
           cout<<"\n\tEnter two No. ";
           cin>>m>>n;

           hcf=gcd(max(m,n),min(m,n));
           cout<<"\n\tGCD of "<<m<<" and "<<n<<" = "<<hcf;
      }
  int gcd(int m,int n)
      {

           if(n<=0)
              {
                   return m;
              }
           else
              {
                   return gcd(n,m%n);
              }
      }




Thursday, November 22, 2012

Scope Resolution Operator - C++


Scope Resolution Operator : ‘::’ is known as Scope Resolution Operator. Different program modules are written in various blocks. Same variable name can be used in different blocks. Scope of a variable extends from the point of declaration to the end of the block. A variable declared inside a block is ‘local’ variable. scope resolution operator is used to define the member function outside the class. It is also used to print the value of global variable and local variable It comes in two forms:

::Num   -  unary operator – refers to external scope)

TestClass::Num

Its unary form is used to access a name that has external scope and has been hidden by local or class scope.


class TestClass
  {
      private :: //Declaration Private Data Member
      public:
           void GetData(); //Member Method               
  };

/* The Member Method using Scope Resolution operator.*/

void TestClass::GetData()  // GetData()
{
}



Example of Scope Resolution Operator.


#include<iostream.h>
#include <conio.h>

  class test
      {
           private:
              int num;
           public:
              test()
                   {
                        num=20;
                   }
              void disp()
                   {
                        int num=5;
                        cout<<"\n\tDisplaying the value of global variable "<< test::num;
                        cout<<"\n\tDisplaying the value of local variable "<< num;
}
};

void main()
{
test t;
t.disp();
}


Example - Find the Sum Series - //sum of series : x+x^2/2!+x^3/3! ... + x^n/n!


#include<iostream.h>
#include <conio.h>

  class sumseries
      {
           private:
              int x,n;
              float sum;
           public:
              void accept();
              void displaysum();
              int fact(int);
              double term(int,int);
              sumseries();
      };
      sumseries::sumseries()
           {
              sum=1.0;
           }
      void sumseries::accept()
           {
              cout<<"\n\tEnter value of N :";
              cin>>n;
               cout<<"\n\tEnter value of X :";
              cin>>x;
           }
      int sumseries::fact(int n)
           {
              int f=1,i;
              for(i=1;i<=n;i++)
                   {
                        f=f*i;
                   }
              return(f);
           }
      double sumseries::term(int p,int q)
           {
              int j;
              float y=1;
              for(j=1;j<=q;j++)
                   {
                        y=y*p;
                   }
              return y;
           }
      void sumseries::displaysum()
           {
              int i;
              for(i=1;i<=n;i++)
                   {
                        if(i<n)
                        cout<<x<<" ^ "<<i<<" / "<<i<<"! "<<"+ ";
                        else
                        cout<<x<<" ^ "<<i<<" / "<<i<<"! ";
                        sum=sum+(term(x,i)/fact(i));
                   }
              cout<<" = "<<sum<<"\n";
           }

      void main()
           {
              clrscr();
              sumseries s;
              s.accept();
              s.displaysum();
              getch();

           }