/* Hello, can you please help me with this one. This is in an intro course so pl
ID: 3813092 • Letter: #
Question
/* Hello, can you please help me with this one. This is in an intro course so please keep it basic enough, also we use C language. thank you. Please read enire question */
Call by Value and call by Reference rite a C++ program to include two functions to swap two integer values which you get from the user. a- Write two version of the swap function, one version using call value and the other version use call by reference. Using call by reference you should not print anything inside the function. Test your program and output the swapped values for both versions. b- Write two overloaded functions to swap two double and two character values (use call by reference) So all together you need to write 4 functions. Declare the prototype of the functions and include proper commentsExplanation / Answer
Explanation:
In call by Value we are just passing the value of the variable. So in the called swap method these two values are changed. But the parameters in the called method and the actual variables passed are not the same. We are just passing the values of the variables, not any reference or address. So in the called method it creates two local variable with the same values and swap those. But these changes are not reflecting to the actual variables (the variables we want to change).
In Call By Reference , we are passing the reference or address of the variable(using &a and &c) , hence in the called method we are changing the values stored on that address directly( accessing the values stored using *a and *b). Hence the value of the original variables are getting changed.
For double and char also we can overload the methods that will take double and char arguments respectively. But the logic remains same.
code:
#include <iostream>
void swapByValue(int x, int y);
void swapByRef(int *x, int *y);
void swapByValue(double x, double y);
void swapByRef(double *x, double *y);
void swapByValue(char x, char y);
void swapByRef(char *x, char *y);
using namespace std;
int main () {
int a = 100;
int b = 200;
cout<<"a and b before swap :"<< a<<" ," << b<< endl;
swapByValue(a,b);
cout<<"a and b after swap by Call By Value :"<< a<<" ," << b<< endl;
swapByRef(&a, &b);
cout<<"a and b after swap by Call By Reference :"<< a<<" ," << b<< endl;
double da=10.5;
double db = 19.7;
cout<<"da and db before swap :"<< da<<" ," << db<< endl;
swapByValue(da,db);
cout<<"da and db after swap by Call By Value :"<< da<<" ," << db<< endl;
swapByRef(&da, &db);
cout<<"da and db after swap by Call By Reference :"<< da<<" ," << db<< endl;
char ca='a';
char cb='z';
cout<<"ca and cb before swap :"<< ca<<" ," << cb<< endl;
swapByValue(ca,cb);
cout<< "ca and cb after swap by Call By Value :"<< ca<<" ," << cb<< endl;
swapByRef(&ca, &cb);
cout<<"ca and cb after swap by Call By Reference :"<< ca<<" ," << cb<< endl;
return 0;
}
void swapByValue(int x,int y){
int temp;
temp=x;
x=y;
y=temp;
cout<< "swap value in local method(Call By Value) :"<< x <<" , "<< y<< endl;
}
void swapByRef(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
void swapByValue(double x,double y){
double temp;
temp=x;
x=y;
y=temp;
cout<< "swap value in local method(Call By Value) :"<< x <<" , "<< y<< endl;
}
void swapByValue(char x,char y){
char temp;
temp=x;
x=y;
y=temp;
cout<< "swap value in local method(Call By Value) :"<< x <<" , "<< y<< endl;
}
void swapByRef(double *x, double *y) {
double temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
void swapByRef(char *x, char *y) {
char temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.