469,620 Members | 1,857 Online

# query regarding swap function

1>
#include<iostream>
using namespace std;

Expand|Select|Wrap|Line Numbers
1. void swap(int& i, int& j)
2.  {
3.     int tmp = i;
4.     i = j;
5.     j = tmp;
6. cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;
7.
8. }
9. int main()
10. {
11. int x, y;
12. cout<<"enter the value of x and y"<<endl;
13. cin>>x>>y;
14. swap(x,y);
15. cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
16. }
2>
Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. using namespace std;
3. main()
4.     {
5.         int a,b;
6.         //void swap(int a,int b);
7.         cout<<"enter the value of a and b"<<endl;
8.         cin>> a>>b;
9.         swap (a,b);
10.         cout<<"The value after swap is"<<a<<":"<<b<<endl;
11.     }
12. void swap(int& x, int& y)
13.     {
14.         x=x+y;
15.         y=x-y;
16.         x=x-y;
17.         cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
18.     }
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
Mar 19 '07 #1
3 1398
chella
51
1>
#include<iostream>
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

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,

Expand|Select|Wrap|Line Numbers
1. #include <iostream.h>
2. main()
3. {
4. int a,b;
5. void swap(int a,int b);
6. cout<<"enter the value of a and b"<<endl;
7. cin>> a>>b;
8. swap (a,b);
9. cout<<"The value after swap is"<<a<<":"<<b<<endl;
10. }
11. void swap(int x, int y)
12. {
13. x=x+y;
14. y=x-y;
15. x=x-y;
16. cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
17. }
Regards,
Chella
Mar 19 '07 #2
arne
315 Expert 100+
1>
#include<iostream>
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.
Mar 19 '07 #3
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
Mar 19 '07 #4