Question on C++ code

closed account (Doj23TCk)
#include <stdio.h>
void swap(int *a, int *b, int *c)
{
int max,min,mid;
max = *a>*b ? *a:*b;
max = max>*c ? max:*c;

min = *a<*b ? *a:*b;
min = min<*c ? min:*c;

mid = *a + *b + *c - max - min;
*c = max;
*b = mid;
*a = min;
}

int main()
{
int a,b,c;
a=20;
b=23;
c=10;
printf("a = %d, b = %d, c = %d\n", a , b, c);
swap(&a,&b,&c);
printf("a = %d, b = %d, c = %d\n", a , b, c);
return 0;
}

In the code what does the "?" mean?
max = *a>*b ? *a:*b;
max = max>*c ? max:*c;

min = *a<*b ? *a:*b;
min = min<*c ? min:*c;
Please always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/



http://www.cplusplus.com/doc/tutorial/operators/


This is the conditional ternary operator - a short cut if then else

Good Luck !!
closed account (Doj23TCk)
so if you were going to write the part of
void swap(int *a, int *b, int *c)
{
int max,min,mid;
max = *a>*b ? *a:*b;
max = max>*c ? max:*c;

min = *a<*b ? *a:*b;
min = min<*c ? min:*c;

mid = *a + *b + *c - max - min;
*c = max;
*b = mid;
*a = min;
}
into an if and else statement how would you do that?
Hi,

Have a look in the tutorial, it shows examples.

Edit:
No, never mind - the question is probably answered in your other duplicate post

Please don't do this, it is annoying for those who spend their time to reply, that time is effectively wasted.

I am also less than enthused about your lack of willingness to use code tags.
Last edited on
Topic archived. No new replies allowed.