467,907 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

biggest of 3 numbers

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

May 4 '06 #1
  • viewed: 13442
Share:
30 Replies
vim wrote:
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 4 '06 #2
vim wrote:
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

May 4 '06 #3
Op Thu, 04 May 2006 13:48:13 +0200 schreef vim <vg*****@gmail.com>:
hi
How to find biggest of three numbers without comaprimg numbers


Without a comparison, the notion of 'biggest' is meaningless.
--
Gemaakt met Opera's revolutionaire e-mailprogramma:
http://www.opera.com/mail/
May 4 '06 #4
On 2006-05-04, vim <vg*****@gmail.com> wrote:
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.
May 4 '06 #5
vim wrote:
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 4 '06 #6
In article <sl**********************@random.yi.org> Jordan Abel <ra*******@gmail.com> writes:
On 2006-05-04, vim <vg*****@gmail.com> wrote:
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.


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;
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
May 4 '06 #7
[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);
}
}
---------------
Sundar

May 4 '06 #8
On 2006-05-04, Dik T. Winter <Di********@cwi.nl> wrote:
In article <sl**********************@random.yi.org> Jordan Abel <ra*******@gmail.com> writes:
> On 2006-05-04, vim <vg*****@gmail.com> wrote:
> > 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.


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;


More risk of overflow, though.
May 4 '06 #9
Dik T. Winter wrote:
In article <sl**********************@random.yi.org> Jordan Abel <ra*******@gmail.com> writes:
> On 2006-05-04, vim <vg*****@gmail.com> wrote:
> > 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.


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;


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. :)
May 4 '06 #10
Eric Sosman wrote:
vim wrote:
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.

Thanks. Now I have coffee sprayed all over my monitor and keyboard.
May 4 '06 #11
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);
}
}
---------------
Sundar


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.
--
Coos
May 4 '06 #12
vim wrote:
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.

May 4 '06 #13
Jordan Abel wrote:
On 2006-05-04, Dik T. Winter <Di********@cwi.nl> wrote:
In article <sl**********************@random.yi.org> Jordan Abel writes:
> On 2006-05-04, vim <vg*****@gmail.com> wrote:
> > How to find biggest of three numbers without comaprimg numbers
>
> 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;


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.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

May 4 '06 #14
Bart Rider wrote:
Dik T. Winter wrote:
In article <sl**********************@random.yi.org> Jordan Abel
<ra*******@gmail.com> writes:
> On 2006-05-04, vim <vg*****@gmail.com> wrote:
> > 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.

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;


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)

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
May 4 '06 #15
Julian V. Noble wrote:
Bart Rider wrote:
Dik T. Winter wrote:
In article <sl**********************@random.yi.org> Jordan Abel
<ra*******@gmail.com> writes:
> On 2006-05-04, vim <vg*****@gmail.com> wrote:
> > 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.

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;


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)


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.
--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
May 4 '06 #16
Jordan Abel wrote:
On 2006-05-04, vim <vg*****@gmail.com> wrote:
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.


Here's one I prepared earlier...

http://groups.google.com/group/comp....eee18e55ea156c

--
Peter

May 5 '06 #17
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"?
May 7 '06 #18
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!
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*/


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"?
May 7 '06 #19
On 2006-05-07, Peter "Shaggy" Haywood <ph******@alphalink.com.au.NO.SPAM> wrote:
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.


What's 32760 - (-32760)?

65520, right?

Or maybe it's -10.
May 7 '06 #20
Peter Shaggy Haywood schrieb:
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!
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*/


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.


Could you please explain that?
I would have expected a "round-down behaviour for negative
results" argument but I fail to see how 8/7 <= 1 in C.
Maybe it's just my not having had my caffeine fix yet...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 7 '06 #21
Jordan Abel schrieb:
On 2006-05-07, Peter "Shaggy" Haywood <ph******@alphalink.com.au.NO.SPAM> wrote:
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.

What's 32760 - (-32760)?

65520, right?

Or maybe it's -10.


ITYM -0x10 or -16

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 7 '06 #22
On 2006-05-07, Michael Mair <Mi**********@invalid.invalid> wrote:
Jordan Abel schrieb:
On 2006-05-07, Peter "Shaggy" Haywood <ph******@alphalink.com.au.NO.SPAM> wrote:
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.

