query regarding swap function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Narmada Padhy
    New Member
    • Feb 2007
    • 6

    query regarding swap function

    1>
    #include<iostre am>
    using namespace std;

    Code:
    void swap(int& i, int& j)
     {
        int tmp = i;
        i = j;
        j = tmp;
    cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;
    
    }
    int main()
    {
    int x, y;
    cout<<"enter the value of x and y"<<endl;
    cin>>x>>y;
    swap(x,y);
    cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
    }
    2>
    Code:
    #include <iostream>
    using namespace std;
    main()
        {
            int a,b;
            //void swap(int a,int b);
            cout<<"enter the value of a and b"<<endl;
            cin>> a>>b;
            swap (a,b);
            cout<<"The value after swap is"<<a<<":"<<b<<endl;
        }
    void swap(int& x, int& y)
        {
            x=x+y;
            y=x-y;
            x=x-y;
            cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
        }
    i have written this two simple program. but the problem is that in the first case both the cout is being printed but is the second case the cout inside the defination of the swap function is not printer. can any one please let me understand why? pzzzzzzzzzzzz
    Last edited by Ganon11; Mar 19 '07, 03:04 PM. Reason: code tags added
  • chella
    New Member
    • Mar 2007
    • 51

    #2
    Originally posted by Narmada Padhy
    1>
    #include<iostre am>
    using namespace std;

    void swap(int& i, int& j)
    {
    int tmp = i;
    i = j;
    j = tmp;
    cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;

    }
    int main()
    {
    int x, y;
    cout<<"enter the value of x and y"<<endl;
    cin>>x>>y;
    swap(x,y);
    cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
    }

    2>
    #include <iostream>
    using namespace std;
    main()
    {
    int a,b;
    //void swap(int a,int b);
    cout<<"enter the value of a and b"<<endl;
    cin>> a>>b;
    swap (a,b);
    cout<<"The value after swap is"<<a<<":"<<b< <endl;
    }
    void swap(int& x, int& y)
    {
    x=x+y;
    y=x-y;
    x=x-y;
    cout<<"The value after swap is"<<x<<"\t"<<y <<endl;
    }
    i have written this two simple program. but the problem is that in the first case both the cout is being printed but is the second case the cout inside the defination of the swap function is not printer. can any one please let me understand why? pzzzzzzzzzzzz

    Hi Narmada,
    Can u tell me which compiler u r using? I have executed it in UNIX C++ complier. It works fine.

    In the program u have posted i don't know y u have used the formal parameters as int& x,int& y.

    Here is the code which i have executed,

    Code:
    #include <iostream.h>
    main()
    {
    int a,b;
    void swap(int a,int b);
    cout<<"enter the value of a and b"<<endl;
    cin>> a>>b;
    swap (a,b);
    cout<<"The value after swap is"<<a<<":"<<b<<endl;
    }
    void swap(int x, int y)
    {
    x=x+y;
    y=x-y;
    x=x-y;
    cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
    }
    Regards,
    Chella
    Last edited by Ganon11; Mar 19 '07, 03:04 PM. Reason: code tags added

    Comment

    • arne
      Recognized Expert Contributor
      • Oct 2006
      • 315

      #3
      Originally posted by Narmada Padhy
      1>
      #include<iostre am>
      using namespace std;

      void swap(int& i, int& j)
      {
      int tmp = i;
      i = j;
      j = tmp;
      cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;

      }
      int main()
      {
      int x, y;
      cout<<"enter the value of x and y"<<endl;
      cin>>x>>y;
      swap(x,y);
      cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
      }

      2>
      #include <iostream>
      using namespace std;
      main()
      {
      int a,b;
      //void swap(int a,int b);
      cout<<"enter the value of a and b"<<endl;
      cin>> a>>b;
      swap (a,b);
      cout<<"The value after swap is"<<a<<":"<<b< <endl;
      }
      void swap(int& x, int& y)
      {
      x=x+y;
      y=x-y;
      x=x-y;
      cout<<"The value after swap is"<<x<<"\t"<<y <<endl;
      }
      i have written this two simple program. but the problem is that in the first case both the cout is being printed but is the second case the cout inside the defination of the swap function is not printer. can any one please let me understand why? pzzzzzzzzzzzz

      The reason is that your 2nd program uses the swap() function of the STL, since your function is not in scope at the time of the call. Try to completely comment out your swap function and things will still be swapped :)

      The first version works, since you define your function swap() before the call. So it's in scope.

      Comment

      • Narmada Padhy
        New Member
        • Feb 2007
        • 6

        #4
        Originally posted by arne
        The reason is that your 2nd program uses the swap() function of the STL, since your function is not in scope at the time of the call. Try to completely comment out your swap function and things will still be swapped :)

        The first version works, since you define your function swap() before the call. So it's in scope.
        thanks a lot

        Comment

        Working...