I thought ternary operators were some kind of eye-candy.
This really reminds me of some sort of eye-cancer (no offense).
Better solution (with any count of variables)
If I’m right you want to extract the second greatest number from a
, b
, c
and d
and save it in k
.
Try this:
int compare_int(const void* a, const void* b)
{
int int_a = *((int*)a);
int int_b = *((int*)b);
if (int_a == int_b)
return 0;
else if ( int_a < int_b )
return -1;
else
return 1;
}
int main()
{
int value[4];
// set values in any order:
value[0] = 20; //second greatest
value[1] = -36;
value[2] = 294;
value[3] = 3;
qsort(value, 4, sizeof(int), compare_int);
// now the array is sorted by value.
// tho the second greatest number is in value[2].
printf("%d\n", value[2]);
return 0;
}
Reformatting your code
Nevertheless.. I reformatted your code a bit:
k = (((a > b && a > c && a > d) ? (((b > c && b > d) ? b : ((c > b && c > d) ? c : d)))) : ((b > a && b > c && b > d) ? (((a > c && a > d) ? a : ((c > a && c > d) ? c : d)))) : (((c > a && c > b && c > d) ? (((a > b && a > d) ? a : ((b > a && b > d) ? b : d)))) : ((d > a && d > b && d > c) ? (((a > b && a > c) ? a : ((b > c && b > a) ? b : c))))));
Is the same as:
k = (
// misplaced (
(a > b && a > c && a > d) ?
(
(
(b > c && b > d) ? b :
(
(c > b && c > d) ? c : d
)
)
)
// misplaced )
:
// misplaced (
(b > a && b > c && b > d) ?
(
(
(a > c && a > d) ? a :
(
(c > a && c > d) ? c : d
)
)
)
// misplaced )
:
(
// misplaced (
(c > a && c > b && c > d) ?
(
(
(a > b && a > d) ? a :
(
(b > a && b > d) ? b : d
)
)
)
// misplaced )
:
(
(d > a && d > b && d > c) ?
(
(
(a > b && a > c) ? a :
(
(b > c && b > a) ? b : c
)
)
)
// missing
// :
// (
// expression
// )
)
)
);
Easy huh?
Now you can see that you are even misplaced some(
‘s and)
‘s and also several:
‘s
1
solved 2nd greatest among 4 numbers…iam getting the error as “expected : before )”