-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapusingreference.cpp
More file actions
52 lines (48 loc) · 1.07 KB
/
Copy pathswapusingreference.cpp
File metadata and controls
52 lines (48 loc) · 1.07 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
void swap_v(int,int);
void swap_a(int*,int*);
void swap_r(int&,int&);
int main()
{
int a=10,b=20;
cout<<"Value of a and b before swapping by value->"<<endl;
cout<<"a-> "<<a<<endl<<"b-> "<<b<<endl;
swap_v(a,b);
cout<<"Value of a and b after swapping by value->"<<endl;
cout<<"a-> "<<a<<endl<<"b-> "<<b<<endl;
a=10,b=20;
cout<<"Value of a and b before swapping by address->"<<endl;
cout<<"a-> "<<a<<endl<<"b-> "<<b<<endl;
swap_a(&a,&b);
cout<<"Value of a and b after swapping by address->"<<endl;
cout<<"a-> "<<a<<endl<<"b-> "<<b<<endl;
a=10,b=20;
cout<<"Value of a and b before swapping by reference->"<<endl;
cout<<"a-> "<<a<<endl<<"b-> "<<b<<endl;
swap_r(a,b);
cout<<"Value of a and b after swapping by reference->"<<endl;
cout<<"a-> "<<a<<endl<<"b-> "<<b<<endl;
return 0;
}
void swap_v(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
void swap_a(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void swap_r(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}