It may seem overkill, but the first thing to do is to sort them.
#define swap(x, y) do {int tmp; tmp = x; x = y; y = tmp; } while (0)
int getMiddleNumber(int a, int b, int c)
{
if (b < a) swap(b, a);
if (c < b) swap(c, b);
if (b < a) swap(b, a);
return b;
}
Then all you have to do, is use b - it is in the middle.
(this code pinched from
here[
^])
"Using macros like that (while convenient) is generally frowned on. You can't debug a macro, so you have to hope your code is right."
Agreed, but it's been quite a while since I used straight C and I couldn't remember where I put my QuickC disks... :laugh:
void swap(int *x, int* y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int getMiddleNumber(int a, int b, int c)
{
if (b < a) swap(&b, &a);
if (c < b) swap(&c, &b);
if (b < a) swap(&b, &a);
return b;
}