Thread: Swapping 2 variables in C

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    Swapping 2 variables in C

    Hi! I'm quite new to C programming, and I'd be glad if you could help me solve this little problem I'm having at the moment please..

    I need to input 2 integer numbers (e.g. int a; int b, read them (e.g. a=10; b=17; or any value that may be input by the user), then swap them without using a 3rd temporary variable

    can you please help me with it?
    Thanks and regards

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    #define sawp(a, b) (a ^= b ^= a ^= b)

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You'd better read this thread... and in particular this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Code:
    void swap( int &x, int &y ) {
      int temp;
      temp = x;
      x = y;
      y = temp;
    }
    
    or
    
    void swap(int *x, int *y) {
      int temp;
      temp = *x;
      *x = *y;
      *y = temp;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dharh
    Code:
    void swap( int &x, int &y ) {
      int temp;
    
    }
    
    or
    
    void swap(int *x, int *y) {
      int temp;
    }
    You obviously missed the original question. They said without a temporary variable. Doubtless this is one of those homework problems that are supposed to increase your knowledge, but in fact just end up teaching you how to do things the Wrong Way(TM).

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Every now and then I throw code around, sorry if a decapitate a head or two.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int a;
    int b;
    
    a = a + b;
    b = a - b;
    a = a - b;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm...I am not sure why you don't want to use a temporary variable.

    Code:
    int a, b, temp
    
    //first we'll swap with no temp
    a = 4;
    b = -35;
    
    a = a + b;
    b = a - b;
    a = a - b;
    
    //now with the temp
    a = 4;
    b = -35;
    
    temp = a;
    a = b;
    b = temp;
    The reason I question why you choose to do things without the temp is because its operational overhead exceeds whatever memory conservation it provides. I assume this is some sort of assignment that is to only reasonable explanation for this attempt.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dharh
    Every now and then I throw code around, sorry if a decapitate a head or two.
    I throw out code from time to time also. However, I was just letting you know that they were asking how to do it without another variable.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    This is the understanding that there are lots of ways to solve a problem. And he presented an interesting way.. every one cannot do it in a second.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by zahid
    This is the understanding that there are lots of ways to solve a problem. And he presented an interesting way.. every one cannot do it in a second.
    Actually, with the specifications the original poster provided, there are very few ways to solve this. The two most common are addition and XOR-ing.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM