Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>
using namespace std;
int swap(int *, int *);
int main()
{
	int a, b;
	cout <<"Enter two number:\nA.";
	cin >> a;
	cout <<"B.";
	cin >> b;
	cout<<swap(&a, &b);
	return 0;
}
int swap(int x, int y)
{
	x = x + y;
	y = x - y;
	x = x - y; 
	return(x, y);

}


What I have tried:

#include<iostream>
using namespace std;
int swap(int *, int *);
int main()
{
int a, b;
cout <<"Enter two number:\nA.";
cin >> a;
cout <<"B.";
cin >> b;
cout<
Posted
Updated 10-Feb-20 2:07am

- The implementation of the swap method does not match its declaration. Arguments should be pointers to integers, so that variables can be swapped. The mismatch should prevent compilation anyway, and compiler should issue an explicit error.
C++
int swap(int *x, int *y) // ...

- The return type of the swap function (an integer) does not match what you are actually trying to return (some kind of tuple). That should also be signaled as an error by the compiler. If you define x and y parameters as pointers, you won't need to return anything, anyway.
 
Share this answer
 
You should use the reference syntax for your swap function:
C++
int swap(int &x, int &y)
Than no new instances of the variables are created on the call and you direct access the variables in the call.
 
Share this answer
 
Comments
CPallini 10-Feb-20 7:59am    
Indeed. My 5.
Note the return value could possibly be void.
KarstenK 10-Feb-20 11:41am    
You are right, but the result is used for output. ;-)
The corrected code with pointers should be below.

C++
#include <iostream>
using namespace std;
void swap(int*, int*);

int main()
{
    int a, b;
    cout << "Enter two number:\nA.";
    cin >> a;
    cout << "B.";
    cin >> b;
    swap(&a, &b);
    cout << "a,b is now " << a << "," << b << endl;
    return 0;
}

// xor swap
void swap(int* x, int* y)
{
    *x = (*x) + (*y);
    *y = (*x) - (*y);
    *x = (*x) - (*y);
}

The sample output is below.
Enter two number:
A.56
B.89
a,b is now 89,56
 
Share this answer
 
v3
Comments
Stefan_Lang 10-Feb-20 7:15am    
Why did you post that solution? There is no indication another swap() implementation is needed, and the actual problem was aleady addressed.
Shao Voon Wong 10-Feb-20 7:49am    
Indeed!
CPallini 10-Feb-20 8:55am    
Have my 5.
KarstenK 10-Feb-20 11:42am    
The code is buggy, when some overflow conditions occur.
To elaborate KarstenK solution (that is, by the way, the cleanest one), since you apparently don't master pointer (as well reference) syntax:

C
#include<iostream>
using namespace std;
void swap(int &, int &);

int main()
{
  int a, b;
  cout <<"Enter two number:\nA.";
  cin >> a;
  cout <<"B.";
  cin >> b;

  swap( a, b);
  cout << "After swap call\n";

  cout << "A = " << a << "\nB = " << b << "\n";
}
void swap(int & x, int & y)
{
  x = x + y;
  y = x - y;
  x = x - y;
}
 
Share this answer
 
Comments
Shao Voon Wong 10-Feb-20 8:36am    
5! Reference has a more natural syntax compared to pointer.
CPallini 10-Feb-20 8:54am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900