0

I'm trying to find max value of three integers without the use of if...else statement, when I was doing some search, I came across this lines of code.. Besides the abs function, all the other lines I could understand.Can anyone explain how the abs function in the code works?

#include <stdio.h> #include <stdlib.h> int main() { int x, y, z, result, max; printf("\nInput the first integer: "); scanf("%d", &x); printf("\nInput the second integer: "); scanf("%d", &y); printf("\nInput the third integer: "); scanf("%d", &z); result=(x+y+abs(x-y))/2; max=(result+z+abs(result-z))/2; printf("\nMaximum value of three integers: %d\n", max); return 0; } 
6
  • abs gives you the absolute value. Commented Aug 8, 2018 at 8:31
  • how it helps the code here? Commented Aug 8, 2018 at 8:32
  • Is this a puzzle, or a practical programming problem? Commented Aug 8, 2018 at 9:05
  • Please explain the reason for not using if. This is relevant for example in case the reason is a homework assignment (this kind of restrictions are typcically found in them). For that the answer needs to be tailored to the things you have already learned in the course, ideally the answer is tailored to the mechanism the teacher has in mind. A different angle: Be careful to really understand the solution and to be able to explain it based on previous textbook chapters. Commented Aug 8, 2018 at 9:09
  • I have just started learning programming and we still haven't reached the if.. Else statement.. That's the only reason I asked :) Commented Aug 8, 2018 at 11:01

3 Answers 3

4

(a+b+abs(a-b))/2

If a>b then this expression becomes (a+b+(a-b))/2 which is a.
If b>a then this expression becomes (a+b-(a-b))/2 which is b.

So this expression just gives you the maximum of two values.

In order to get the maximum of three values you just take the maximum of the maximum of the first two and the third value. max(a,b,c) = max(max(a,b),c)

Sign up to request clarification or add additional context in comments.

2 Comments

What happens if there are unsigned numbers?
@Michi For unsigned int and b>a, a-b is calculated modulo UINT_MAX+1. So you may get a number which can not be represented by int. abs takes as argument an int so the argument is implicitly casted. The cast of unsigned int to int however is implementation defined for value that can not be represented by int.
1

The man page for abs() states,

The abs() function computes the absolute value of the integer argument.

Also,

Returns the absolute value of the integer argument, of the appropriate integer type for the function.

So, if you know basic math then you'd understand that this function returns the absolute value of the expression.

Additionally, over a longer term stop following such strange (and stupid) exercises. These are only helpful for building up the logic, later, these are all useless.

As a pun, following the problem literally, I would have done it this way:

// So, `if..else` statements are banned, right! Take that! result = (a > b) ? a : b; max = (result > c) ? result : c; 

Remember, reading man pages for a function always helps.

Comments

1

Simply use the if statement. Writing C code without if statements is harmful and silly practice. If you try to minimize branches in the code, then that's another story, but that's not really a task for a beginner.


That being said, a solution to the problem is to implement a truth table and use boolean algebra with the > operator. Learning how to implement such a table is useful practice (as opposed to avoiding if statements), since boolean algebra has many uses in programming.

In this case, the truth table is (assuming no values are equal):

 x>y x>z y>z x largest 1 1 - y largest 0 - 1 z largest - 0 0 

Where - means "don't care".

This can be implemented as a 3D array in C, where each index is the result of the corresponding > comparison. That is:

array [2] [2] [2] // x>y x>z y>z 

This will however contain some cases that never can be true and the assumption is that no values are equal. You can implement it like this:

#include <stdio.h> int main() { const char* const largest [2] [2] [2] = // x>y x>z y>z { { // x<y { // x<z "z", // y<z "y" // y>z }, { // x>z "-", // y<z, not possible "y", // y>z } }, { // x>y { //x<z "z", //y<z "-" //y>z, not possible }, { //x>z "x", // y<z "x", // y>z }, }, }; const int use_cases [6][3] = { {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2}, {3,2,1} }; for(int i=0; i<6; i++) { int x = use_cases[i][0]; int y = use_cases[i][1]; int z = use_cases[i][2]; printf("x:%d, y:%d, z:%d, largest: %s\n", x, y, z, largest[x>y][x>z][y>z]); } return 0; } 

Output:

x:1, y:2, z:3, largest: z x:1, y:3, z:2, largest: y x:2, y:1, z:3, largest: z x:2, y:3, z:1, largest: y x:3, y:1, z:2, largest: x x:3, y:2, z:1, largest: x 

Comments