+ Reply to Thread
Results 1 to 7 of 7

Thread: swap 2 numbers without arithmatic operations, pointers & Temp

  1. #1
    Junior Member
    Join Date
    Feb 2006
    Location
    India
    Posts
    13
    Rep Power
    8

    swap 2 numbers without arithmatic operations, pointers & Temp









    Hi friends




    I need an algorithm to swap two variables without using temporary variables,arithmatic operators and pointers. Please tell your logic to solve this problem.

  2. #2
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello







    #include <iostream>

    using namespace std;

    int main()
    {
    float a = 1.7;
    float b = -7.1;

    cout << "a = " << a << " b = " << b << endl;
    // swap a with b
    a = a + b;
    b = a - b;
    a = a - b;
    cout << "after swapping a with b:" << endl;
    cout << "a = " << a << " b = " << b << endl;

    cin.get(); // wait
    return EXIT_SUCCESS;
    }

    this is the logic of simple program

    bye



    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

  3. #3
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello



    the program given will help u to decide the algorithem okey



    so BUILD IT ON UR OWN very easy . try it out im sure u will get



    bye[img]smileys/smiley1.gif[/img]




    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

  4. #4
    Junior Member
    Join Date
    Feb 2006
    Location
    India
    Posts
    1
    Rep Power
    8
    Hey use these lines intead of those given by sowmya

    think ^ denotes XOR

    a = a ^ b
    b = b ^ a
    a = a ^ b

    you can decode these steps as

    1. a = a ^ b -> a has both the values of a and b.

    2. b = b ^ a -> this is equivalent to b = b ^ (a ^ b). XORing same values results in 1. So the value a is set to b.

    3. a = a ^ b -> this is equivalent to a = (a ^ b) ^ (a). So the value b is set to a.

    Note : the values betwwen ( and ) gives the value replaced for that variable.




  5. #5
    Super Moderator sk_kireeti's Avatar
    Join Date
    Feb 2006
    Posts
    2,279
    Rep Power
    65
    Thanks Sankar... I was really searching for this logic.

  6. #6
    Member
    Join Date
    Feb 2006
    Location
    India
    Posts
    96
    Rep Power
    8


    Great job dude.......


    Rock on..............


    [img]smileys/smiley1.gif[/img][img]smileys/smiley17.gif[/img][img]smileys/smiley2.gif[/img]
    ArunabhA

    ******************
    *---------------------*
    * LET THERE BE LIGHT *
    *---------------------*
    ******************

  7. #7
    Moderator
    Join Date
    Feb 2006
    Posts
    1,411
    Rep Power
    24
    hello



    Ya this is the logic XOR operator will do it .. Gud work Sarkar!



    bye



    sowmya


    http://livetolead.blogspot.com/
    all the best
    Arise Awake N Stop Not Until Ur Goal Is Reached!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How many temp variables are required to swap two numbers?
    By sagar_dspt in forum DATA STRUCTURES, C, C++, VC ++
    Replies: 1
    Last Post: 19-02-07, 03:21 PM
  2. Write program to swap 2 numbers
    By HELP in forum DATA STRUCTURES, C, C++, VC ++
    Replies: 5
    Last Post: 01-05-06, 09:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Content Relevant URLs by vBSEO 3.5.1 PL1