# Beginner Programming Help

• 02-03-2003
Beginner921
Beginner Programming Help
I'm trying to write a program that gives me the largest of three inputted integers. I've been trying to make different functions with arguments and comparisons and none of them seem to be consistently giving me the largest integer. Some of the things i've been trying are

if(a > b > c)
max = a;
if (b > a > c)
max = b;
if (c > a > b)
max = c;

and another one

max = a;
if (max < b)
max = b;
if (max < c)
max = c;
return max;

and another one

if (x > y > z)
return x;
else if (y > x > z)
return y;
else
return z;

I've tried all of these and whenever I put in any 3 integers, it usually always gives me the first or last entered integer back, not the largest one. If anybody can help, it would be a really big help. Thanks in advance.
• 02-03-2003
Polymorphic OOP
You can't string logical comparisons together like that.

To understand why, you have to understand exactly what those operators are doing.

Every time you do something like

a < b

the result is 1 if the expression is true and 0 if the expression is false.

If you, in turn, try to do

a < b < c

then a < b will evaluate to either a 1 or a 0 (true or false), and then that 1 or 0 will be compared with c.

What you really want to do is use the logical AND operator which is &&

a < b && b < c // a is less than b AND b is less than c

however, in this case, there is a better way of accomplishing what you want:

Code:

```if( a > b )     if( a > c )         max = a;     else         max = c; else     if( b > c )         max = b;     else         max = c;```
or, if you want to learn the ?: operator, it can also be done like this

max = ( a > b ? ( a > c ? a : c ) : ( b > c ? b : c ) );
• 02-03-2003
Beginner921
it works now!! thanks so much for the help and thanks for the C lesson! :)