Results 1 to 10 of 10

Thread: funtion swap

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Answers
    3

    funtion swap

    How do you write a function swap?


  2. #2
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: funtion swap

    you can use follwing code:
    #include<stdio.h>
    #include<conio.h>
    void swap(int *a,int *b)
    {
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
    printf("a=&#37;d b=%d",*a,*b);
    }
    void main()
    {
    int a=5,b=8;
    swap(&a,&b);
    }


  3. #3
    Junior Member
    Join Date
    Feb 2008
    Answers
    3

    Re: funtion swap

    Well, Swetha's method is proper, but only meant for integers.
    Following is the swap function for any type like: int, char, structure, pointers etc. anything.

    template<class T>
    void swap(T &data1, T &data2)
    {
    T temp = data1;
    data1 = data2;
    data2 = temp;
    }


  4. #4
    Junior Member
    Join Date
    Feb 2010
    Answers
    18

    Re: funtion swap

    fine...you can use a single statement to swap two numbers without the use of temporary variable...

    a^=b^=a^=b

    regards rms...


  5. #5
    Junior Member
    Join Date
    Feb 2008
    Answers
    3

    Re: funtion swap

    No, 1st of all, it's just an illusion that you don't use temporary. When u do a ^= b, the XOR of 2 variables is indeed stored in Accumulator (which is a temporary). So, this operation will be slower (swap result into accumulator and then accumulator back to the variable 'a').
    2nd thing is, you can't swap structures or complex data types with this method.


  6. #6
    Junior Member
    Join Date
    Feb 2010
    Answers
    18

    Re: funtion swap

    ya,correct,its for integer variables only...But we dont use temporary varibles explicitly...


  7. #7
    Junior Member
    Join Date
    Mar 2010
    Answers
    4

    Re: funtion swap

    Try with the followings.
    Code:
    main()
    {
            int a=10;
            int b=20;
            printf("a = %d\nb = %d\n",a,b);
            a =a+b; b=a-b; a=a-b; // This is first way.
    
            a =a*b; b=a/b; a=a/b;
            printf("a = %d\nb = %d\n",a,b);
    }
    Code:
    main()
    {
            int a=10;
            int b=20;
            printf("a = %d\nb = %d\n",a,b);
            a =a*b; b=a/b; a=a/b; //This is second way
            printf("a = %d\nb = %d\n",a,b);
    }



  8. #8
    Junior Member
    Join Date
    Feb 2010
    Answers
    18

    Re: funtion swap

    Hi,The second way will not work properly when the variables a or b equal to zero,another one problem is that if "a*b" exceeds integer range, it will give unexpected results.


  9. #9
    Junior Member
    Join Date
    May 2010
    Answers
    2

    Re: funtion swap

    /*Usin call by value parameter*/
    void swap(int a,int b)
    {
    int c=a;
    a=b;
    b=c;
    printf("After swap : a = %d, b = %d\n",a,b);
    }
    /*Usin call by reference*/
    void swap_a(int &a, int &b)
    {
    int temp = *a;
    *a =*b;
    *b = temp;
    }
    /*Usin reference parameter*/
    void swap_b(int &a,int &b)
    {
    int c = a;
    a= b;
    b= c;
    }
    void main()
    {
    int a =5,b=6;
    swap(a,b);
    swap_a(&a,&b);
    printf("After swap : a = %d, b = %d\n",a,b);

    swap_b(a,b);
    printf("After swap : a = %d, b = %d\n",a,b);

    }


  10. #10
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: funtion swap

    What language, and what are you trying to swap?

    In C++ it's easy:

    Code:
    template <typename T>
    void swap(T& a, T& b)
    {
      T tmp = a;
      a = b;
      b = tmp;
    }
    Since C doesn't provide built-in support for generics, you either have to write separate functions for each data type, or you need to muck with void pointers (not recommended).


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact