27.7k views
3 votes
Write the definition of a function named maxmin that is passed four int arguments. The function returns nothing but stores the larger of the first two arguments in the third argument it receives and the the smaller of the first two arguments in its fourth argument. So, if you invoke maxmin(3,7,x,y), upon return x will have the value 7 and y will have the value 3.

User Cory Loken
by
4.7k points

1 Answer

6 votes

Answer:

To get the values in x and y, we should use the pass -by -reference method. Where a reference to the actual variable is created in the called function. When we update the value inside the called function, it reflects in the calling function.

C++ code:

#include<iostream>

using namespace std;

void maxmin(int p, int q, int &r, int &s);

int main()

{

int k,l,x,y;

cout<<"Enter two integers for k and l: ";

cin>>k>>l;

maxmin(k, l, x, y);

cout<<"The maximum of two numbers is: "<<x<<endl;

cout<<"The minimum of two numbers is: "<<y<<endl;

}

void maxmin(int p, int q, int &r, int &s)

{ //r and s holding the address of x and y respectively.

if(p>q)

{

r=p;

s=q;

}

else

{

r=q;

s=p;

}

}

Step-by-step explanation:

The program shows how the method is working and the output is

Write the definition of a function named maxmin that is passed four int arguments-example-1
User Joselufo
by
4.7k points