Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Greatest of three numbers

Reply
Thread Tools

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??

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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.


 
Reply With Quote
 
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.
 
Reply With Quote
 
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/

 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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.
 
Reply With Quote
 
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...)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Three Mobile --> Skype on three (Non-three [Symbian - Nokia] handsets) Harry Stottle UK VOIP 0 01-05-2010 08:59 AM
greatest of two numbers aarklon@gmail.com C Programming 37 12-29-2007 11:29 PM
OT: Greatest headline ever Briscobar MCSE 29 12-22-2005 09:11 PM
THE GREATEST VITAMIN IN THE WORLD Susan Richmeier Computer Support 3 04-30-2004 05:00 AM
Bill Gates' Greatest Hits Harrison Computer Support 2 02-20-2004 11:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57