Velocity Reviews > Greatest of three numbers

# Greatest of three numbers

Amar Kumar Dubedy
Guest
Posts: n/a

 07-01-2007
How to find the greatest of three numbers without using any comparison
operator or ternary operator??

Flash Gordon
Guest
Posts: n/a

 07-01-2007
Amar Kumar Dubedy wrote, On 01/07/07 07:36:
> How to find the greatest of three numbers without using any comparison
> operator or ternary operator??

Put them all in a ring, throw weapons in, and wait to see which one
comes out alive.

If it is homework, do it yourself, if you are doing some stupid quiz, do
it yourself. People *might* help if you post an attempt here, but why
should we do it for you?
--
Flash Gordon

Malcolm McLean
Guest
Posts: n/a

 07-01-2007

"Amar Kumar Dubedy" <> wrote in message
news: ups.com...
> How to find the greatest of three numbers without using any comparison
> operator or ternary operator??
>

If the numbers are positive integers

a/b is zero if b is greater than a.

C allows an implict comparison to zero, so

if(a/b) is equivalent to if( b > a)

and you can build a first stab around that.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Richard Heathfield
Guest
Posts: n/a

 07-01-2007
Amar Kumar Dubedy said:

> How to find the greatest of three numbers without using any comparison
> operator or ternary operator??

Ensure that one of the numbers is LDBL_MAX, and simply return LDBL_MAX
from your solving function. That way, you don't need any operators of
any kind at all.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Army1987
Guest
Posts: n/a

 07-01-2007

"Amar Kumar Dubedy" <> ha scritto nel messaggio news: ups.com...
> How to find the greatest of three numbers without using any comparison
> operator or ternary operator??

#include <stdlib.h>
....
if (a - b == abs(a - b))...

(but beware of overflows...)

HTH.

Richard Tobin
Guest
Posts: n/a

 07-01-2007
In article <vJJhi.125182\$> ,
Chris Dollin <> wrote:
>Army1987 wrote:

>>> How to find the greatest of three numbers without using any comparison
>>> operator or ternary operator??

>> #include <stdlib.h>
>> ...
>> if (a - b == abs(a - b))...

>In what way is `==` not a comparison operator?

Perhaps Army was thinking of "relational operator", which C defines
as not including the equality operators.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Chris Dollin
Guest
Posts: n/a

 07-01-2007
Army1987 wrote:

> "Amar Kumar Dubedy" <> ha scritto nel messaggio
> news: ups.com...
>> How to find the greatest of three numbers without using any comparison
>> operator or ternary operator??

>
> #include <stdlib.h>
> ...
> if (a - b == abs(a - b))...

In what way is `==` not a comparison operator?

--
Archetype Hedgehog
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Richard Heathfield
Guest
Posts: n/a

 07-01-2007
Army1987 said:

>
> "Amar Kumar Dubedy" <> ha scritto nel messaggio
> news: ups.com...
>> How to find the greatest of three numbers without using any
>> comparison operator or ternary operator??

>
> #include <stdlib.h>
> ...
> if (a - b == abs(a - b))...

== can be regarded as a comparison operator.

> (but beware of overflows...)

That's another problem.

The Right Thing here is to use comparison operators - specifically the
'greater-than' operator:

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

Therefore, the question is mistaken.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Richard Tobin
Guest
Posts: n/a

 07-01-2007
In article <. com>,
Amar Kumar Dubedy <> wrote:

>How to find the greatest of three numbers without using any comparison
>operator or ternary operator??

Do you really need to determine which of them it is, or would it be
sufficient to return a number equal to the greatest one?

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Army1987
Guest
Posts: n/a

 07-01-2007

"Richard Tobin" <> ha scritto nel messaggio news:f67osm\$1o3d\$...
> In article <vJJhi.125182\$> ,
> Chris Dollin <> wrote:
>>Army1987 wrote:

>
>>>> How to find the greatest of three numbers without using any comparison
>>>> operator or ternary operator??

>
>>> #include <stdlib.h>
>>> ...
>>> if (a - b == abs(a - b))...

>
>>In what way is `==` not a comparison operator?

>
> Perhaps Army was thinking of "relational operator", which C defines
> as not including the equality operators.

if (!(a - b - abs(a - b))
(But beware of disasters...)