I think you need something like this:
// Make sure x is smaller than y
if(x>y)
{
swap(&x, &y);
}
// Make sure x is smaller than z
if(x>z)
{
swap(&x, &z);
}
// Now x is smaller than both y and z
// Make sure y is smaller than z
if(y>z)
{
swap(&y, &z);
}
So the full program would look:
#include <stdio.h>
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
int main(void)
{
int x,y,z;
x=10;
y=-1;
z=5;
printf("x=%d y=%d z=%d\n",x,y,z);
// Make sure x is smaller than y
if(x>y)
{
swap(&x, &y);
}
// Make sure x is smaller than z
if(x>z)
{
swap(&x, &z);
}
// Now x is smaller than both y and z
// Make sure y is smaller than z
if(y>z)
{
swap(&y, &z);
}
printf("x=%d y=%d z=%d",x,y,z);
return 0;
}
The output is:
x=10 y=-1 z=5
x=-1 y=5 z=10
3
solved (Function) Swaping two numbers in c