what is swaping ?????

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • montakin
    New Member
    • Dec 2009
    • 2

    what is swaping ?????

    I want to know details about swaping in c or c++ language and give me some example code on it....please..
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    Code:
    int a = 5565, b = 30;
    int temp;
    
    temp = a;  //Save value of a
    a = b;  //b goes to a  
    b = temp;  //a goes to b, swap completed!(temp no more needed)

    Comment

    • murugesandins
      New Member
      • Jun 2006
      • 4

      #3
      swapping two values with normal variables and user-defined variables:


      #include <string.h>
      #include <iostream.h>
      class murugesandins
      {
      int a ;
      int b ;
      char name[100] ;
      public:
      murugesandins()
      {
      a = 8 ;
      b = 12 ;
      memset( name, '\0', 100) ;
      strcpy( name, "defaultVal ue") ;
      }
      murugesandins(c har *name, int a, int b):a(a),b(b)
      {
      strcpy( this->name, name) ;
      }
      murugesandins operator +(murugesandins next)
      {
      murugesandins temp(name, a, b) ;
      temp.a = temp.a + next.a ;
      temp.b = temp.b + next.b ;
      return temp ;
      }
      murugesandins operator -(murugesandins next)
      {
      murugesandins temp(name, a, b) ;
      temp.a = temp.a - next.a ;
      temp.b = temp.b - next.b ;
      return temp ;
      }
      void myValue()
      {
      cout << "murugesand ins name:" << name << endl ;
      cout << "murugesand ins a:" << a << endl ;
      cout << "murugesand ins b:" << b << endl ;
      }
      };
      int main()
      {
      int a = 6, b = 12 ;
      cout << "Initial values\na:" << a << ": b:" << b << ":\n" ;
      a = a + b ;
      b = a - b ;
      a = a - b ;
      cout << "Final values\na:" << a << ": b:" << b << ":\n" ;
      murugesandins one( "one", 1, 3) ;
      murugesandins two( "two", 6, 12) ;
      one.myValue() ;
      two.myValue() ;
      one = one + two ;
      two = one - two ;
      one = one - two ;
      one.myValue() ;
      two.myValue() ;
      return 0 ;
      }

      Comment

      Working...