Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find maximum of three number in C without using conditional statement and ternary operator

I have to find maximum of three number provided by user but with some restrictions. Its not allowed to use any conditional statement. I tried using ternary operator like below.

max=(a>b?a:b)>c?(a>b?a:b):c 

But again its restricted to use ternary operator. Now I am not getting any idea how to do this?

like image 359
Microsoft Developer Avatar asked Aug 16 '11 05:08

Microsoft Developer


People also ask

How do you find the maximum element in a linked list without using conditional operators?

Approach 1 We can use (a > b) × b + (b > a) × a expression to find maximum number. This expression works as explained below. The following C program demonstrates it: C.

How do you find the maximum of three numbers?

First, the three numbers are defined. If num1 is greater than num2 and num3, then it is the maximum number. If num2 is greater than num1 and num3, it is the maximum number.


Video Answer


2 Answers

Taking advantage of short-circuiting in boolean expressions:

int max(int a, int b, int c) {      int m = a;      (m < b) && (m = b); //these are not conditional statements.      (m < c) && (m = c); //these are just boolean expressions.      return m; } 

Explanation:

In boolean AND operation such as x && y, y is evaluated if and only if x is true. If x is false, then y is not evaluated, because the whole expression would be false which can be deduced without even evaluating y. This is called short-circuiting when the value of a boolean expression can be deduced without evaluating all operands in it.

Apply this principle to the above code. Initially m is a. Now if (m < b) is true, then that means, b is greater than m (which is actually a), so the second subexpression (m = b) is evaluated and m is set to b. If however (m < b) is false, then second subexpression will not be evaluated and m will remain a (which is greater than b). In a similar way, second expression is evaluated (on the next line).

In short, you can read the expression (m < x) && (m = x) as follows : set m to x if and only if m is less than x i.e (m < x) is true. Hope this helps you understanding the code.

Test code:

int main() {         printf("%d\n", max(1,2,3));         printf("%d\n", max(2,3,1));         printf("%d\n", max(3,1,2));         return 0; } 

Output:

3 3 3 

Note the implementation of max gives warnings because evaluated expressions are not used:

prog.c:6: warning: value computed is not used
prog.c:7: warning: value computed is not used

To avoid these (harmless) warnings, you can implement max as:

int max(int a, int b, int c) {      int m = a;      (void)((m < b) && (m = b)); //these are not conditional statements.      (void)((m < c) && (m = c)); //these are just boolean expressions.      return m; } 

The trick is that now we're casting the boolean expressions to void, which causes suppression of the warnings:

like image 159
Nawaz Avatar answered Sep 22 '22 15:09

Nawaz


Assuming you are dealing with integers, how about:

#define max(x,y) (x ^ ((x ^ y) & -(x < y))) int max3(int x, int y, int z) {     return max(max(x,y),z); } 
like image 45
Foo Bah Avatar answered Sep 22 '22 15:09

Foo Bah