This is the question:
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 */
This is my code:
void minMax(int x, int y, int z, int* big, int* small)
{
if (x < y && x < z)
*small = x;
else if (y < x && y < z)
*small = y;
else if (z < x && z < y)
*small = z;
if (x > y && x > z)
*big = x;
else if (y > x && y > z)
*big = y;
else if (z > x && z > y)
*big = z;
}
This is the error I’m getting:
Your function did not change the value of small. Make sure you are dereferencing it in your function.
Not sure what’s wrong?
Thanks.
I see one immediate problem.
What do you think will happen when you pass the numbers
1,1and7?Perhaps you may want to consider the use of
<=and>=rather than just<and>.Since that error message looks nothing like any compiler error I’ve seen before (and the code is valid syntactically), I’d suggest the message is coming from a test harness which probably:
big/smallvalues to numbers other than those being passed in (eg,-9999).1,1,7).In addition, it’s not the most readable code in the world (no offence intended). If you can structure your code in such a way that its intent is clear from a glance (including comments where appropriate), you’ll have hordes of future programmers singing your praises and worshiping your name 🙂
Something like this shows the intent a little more clearly (IMNSHO) than lots of those
else ifconstructs: