Copyright © Programs ++
Design by Dzignine
Sunday, 29 January 2012

Swapping two variables(with and without using a third variable)

Swapping of variables is one of the most common type of operation in c++. It involves interchanging the values of two variables. It can be achieved by one of three ways.
1 - Using a third variable.
2 - Without using a third variable

1- Swapping using a third variable : In this method a third variable is used to hold the value of one of the variables.Suppose we want to swap the values of two integers, say x and y. To do that , we would first declare a third variable, say temp. Next, assign the value of x to temp so we have to copies of x , one in x and another in temp. 
Then, assign the value of y to x. This will overwrite the value of x with y.
Now, assign the value of x to y, using temp, which contains the original value of x, and there we have it. Swap successful !!!! The following program shows the above procedure :
// Program to swap two numbers
#include <iostream>

int main()
{
    int x,y,temp;
    std::cout<<" Enter the value of x : ";
    std::cin>>x;
    std::cout<<"\n Enter the value of y : ";
    std::cin>>y;
    std::cin.ignore();                  // to clear the buffer
    std::cout<<"\n Value of x and y before swapping : "<<x<<" "<<y;
    // swaps the two variables x and y using the third variable temp
    temp = x;           
    x = y;
    y = temp;
    std::cout<<"\n Value of x and y after swapping : "<<x<<" "<<y;
    std::cin.get();      // to prevent the console from closing immediately
    return 0;
} // end of main 
Swapping using a third variable











 2 - Swapping using WITHOUT using a third variable : This is a very simple concept to understand. First add both the variables, then subtract two times. For e.g. , let us swap two variables x and y each having values 10 and 5 respectively. 
Then, according to swap the values , firstly
x = x + y; // adding x and y and assigning the result to x ie x = 10 + 5 i.e x = 15;
y = x - y; // subtracting  x and y and assigning it to y i.e y = 10 - 5 which is 5, which is also the original value of x, hence y now has the original value of x.
finally x = x - y; // i.e 15 - 5 = 10, hence x = 10, which is the original value of y.
Who la, we have swapped the two value.
NOTE : This method is only useful when x > y and will not work the other way round.
A program is given below to demonstrate the above method :
 // Program to swap two numbers(without using a third variable)
#include <iostream>

int main()
{
    int x,y;
    std::cout<<" Enter the value of x : ";
    std::cin>>x;
    std::cout<<"\n Enter the value of y : ";
    std::cin>>y;
    std::cin.ignore(); // to clear the buffer
    std::cout<<"\n Value of x and y before swapping : "<<x<<" "<<y;
    x = x + y;        
    y = x - y;
    x = x - y;
    std::cout<<"\n Value of x and y after swapping : "<<x<<" "<<y;
    std::cin.get();      // to prevent the console from closing immediately
    return 0;
} // end of main

Swapping without using a third variable









 
 

2 comments:

  1. One advantage with the first method is that it also works for more advanced types, like strings, vectors etc.

    The second method also has some problems if x + y overflow.

    ReplyDelete
    Replies
    1. the first method is more commonly used and although it requires the use of a 3rd variable, it has no variations and is perfectly stable, as compared to using 2nd method which only works when x > y.

      Delete

Comment Here....