59.4k views
3 votes
Write a C++ program which prompts user to choose from the two alternative funtions specified below, each of which passes a random number between 1 and 4 and returns a random message. The two functions are: a) Function messageByValue that passes a random value between 1 and 4 and returns the message by value and b) Function messageByReference that passes a random value between 1 and 4 and returns the message by reference.

User Dan Fitch
by
6.4k points

1 Answer

4 votes

Answer:

#include <iostream> //header file

#include <stdlib.h>

#include <time.h>

using namespace std; // using namespace

string call_by_value(int num) // call by value

{

if(num==1)

return "hello sir!";

if(num==2)

return "what's going on?";

if(num==3)

return "howz you master?!";

if(num==4)

return "i m fine!";

}

string call_by_reference(int *num) //call by reference

{

if(*num==1)

return "hello sir!!";

if(*num==2)

return "what's going on?";

if(*num==3)

return "howz you master?";

if(*num==4)

return "i m fine";

}

int main() { //main function

int n,v,r;

string str;

srand (time(NULL));

cout<<"enter a value 1 and 2 for choice and -1 for exit : ";

cin>>n;

while(n!=-1)

{

if(n==1)

{

int num = (rand()%(4)) + 1;

str=call_by_value(num);

cout<<str<<endl;

}

else if(n==2)

{

int num = (rand()%(4)) + 1;

str=call_by_reference(&num);

cout<<str<<endl;

}

else

cout<<"invalid input !"<<endl;

cout<<"enter a value 1 and 2 for choice and -1 for exit :";

cin>>n;

}

return 0;

}

Explanation:

Firstly, we declare three header files, then use namespace

Then, create the module of call by value and then call by reference.

Then we define the main() function in which we call both the modules.

User Mybuddymichael
by
6.0k points