biggest of 3 numbers

E

Eric Sosman

vim said:
hi
How to find biggest of three numbers without comaprimg numbers

return a>b?a>c?a:c:b>c?b:c;

No comaprisoms were haremd in the comuptatiom of this reslut.
 
R

Robert Gamble

vim said:
hi
How to find biggest of three numbers without comaprimg numbers

Have someone else compare them for you and tell you what the biggest
one is. Do you have a C language question?

Robert Gamble
 
F

Flash Gordon

vim said:
hi
How to find biggest of three numbers without comaprimg numbers

Ask someone else to compare them for you.

The comparison operators are there for a reason, do you have a good
reason for not using them? If your reason is your instructor told you
not to then that is a very good reason for us to *not* give you the
solution.
 
J

Jordan Abel

hi
How to find biggest of three numbers without comaprimg numbers

First, write an algorithm to find the biggest of two numbers without
comparing them.

Then

maxof3(a, b, c) {
return max(a,max(b,c));
}

This can be extended recursively to find the biggest of four, five, etc
numbers.
 
D

Dik T. Winter

>
> First, write an algorithm to find the biggest of two numbers without
> comparing them.
Yes.

> Then
>
> maxof3(a, b, c) {
> return max(a,max(b,c));
> }

Oh, well, simply:
max = (a + b + c * 2 + abs(a - b) + abs(a + b - c * 2 + abs(a - b))) / 4;
 
S

Sundar

[vim]
How to find biggest of three numbers without comaprimg numbers

I've tried to use the division technique without actually using any
comparision operators
Looks non-sense but doesn't compare numbers :)
---------------------code snippet---------
{
int ia;
int ib;
int ic;

if(ia/ib && ic/ib) /*Division of a smaller int by a greater int
results in 0*/
{
printf("\n%d is the smallest\n",ib);
}
else if(ib/ia && ic/ia)
{
printf("\n%d is the smallest\n",ia);
}
else
{
printf("\n%d is the smallest\n",ic);
}
}
 
B

Bart Rider

Dik said:
Oh, well, simply:
max = (a + b + c * 2 + abs(a - b) + abs(a + b - c * 2 + abs(a - b))) / 4;

nice peace of code, got me a while that it comes from:
max(a,b) = (a + b + abs(a - b))/2

but using this the comparison is simple hidden in the
abs statement, where a number is compared to zero. :)
 
V

void * clvrmnky()

Eric said:
return a>b?a>c?a:c:b>c?b:c;

No comaprisoms were haremd in the comuptatiom of this reslut.
Thanks. Now I have coffee sprayed all over my monitor and keyboard.
 
C

Coos Haak

Op 4 May 2006 06:07:26 -0700 schreef Sundar:
[vim]
How to find biggest of three numbers without comaprimg numbers

I've tried to use the division technique without actually using any
comparision operators
Looks non-sense but doesn't compare numbers :)
---------------------code snippet---------
{
int ia;
int ib;
int ic;

if(ia/ib && ic/ib) /*Division of a smaller int by a greater int
results in 0*/
{
printf("\n%d is the smallest\n",ib);
}
else if(ib/ia && ic/ia)
{
printf("\n%d is the smallest\n",ia);
}
else
{
printf("\n%d is the smallest\n",ic);
}
}

The Romans would not have problems with this, but since an Indian lady
discovered it about a millenium ago, we have the number zero!
There is a fair chance that ia, ib or ic has this value.
 
M

Martin Ambuhl

vim said:
hi
How to find biggest of three numbers without comaprimg numbers

Anything you might do to find the biggest of three numbers counts as
comparison.
 
W

websnarf

Jordan said:
More risk of overflow, though.

Indeed, so try this:

/* On an old CRAY: */
#define signedrightshift(v,s) ((v) / (1 << (s)))
/* On real platforms */
#define signedrightshift(v,s) ((v) >> (s))

static int maskgt (int a, int b) {
int v = ((a^b) & b) | ((~(a^b)) & (b - a));
return signedrightshift (v, sizeof (int) * CHAR_BIT - 1);
}

int max (int a, int b) {
int v = maskgt (a, b);
return (v & a) | (~v & b);
}

Disclaimer: I have no idea what this will do on 1s complement machines,
and more than likely neither do you.
 
J

Julian V. Noble

Bart said:
nice peace of code, got me a while that it comes from:
max(a,b) = (a + b + abs(a - b))/2

but using this the comparison is simple hidden in the
abs statement, where a number is compared to zero. :)

The ABS function can be done without comparisons: first by
forcing the sign positive; second (and agreed, it is slow!)
by
ABS(a) = SQRT(a*a)
 
J

Julian V. Noble

Julian said:
The ABS function can be done without comparisons: first by
forcing the sign positive; second (and agreed, it is slow!)
by
ABS(a) = SQRT(a*a)

And how, you might ask, do you force the sign positive? For IEEE
fp numbers it is easy: the leftmost (most significant) bit is
the sign, so for 32-bit fp numbers you AND with hex 7FFF and with
64-bit numbers, AND with 7FFFFFFF . I suppose there is something
similar one can do with 2's complement integers.
 
P

Peter Shaggy Haywood

Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in
comp.lang.c.
biggest of 3 numbers's a cool scene! Dig it!
How to find biggest of three numbers without comaprimg numbers

And your C question is...?
Find the bigger of three numbers by finding the bigger of two
numbers, one of which is itself the bigger of two numbers. Find the
bigger of two numbers by subtracting one from the other and
determining whether the result is greater than zero or not.

#define BIGGER_OF_TWO(x,y) ((x) - (y) > 0 ? (x) : (y))
#define BIGGER_OF_THREE(x,y,z) BIGGER_OF_TWO(BIGGER_OF_TWO(x, y), z)

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

Groovy hepcat Sundar was jivin' on 4 May 2006 06:07:26 -0700 in
comp.lang.c.
Re: biggest of 3 numbers's a cool scene! Dig it!
I've tried to use the division technique without actually using any
comparision operators
Looks non-sense but doesn't compare numbers :)
---------------------code snippet---------
{
int ia;
int ib;
int ic;

if(ia/ib && ic/ib) /*Division of a smaller int by a greater int
results in 0*/

So does division of an int by a smaller int large enough to cause a
value of magnitude less than 1, such as 8/7 or 32/17.
{
printf("\n%d is the smallest\n",ib);
}
else if(ib/ia && ic/ia)
{
printf("\n%d is the smallest\n",ia);
}
else
{
printf("\n%d is the smallest\n",ic);
}
}

Doomed to failure, I'd say.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
J

Jordan Abel

Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in
comp.lang.c.
biggest of 3 numbers's a cool scene! Dig it!


And your C question is...?
Find the bigger of three numbers by finding the bigger of two
numbers, one of which is itself the bigger of two numbers. Find the
bigger of two numbers by subtracting one from the other and
determining whether the result is greater than zero or not.

What's 32760 - (-32760)?

65520, right?

Or maybe it's -10.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top