Question

Write the definition of a function minMax that has five parameters. The first three parameters are...

Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19, big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */

I have

void minMax(int a, int b, int c, int*big, int*small){
   if (a > b && a > c){
   *big = a;
   }
   else if (b > a && b > c){
       *big = b;
   }
   else if (c > a && c > b){
       *big = c;
   }
   if (a < b && a < c ){
       *small = a;
   }
   else if (b < a && b < c){
       *small = b;
   }
   else if (c < a && c < b){
       *small = c;
   }
}

says my function did not change the value of small. Make sure you are dereferencing it in your function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The function calling statement for this function must be like this:

minMax(31, 5, 19, &big, &small);

We need to pass the address of variable 'big' and 'small' because in the function definition both of these parameters are of pointer type.

In the function definition, the code for small is correct as given below:

if (a < b && a < c ){
       *small = a;
   }
   else if (b < a && b < c){
       *small = b;
   }
   else if (c < a && c < b){
       *small = c;
   }

The above code will change the value at address passed by the function calling statement by the smallest value.

In the main() function, the smallest and the biggest value will be updated in the same variable and we have no need to dereference them by using the pointer concept because these are simple integer type variables.

The complete program including the main() program with output is given below:

#include <iostream>

using namespace std;

void minMax(int a, int b, int c, int*big, int*small)
{
if (a > b && a > c)
{
*big = a;
}
else if (b > a && b > c)
{
*big = b;
}
else if (c > a && c > b)
{
*big = c;
}
if (a < b && a < c )
{
*small = a;
}
else if (b < a && b < c)
{
*small = b;
}
else if (c < a && c < b)
{
*small = c;
}
}

int main()
{
int big, small;
minMax(31, 5, 19, &big, &small);
cout<<"small = "<<small<<endl<<"big = "<<big;

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write the definition of a function minMax that has five parameters. The first three parameters are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT