/*swap function by address,value and reference in c++, in each case the program require an user input*/
#include<iostream.h>
#include<stdlib.h> //For system("pause")
//using namespace std; uncomment if using MS Visual Studio
void swapv(int , int ); //For Swapping by value
void swapa(int *,int *); //For Swapping by address
void swapr(int &,int &); //For Swapping by reference
void main(void)
{
int a1,b1;
cout<<"Enter value of a1 and b1 for swapping by value: ";
cin>>a1>>b1;
swapv(a1,b1); //call by value.
cout<<"swap is ="<<"a1: "<<a1<<"\t"<<"b1: "<<b1<<endl;
int a2,b2;
cout<<"Enter value of a2 and b2 for swapping by address: ";
cin>>a2>>b2;
swapa(&a2,&b2); //call by address.
cout<<"swap is "<<"a2: "<<a2<<"\t" <<"b2: "<<b2<<endl;
int a3,b3;
cout<<"Enter the value of a3 and b3 for swapping by refrence: " ;
cin>>a3>>b3;
swapr(a3,b3); //call by reference.
cout<<"swap is " <<"a3: "<<a3<<"\t"<<"b3: "<< b3<<endl;
system("pause");
}
void swapv(int x,int y)
{
int t=x;
x=y;
y=t;
}
void swapa(int *x,int *y)
{
int t=*x;
*x=*y;
*y=t;
}
void swapr(int &x,int &y)
{
int t=x;
x=y;
y=t;
} //End of the code
ScreenShots:
#include<iostream.h>
#include<stdlib.h> //For system("pause")
//using namespace std; uncomment if using MS Visual Studio
void swapv(int , int ); //For Swapping by value
void swapa(int *,int *); //For Swapping by address
void swapr(int &,int &); //For Swapping by reference
void main(void)
{
int a1,b1;
cout<<"Enter value of a1 and b1 for swapping by value: ";
cin>>a1>>b1;
swapv(a1,b1); //call by value.
cout<<"swap is ="<<"a1: "<<a1<<"\t"<<"b1: "<<b1<<endl;
int a2,b2;
cout<<"Enter value of a2 and b2 for swapping by address: ";
cin>>a2>>b2;
swapa(&a2,&b2); //call by address.
cout<<"swap is "<<"a2: "<<a2<<"\t" <<"b2: "<<b2<<endl;
int a3,b3;
cout<<"Enter the value of a3 and b3 for swapping by refrence: " ;
cin>>a3>>b3;
swapr(a3,b3); //call by reference.
cout<<"swap is " <<"a3: "<<a3<<"\t"<<"b3: "<< b3<<endl;
system("pause");
}
void swapv(int x,int y)
{
int t=x;
x=y;
y=t;
}
void swapa(int *x,int *y)
{
int t=*x;
*x=*y;
*y=t;
}
void swapr(int &x,int &y)
{
int t=x;
x=y;
y=t;
} //End of the code
ScreenShots:
0 comments:
Post a Comment