-1

3 numbers are a,d and f and s is number which will store max of those 3.

The logic is:

s = a > d ? a > f ? a : f : d > f ? d : f;

Can someone explain what it exactly means?

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Anirudh
  • 1
  • 1
  • Oops, this is a C++ dupe, let me find a C one. – Sourav Ghosh Nov 25 '18 at 08:48
  • 1
    It mostly means that someone is more interested in being clever than being clear. It isn't a good way to do the job. `s = a; if (d > s) s = d; if (f > s) s = f;` is clearer. So is `s = (a > d) ? a : d; s = (f > s) ? f : s;`, though it is not as clear. – Jonathan Leffler Nov 25 '18 at 08:54
  • While the clearer way to express this would definitely not abuse the ternary operator so much, it's important not to belittle the importance of formatting. Even the horrid line your are asking about can be formatted to be something clearer https://ideone.com/HtmjVp – StoryTeller - Unslander Monica Nov 25 '18 at 09:08

3 Answers3

2

It's probably simplest to explain with a single-ternary MAX macro:

#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))

So, if you want to find the max of 3 numbers, you can string these macros together

MAX(MAX(X, Y), Z)
Paul
  • 370
  • 1
  • 6
0

If a > d and a > f, then max is a. If a > d but f >= a, then max is f. If a <= d and d > f, then max is d. If a <= d and d <= f then max is f.

eozd
  • 1,153
  • 1
  • 6
  • 15
0

So a ternary operator is like this. a ? b : c. If a is true it evaluates to b and if not then it evaluates to c. Apply that to your problem and if a is greater than both d and f then a is the new max. if not it checks if d > f then d is the new max. Otherwise f is the new max. Sorry for the long explanation. Hope this helps