Greatest of three numbers

  • Thread starter Amar Kumar Dubedy
  • Start date
A

Amar Kumar Dubedy

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

Flash Gordon

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

Malcolm McLean

Amar Kumar Dubedy said:
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.
 
R

Richard Heathfield

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.
 
A

Army1987

Amar Kumar Dubedy said:
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.
 
R

Richard Heathfield

Army1987 said:
#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.
 
R

Richard Tobin

Amar Kumar Dubedy said:
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
 
A

Army1987

Richard Tobin said:
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...)
 
C

cr88192

Amar Kumar Dubedy said:
How to find the greatest of three numbers without using any comparison
operator or ternary operator??

well, just a guess, wandering by this group, and people have already tried
answering...

just a guess really (assumes 3 integers > 0).

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }
int max(int a, int b)
{ return(norz(a, b)+norz(b, a)); }
int max2(int a, int b, int c)
{ return(max(max(a, b), max(b, c))); }
 
M

Malcolm McLean

Richard Heathfield said:
Harald van D?k said:
Richard said:
cr88192 said: [un-snip]
just a guess really (assumes 3 integers > 0).

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

If b is 0, then you don't have three integers > 0.

In which case we have demonstrated that the assumption is false. :)

Note that this assumption was not present in the OP.
If we allow that the number must be positive integers then it turns into a
trival problem of C syntax, which I don't mind helping the OP with even if
it is homework.
Make them arbitrary floating point numbers and it is a lot more difficult.
Specify that the operation must not overflow machine precision for any legal
input and I think it might be impossible.
 
F

Flash Gordon

Malcolm McLean wrote, On 01/07/07 17:38:
Richard Heathfield said:
Harald van D?k said:
Richard Heathfield wrote:
cr88192 said:
[un-snip]
just a guess really (assumes 3 integers > 0).

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

If b is 0, then you don't have three integers > 0.

In which case we have demonstrated that the assumption is false. :)

Note that this assumption was not present in the OP.
If we allow that the number must be positive integers then it turns into
a trival problem of C syntax, which I don't mind helping the OP with
even if it is homework.
Make them arbitrary floating point numbers and it is a lot more
difficult. Specify that the operation must not overflow machine
precision for any legal input and I think it might be impossible.

Since the OP said "numbers" there is no reason to assume only +ve or an
integer type. There was also no license given to loose precision (so no
floating point arithmetic) or have it fail on some inputs. All of which
shows why it was a stupid question. However, there is an easy answer, it
just relies on the user of the program answering the questions the
program asks correctly.
 
R

regis

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

if all three are unsigned...

#define N (CHAR_BIT * sizeof (unsigned) - 1)

int /* returns the carry flag of the sum a+b */
carry_flag (unsigned a, unsigned b)
{
int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}};
unsigned s= a + b;
return cf [a >> N] [b >> N] [s >> N];
}

int /* returns 1 iff a < b */
below (unsigned a, unsigned b)
{
return carry_flag (a + ~b, 1)
| carry_flag (a , ~b)

}

unsigned /* returns the greatest of 3 arguments */
greatest (unsigned a, unsigned b, unsigned c)
{
unsigned res [2][2][2];
res [0][0][0]= a; /* or any, all equal */
res [0][0][1]= res [0][1][1]= a;
res [1][0][0]= res [1][0][1]= b;
res [0][1][0]= res [1][1][0]= c;
res [1][1][1]= 0; /* should not happen */
return res [below (a,b)] [below (b,c)] [below (c,a)];
}
 
C

cr88192

Richard Heathfield said:
cr88192 said:


Fails when b is 0.

yes, but note, I said, 3 integers > 0.

that it fails when one of them is 0, is pretty damn obvious, which is why I
wrote this as an assumption...

now, by adding and subtracting 1 in the right places, it is possible to make
this valid for 3 integers >= 0...

oh well, the main reason I was here was seing if this was a good place to
talk about compiler writing and design (namely, I wrote a C compiler), ...,
but it does not look like it, which is why I have not posted on this subject
here...
 
H

Harald van =?UTF-8?B?RMSzaw==?=

cr88192 said:
oh well, the main reason I was here was seing if this was a good place to
talk about compiler writing and design (namely, I wrote a C compiler),
..., but it does not look like it, which is why I have not posted on this
subject here...

<OT, I guess>
You're right, this isn't the best group for that, but you might like the
comp.compilers newsgroup for it.
</OT>
 
P

Peter Nilsson

Amar Kumar Dubedy said:
How to find the greatest of three numbers without using
any comparison operator or ternary operator??

For integers, just extend the case for 2 numbers...
[Posted Jan '05]

The following uses strtol() to convert arguments on the
command line, and printf() to output the maximum.
Otherwise, it doesn't use any conditional, logical,
equality or relational operators, nor does it use any
selection or iteration statements.

% type max2.c
typedef unsigned long q1;int printf(const char*,...);long
strtol(const char*,char**,int);q1 q2(q1 q3,q1 q4,q1 q5,q1
q6){return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-
q3))+1))-1)+q6;}q1 q7(long q8){return q2(q8,0,q2(q8/2,0,1
,0),0);}q1 q9(long q8){return q2(q8,-1,q2(q8/2,-1,1,q2(q8
/2,0,1,0) ),0);}q1 q10(long);q1 q11(long q8){return 0;}q1
q12(long q8){return 1;}q1 q13(long q8){return q10(q8/2);}
q1 q14(long q8){q1(*q15[])(long)={q12,q13};return q15[q2(
q9(q8),1,0,1)](q8);}q1 q10(long q8){q1(*q15[])(long)={q11
,q14};return q15[ q2(q7(q8),1,0 ,1)](q8);}q1 q17(long q3,
long q4){return q2(q10( q3),1,q2(q10(q4),1,q10(q3-q4),1),
q2(q10(q4),1 ,0,q10(q3-q4 )));}long q18(long q3,long q4){
return q3;}long q19(long q3,long q4){return q4;}long q20(
long q3,long q4){long(*q15[])(long,long)={q19,q18};return
q15[q2(q17(q3,q4),1,0,1)](q3,q4);}long q21(const char*q2)
{return strtol(q2,0,10);}int q22(char **q23){printf("%ld"
"\n",q20(q21(q23[1]),q21(q23[2])));return 0;}int q24(char
**q23){return 0;}int main(int q25,char**q23){int(*q15[])(
char**)={q24,q22};return q15[q2(q25,3,1,0)](q23);}

% gcc -ansi -pedantic max2.c -o max2.exe

% max2 -5 42
42
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top