-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap.cpp
More file actions
32 lines (24 loc) · 745 Bytes
/
Copy pathswap.cpp
File metadata and controls
32 lines (24 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* TODO: write a function which takes two integer parameters and swaps
* their contents. That is, if for example x==0,y==1, then after calling
* swap(x,y), it should be the case that x==1,y==0. */
#include <iostream>
using std::cin;
using std::cout;
void swap(int &a, int &b){
int temp = a;
a=b;
b=temp;
}
/* your answer goes here... */
int main()
{
int x=0, y=1;
cout << "x::"<< x<< " y::"<<y << std::endl;
swap(x,y);
cout << "x:: "<< x<< " y::"<<y << std::endl;
/* TODO: write a little test here to make sure your function works. */
return 0;
}
/* TODO: (bonus question) Can you rewrite your swap function so that it does
* not use a temporary variable? (Hint: use algebra/arithmetic...) */
// vim:foldlevel=2