-1

I was asked if I could write a code to find the biggest of the five numbers, rather than going with the conventional if...else (can't use switch or traversal - restricted) I decided to use compact version but IMO I found it verbose and confusing (tried it on three numbers first):

 #include<stdio.h>

 int main(void)
 {
     int a[3]={9,5,13};
     printf("Biggest No. is: %d\n",(a[0]>
                                         (a[1]>a[2]?a[1]:a[2])?a[0]:
                                                                    (a[1]>a[2]?a[1]:a[2])));
     return 0;
 }

Now doing this thing with this method for five numbers seems disastrous, is there any way it can be made simpler (by using this compact method of ?: only) as writing the expression which acts as a condition and writing it again as a result seems too much?

Or should I go with the conventional if ... else block?

Found a thing: To use macro for one expression and use that macro as condition and result for another comparison.

Ayak973
  • 468
  • 1
  • 6
  • 23
Gaurav
  • 1,570
  • 5
  • 17
  • I think you should make a loop, not a --simple-- `if else` block – TDk Sep 21 '18 at 08:23
  • Yup, use `if ... else`, or use a [sorting network](https://stackoverflow.com/questions/39673059), and take the last value. – user3386109 Sep 21 '18 at 08:24
  • 1
    In my opinion a code with if else block is much easier to read and maintain. – Mike Sep 21 '18 at 08:25
  • Since you can traverse (iterate) then make a recursive function max with the prototype: `int max(int *nums, int len)`. – Andreas Louv Sep 21 '18 at 08:32
  • @andlrc Can't try anything apart from `if-else`. – Gaurav Sep 21 '18 at 08:37
  • If you can use STL algorithm, take a look at the `qsort()` function provided by stdlib.h, provide a comparison function for integers, sort the data, and take the first (or last) element of the sorted array (ie: [SO ref](https://stackoverflow.com/a/1788048/5847906). The benefit is that you can use a bigger array without too much modifications of your code. – Ayak973 Sep 21 '18 at 08:37
  • 1
    If you can only use if else, then I would rather use that than a nested barely readable set of conditional operators – mdewit Sep 21 '18 at 08:40
  • @mdewit Mentioned the solution in question. – Gaurav Sep 21 '18 at 08:43
  • 4
    `?:` really shouldn't be used for complex cases like this, as you say it quickly turns unreadable. The sane solution is to use a loop to check every item, or alternatively sort the data. Everything else is nonsense and not worth spending time pondering about. – Lundin Sep 21 '18 at 08:45
  • A loop is the way to go. You do not sacrifice clarity using a loop. – Forrest Milner Feb 04 '22 at 17:52

2 Answers2

4

You can use nested macros to break down the logic.

#define MAX2(a,b)       (a)>(b)?(a):(b)
#define MAX3(a,b,c)     (a)>(b)? MAX2(a,c): MAX2(b,c)
#define MAX4(a,b,c,d)   (a)>(b)? MAX3(a,c,d): MAX3(b,c,d)
#define MAX5(a,b,c,d,e) (a)>(b)? MAX4(a,c,d,e): MAX4(b,c,d,e)
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
1

Try this:

int main(){

    int a[3]={9,5,13};

    int arr1[] = { a[0], a[1] };
    int arr2[] = { arr1[ a[0] < a[1] ], a[2] };

    printf("max is %d\n", arr2[ arr2[0] < a[2] ]);
}

I tried a few cases, seemed to work.

P.W
  • 26,289
  • 6
  • 39
  • 76