(C++) In your main, create a bunch of character variables and use them to print out a word (similar to the last on-paper exercise you did with “google”). Now write a function that uses call by value, call by reference, and call by pointer (it doesn’t have to be in that order) to modify the characters to new characters. Back in main, print out your new word. Note that you can’t use the examples used in the last on-paper exercise.
// Code
#include <stdio.h>
#include <iostream>
using namespace std;
// call by reference
void callByRefer(char& a, char& b, char& c, char&
d, char& e)
{
a = 'a';
b = 'n';
c = 'g';
d = 'e';
e = 'l';
cout<<"value inside callByRefer:
"<<a<<b<<c<<d<<e << endl;
}
// call by pointer
void callByPointer(char* a, char* b, char* c, char* d, char*
e)
{
char p = *a;
*a = *b;
*b = p;
}
// call by reference
void callByValue(char a, char b, char c, char d, char e)
{
a = 'h';
b = 'u';
c = 'm';
d = 'a';
e = 'n';
cout<<"value inside callByValue:
"<<a<<b<<c<<d<<e << endl;
}
int main()
{
char d = 'd';
char e = 'e';
char v = 'v';
char i = 'i';
char l = 'l';
cout<<"Initial State :"<<endl;
cout <<d<<e<<v<<i<<l <<
endl;
cout<<"\ncall by reference"<<endl;
callByRefer(d,e,v,i,l);
cout <<d<<e<<v<<i<<l <<
endl;
cout<<"\ncall by value"<<endl;
callByValue(d,e,v,i,l);
cout <<d<<e<<v<<i<<l <<
endl;
cout<<"\ncall by pointer"<<endl;
callByPointer(&d,&e,&v,&i,&l);
cout <<d<<e<<v<<i<<l <<
endl;
return 0;
}
// Output:
/*
Initial State :
devil
call by reference
value inside callByRefer: angel
angel
call by value
value inside callByValue: human
angel
call by pointer
nagel
*/
Get Answers For Free
Most questions answered within 1 hours.