What's 32760 - (-32760)?

65520, right?

Or maybe it's -10.


ITYM -0x10 or -16


Or it could be -10. or 42. But that is what I meant.
May 7 '06 #23
In article <4b*************@news.dfncis.de> Bart Rider <ca*******@killamail.com> writes:
Dik T. Winter wrote:

....
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. :)


Well, assuming those are ints:

unsigned int abs(int n)
{
unsigned int x = n;
unsigned int m;

m = (1 << (sizeof(int) - 1)) & x;
m = (m - (m >> 31)) | m;
return (x ^ m) + (1 & m);
}

Where is the comparison? But, whatever, abs is standard already in C89,
and I have no idea how it is implemented... But of course, there is
the probability of overflow lurking in that formula.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
May 8 '06 #24
ph******@alphalink.com.au.NO.SPAM (Peter "Shaggy" Haywood) wrote:
Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in
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))


0 is not a number these days?

Richard
May 8 '06 #25
Richard Bos wrote:
ph******@alphalink.com.au.NO.SPAM (Peter "Shaggy" Haywood) wrote:
Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in
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))


0 is not a number these days?


It doesn't matter which you return if the difference is 0, and the
suggested expression will return one of the numbers in that case. So I
don't see your point.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 8 '06 #26
In article <Iy********@cwi.nl> "Dik T. Winter" <Di********@cwi.nl> writes:
In article <4b*************@news.dfncis.de> Bart Rider <ca*******@killamail.com> writes:
> Dik T. Winter wrote: ...
> > 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. :)


Well, assuming those are ints:

unsigned int abs(int n)
{
unsigned int x = n;
unsigned int m;

m = (1 << (sizeof(int) - 1)) & x;
m = (m - (m >> 31)) | m;


Should have been
m = (m - (m >> (sizeof(int) - 1))) | m;
return (x ^ m) + (1 & m);
}

Where is the comparison? But, whatever, abs is standard already in C89,
and I have no idea how it is implemented... But of course, there is
the probability of overflow lurking in that formula.

--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
May 8 '06 #27
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Richard Bos wrote:
ph******@alphalink.com.au.NO.SPAM (Peter "Shaggy" Haywood) wrote:
Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in

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


0 is not a number these days?


It doesn't matter which you return if the difference is 0, and the
suggested expression will return one of the numbers in that case. So I
don't see your point.


The requirement was "without comparing numbers". (Ok, as written it was
"without comaprimg numbers", but...) Zero is a number. Comparing
something to zero falls under comparing numbers, and is therefore non
grata in this homework assignment.

Richard
May 8 '06 #28
Richard Bos wrote:
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Richard Bos wrote:
ph******@alphalink.com.au.NO.SPAM (Peter "Shaggy" Haywood) wrote:

Groovy hepcat vim was jivin' on 4 May 2006 04:48:13 -0700 in

> 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))
0 is not a number these days?

It doesn't matter which you return if the difference is 0, and the
suggested expression will return one of the numbers in that case. So I
don't see your point.


The requirement was "without comparing numbers". (Ok, as written it was
"without comaprimg numbers", but...) Zero is a number. Comparing
something to zero falls under comparing numbers, and is therefore non
grata in this homework assignment.


OK, I missed the point you were making. I agree with you.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 8 '06 #29
Groovy hepcat Michael Mair was jivin' on Sun, 07 May 2006 08:28:48
+0200 in comp.lang.c.
Re: biggest of 3 numbers's a cool scene! Dig it!
Peter Shaggy Haywood schrieb:
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!
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*/


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.


Could you please explain that?
I would have expected a "round-down behaviour for negative
results" argument but I fail to see how 8/7 <= 1 in C.
Maybe it's just my not having had my caffeine fix yet...


I don't know what I'm talking about. I really gotta get some sleep.
Well, that's my excuse anyhow, and I'm sticking to it. :)

--

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"?
May 10 '06 #30
vim
If u compare the result( subtraction of two numbers ) with 0.Then it it
violates the condition.
There should be no comparision

May 10 '06 #31

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.