By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,664 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,664 IT Pros & Developers. It's quick & easy.

Comparing two numbers

P: n/a
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy? huh
here's the catch
do not use 'if' or any conditional statements
if u want it to be a little more tougher you can use the if but this time
no relational operators or any of the predefined functions....

Can someone please help me solve the problem....

Nov 14 '05
Share this Question
Share on Google+
89 Replies


P: n/a
Kenneth Brody wrote:
purifier wrote:

The problem is to write a program in 'C' to find the greatest of 2 given
numbers...


By definition, you cannot find the "greatest" of two things.


That's a pretty useless definition, then. Me, I can find the greatest
of *one* thing. It's the greatest of *no* things that's tricky.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #51

P: n/a
In article <pa****************************@netactive.co.uk> ,
lk****@netactive.co.uk says...
On Mon, 17 Jan 2005 20:25:01 +0000, Mike Wahler wrote:
One answer to that can be fun and recreational programming. And by
exploring the language with unusual contraints you can sometimes learn
something new and surprising about the language and what it can do.


Absolutely. I enjoy reading these threads, because when the "gang-
tackling by geeks" part of it dies out, invariably someone posts a
novel solution to a problem that I had not thought of before.

--
Randy Howard (2reply remove FOOBAR)
"For some reason most people seem to be born without the part
of the brain that understands pointers." -- Joel Spolsky
Nov 14 '05 #52

P: n/a
purifier wrote:
By definition, you cannot find the "greatest" of >two things.


Now, what in the world do you mean by that...


"Greatest" requires at least three items. For two, you want the "greater".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 14 '05 #53

P: n/a
Mr.Kenneth Brody... Why being so formal... When I was able to convey the
meaning to people...where is the need for you to point out that
grammatical mistake? Anyway...cool....

Nov 14 '05 #54

P: n/a
purifier wrote:
By definition, you cannot find the "greatest" of two things.


Now, what in the world do you mean by that...


He means that "greatest" traditionally refers to the - um - greatest
of a group, rather than just two. For two, the word "greater" is used.
Nov 14 '05 #55

P: n/a
Keith Thompson <ks***@mib.org> wrote:
Incidentally, I can think of a reasonable reason to want to find the
greater of two numbers without using certain constructs. Conditional
branches, on some systems, can cause performance problems by messing
with pipelining, instruction caches, and other off-topic stuff. If
performance is sufficiently important, it just might be worthwhile to
micro-optimize such an operation.


Whatever happened to Jackson's Laws? If the compiler cannot perform such
optimisations, they most probably do not work anyway.

Richard
Nov 14 '05 #56

P: n/a
"Chris Dollin" <ke**@hpl.hp.com> wrote in message
news:cs**********@malatesta.hpl.hp.com...
Kenneth Brody wrote:
purifier wrote:

The problem is to write a program in 'C' to find the greatest of 2 given numbers...


By definition, you cannot find the "greatest" of two things.


That's a pretty useless definition, then. Me, I can find the greatest
of *one* thing.


I certainly hope so. My wife tells me I'm her
favorite husband. :-)

-Mike
Nov 14 '05 #57

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Whatever happened to Jackson's Laws? If the compiler cannot perform such
optimisations, they most probably do not work anyway.


Google's top hit for Jackson's Law is "Loss of mental functions
due to disease retraces in reverse order its evolutionary
development." If that's not what you mean, perhaps you should
elaborate. Actually, even if that's what you mean I'd appreciate
elaboration.
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield
Nov 14 '05 #58

P: n/a

In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin <ke**@hpl.hp.com> writes:

max = (a > b) * a + (a <= b) * b;


If we restrict a and b to positive integers, we can get rid of those
relational-operator crutches, using integer division and the !!-trick:

max = ((!!(a/b) * a) + (!!(b/a) * b)) / (!!(a/b) + !!(b/a));

(The final division handles the case where a == b; then the sum is
a+b rather than a+0 or 0+b, so we have to divide by two in that
case. The denominator evaluates to 2 if a == b, and 1 otherwise.)

We could extend this to handle zero by adding 1 to a and b first,
then subtracting 1 from the answer:

max = ((!!((a+1)/(b+1)) * (a+1)) + (!!((b+1)/(a+1)) * (b+1))) /
(!!((a+1)/(b+1)) + !!((b+1)/(a+1)))
- 1;

Handling cases where a and b have different signs looks a bit more
complicated, so I'm not going to bother.

--
Michael Wojcik mi************@microfocus.com

Unlikely predition o' the day:
Eventually, every programmer will have to write a Java or distributed
object program.
-- Orfali and Harkey, _Client / Server Programming with Java and CORBA_
Nov 14 '05 #59

P: n/a
purifier wrote:
Mr.Kenneth Brody... Why being so formal... When I was able to convey the meaning to people...where is the need for you to point out that
grammatical mistake? Anyway...cool....


You really need to start quoting properly. If that web site you use to
post doesn't allow it, don't use it. I'd suggest a real news service if
possible, groups.google as a last resort (what I'm using). Your ISP may
provide a newsfeed, if not the server at http://news.individual.net is
free with registration.

Brian

Nov 14 '05 #60

P: n/a
Lawrence Kirby <lk****@netactive.co.uk> writes:
On Mon, 17 Jan 2005 20:25:01 +0000, Mike Wahler wrote:
"purifier" <cn******@gmail.com> wrote in message
news:5c******************************@localhost.ta lkaboutprogramming.com...
Enough of those insults... I'm sorry for bothering you guys... I
guess this forum is filled with all time intellectuals... I
already explained that I'm an Electronics Engineer just beginning
to learn C... Now why would I lie about my profession?


A much better question is why would you want to avoid
using a language feature specifically designed to do
what you asked about (comparing values). It also has
a name which lends to code readability (which is important):
'if'.


One answer to that can be fun and recreational programming. And by
exploring the language with unusual contraints you can sometimes learn
something new and surprising about the language and what it can do.


Agreed. Learning to program in C involves learning to do the hard
stuff as well as the easy stuff, understanding the more obscure and
difficult features of the language. The trick for an educator is
figuring out how to assign problems that force the student to do the
hard stuff. One way to do this is to assign a problem that can be
solved easily, but forcing the student to solve it with one hand tied
behind his back (by restricting the features that can be used). The
result is something that's entirely unrealistic in real-world terms,
but a more realistic problem that actually needs the difficult
features might be beyond the students' ability.

The risk is that the students might assume that there's some virtue in
doing things the hard way when it's not really necessary.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #61

P: n/a
purifier wrote:
By definition, you cannot find the "greatest" of two things.
Now, what in the world do you mean by that...


By definition, Ali is the greatest. You probably meant the greater.
[... Remainder of homework problem deleted ...]


Now, don't come back to the beginning Mr.Kenneth Brody... I
already made it clear that this one is not a homework problem...
If you have any suggestions, then reply,otherwise, fine...please
don't reply... No one is forcing you to reply over here...


Since all potential homework problems are immeditately rejected or
argued over for a week or so, the mechanism ensures that any
homework answers arrive too late for submission, and thus do not
exacerbate the flood of incompetent programmers.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #62

P: n/a
Ben Pfaff <bl*@cs.stanford.edu> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Whatever happened to Jackson's Laws? If the compiler cannot perform such
optimisations, they most probably do not work anyway.


Google's top hit for Jackson's Law is "Loss of mental functions
due to disease retraces in reverse order its evolutionary
development." If that's not what you mean, perhaps you should
elaborate. Actually, even if that's what you mean I'd appreciate
elaboration.


Jackson's Laws of Optimisation:

1. Don't do it.
2. (For experts only) Don't do it yet.

Someone around here had them in his .sig, at one time.

Richard
Nov 14 '05 #63

P: n/a
Michael Wojcik wrote:
If we restrict a and b to positive integers, ...

<snip>

max = ((!!((a+1)/(b+1)) * (a+1)) + (!!((b+1)/(a+1)) * (b+1))) /
(!!((a+1)/(b+1)) + !!((b+1)/(a+1)))
- 1;


If a and b are unsigned int, and either one is UINT_MAX, you have
division by zero.

If a and b are int, and either one is INT_MAX, you have overflow.
--
Peter

Nov 14 '05 #64

P: n/a
Kenneth Brody wrote:
purifier wrote:
>By definition, you cannot find the "greatest" of >two things.


Now, what in the world do you mean by that...


"Greatest" requires at least three items. For two, you want the
"greater".


You might accept that as a grammatical constraint; I don't.

As side-curiosity, would you accept, using "your" definition

"find the greatest of a, b, and c"
where a = 42, b = 17, c = 42
?

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #65

P: n/a
Chris Dollin wrote:

Kenneth Brody wrote:
purifier wrote:

>By definition, you cannot find the "greatest" of >two things.

Now, what in the world do you mean by that...


"Greatest" requires at least three items. For two, you want the
"greater".


You might accept that as a grammatical constraint; I don't.

As side-curiosity, would you accept, using "your" definition

"find the greatest of a, b, and c"
where a = 42, b = 17, c = 42
?


I would.
I learned English the same way as purifer did.

--
pete
Nov 14 '05 #66

P: n/a
pete wrote:
Chris Dollin wrote:

Kenneth Brody wrote:
> purifier wrote:
>>
>> >By definition, you cannot find the "greatest" of >two things.
>>
>> Now, what in the world do you mean by that...
>
> "Greatest" requires at least three items. For two, you want the
> "greater".


You might accept that as a grammatical constraint; I don't.

As side-curiosity, would you accept, using "your" definition

"find the greatest of a, b, and c"
where a = 42, b = 17, c = 42
?


I would.
I learned English the same way as purifer did.


There's only two items in this example.

I *said* it was a side-curiosity.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #67

P: n/a
Chris Dollin wrote:

pete wrote:
Chris Dollin wrote:

Kenneth Brody wrote:

> purifier wrote:
>>
>> >By definition, you cannot find the "greatest" of >two things.
>>
>> Now, what in the world do you mean by that...
>
> "Greatest" requires at least three items. For two, you want the
> "greater".

You might accept that as a grammatical constraint; I don't.

As side-curiosity, would you accept, using "your" definition

"find the greatest of a, b, and c"
where a = 42, b = 17, c = 42
?
I would.
I learned English the same way as purifer did.


There's only two items in this example.


I counted a, b and c as three items.
I *said* it was a side-curiosity.


--
pete
Nov 14 '05 #68

P: n/a
pete wrote:
Chris Dollin wrote:

pete wrote:
> Chris Dollin wrote:
>>
>> Kenneth Brody wrote:
>>
>> > purifier wrote:
>> >>
>> >> >By definition, you cannot find the "greatest" of >two things.
>> >>
>> >> Now, what in the world do you mean by that...
>> >
>> > "Greatest" requires at least three items. For two, you want the
>> > "greater".
>>
>> You might accept that as a grammatical constraint; I don't.
>>
>> As side-curiosity, would you accept, using "your" definition
>>
>> "find the greatest of a, b, and c"
>> where a = 42, b = 17, c = 42
>> ?
>
> I would.
> I learned English the same way as purifer did.


There's only two items in this example.


I counted a, b and c as three items.


Those are the *names* of the items. The items are 42 and 17. Otherwise
the answer would likely be "c" ...
I *said* it was a side-curiosity.


Ditto.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #69

P: n/a
Kenneth Brody <ke******@spamcop.net> wrote:
purifier wrote:
By definition, you cannot find the "greatest" of >two things.


Now, what in the world do you mean by that...


"Greatest" requires at least three items. For two, you want the "greater".


Several dictionaries disagree with you. The OED, for example, calls
"greatest" the superlative tense of "great"; and under "superlative" it
says that superlatives are used for the highest in degree of a _number_
of choices. Not "number greater than two"; "number".

Richard
Nov 14 '05 #70

P: n/a

In article <11**********************@c13g2000cwb.googlegroups .com>, "Peter Nilsson" <ai***@acay.com.au> writes:
Michael Wojcik wrote:
If we restrict a and b to positive integers, ...

max = ((!!((a+1)/(b+1)) * (a+1)) + (!!((b+1)/(a+1)) * (b+1))) /
(!!((a+1)/(b+1)) + !!((b+1)/(a+1)))
- 1;


If a and b are unsigned int, and either one is UINT_MAX, you have
division by zero.

If a and b are int, and either one is INT_MAX, you have overflow.


Yes, in one draft of that post I had a qualification about remaining
below the maximum value of the type of a and b, but apparently I
removed it during editing. It's so obvious I hardly think it's worth
pointing out anyway; we could make the same criticism of most code
snippets posted to c.l.c. Clearly the expression above fails to work
for values *greater* than the maximum of the type, since we can't
even put them into a and b; should I have mentioned that?

--
Michael Wojcik mi************@microfocus.com

Even though there may be some misguided critics of what we're trying
to do, I think we're on the wrong path. -- Reagan
Nov 14 '05 #71

P: n/a

In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin <ke**@hpl.hp.com> writes:
Kenneth Brody wrote:
purifier wrote:

>By definition, you cannot find the "greatest" of >two things.

Now, what in the world do you mean by that...


"Greatest" requires at least three items. For two, you want the
"greater".


You might accept that as a grammatical constraint; I don't.


It's a question of usage, not grammar. No matter of grammar is under
debate. English grammar tells us that "greatest" is the superlative
form of the adjective "great", and that "greater" is the comparative
form. Whether the superlative is properly used for a set of fewer
than three items is a matter of usage.

Preferring the comparative when only two items are being compared is
certainly longstanding and widespread among those who advocate
specific usages in English.

--
Michael Wojcik mi************@microfocus.com

Thus, the black lie, issuing from his base throat, becomes a boomerang to
his hand, and he is hoist by his own petard, and finds himself a marked man.
-- attributed to a "small-town newspaper editor in Wisconsin"
Nov 14 '05 #72

P: n/a
There are tons of asshole on programmng forum so beware.
They forget what it was like to be new I guess.

l00pyl

Nov 14 '05 #73

P: n/a
Michael Wojcik wrote:

In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin
<ke**@hpl.hp.com> writes:
Kenneth Brody wrote:
> purifier wrote:
>>
>> >By definition, you cannot find the "greatest" of >two things.
>>
>> Now, what in the world do you mean by that...
>
> "Greatest" requires at least three items. For two, you want the
> "greater".
You might accept that as a grammatical constraint; I don't.


It's a question of usage, not grammar. No matter of grammar is under
debate. English grammar tells us that "greatest" is the superlative
form of the adjective "great", and that "greater" is the comparative
form. Whether the superlative is properly used for a set of fewer
than three items is a matter of usage.


Well, I'm not convinced, but that's partly because my notion of
grammar for natural language is as a description of usage, so I'm
not making the distinction in the same way you are.
Preferring the comparative when only two items are being compared is
certainly longstanding and widespread among those who advocate
specific usages in English.


I think my position here is that I'd use "greatest" and "greater" without
organised preference, for any non-empty collections of items, but of
course I might be empirically wrong and in any case my usage has been
heavily influenced by mathematics.

*I* am happy to say "all my helicopters are heavily armed" and "our
biggest cat is on the bed". (But I might not be so happy to say that
to a policeman.)

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #74

P: n/a
mw*****@newsguy.com (Michael Wojcik) wrote:
Preferring the comparative when only two items are being compared is
certainly longstanding and widespread among those who advocate
specific usages in English.


Preferring it, yes. Demanding it of others, no. "The greater of the two"
is correct. "The greatest of the two" is not wrong.

Richard
Nov 14 '05 #75

P: n/a
On Wed, 19 Jan 2005 14:35:41 +0000, Chris Dollin wrote:

....
I counted a, b and c as three items.
Those are the *names* of the items.


Correct, the items themselves are the three objects/variables these
represent.
The items are 42 and 17.
Those are properties of the items. It just happens that two items share
the same property. That does not make them the same item. I have 3 coins
in my hand which I will label x, y and z. The fact that x and z are of the
same denomination does not suddenly mean that I only have 2 coins in my
hand.
Otherwise
the answer would likely be "c" ...


When you use a comparative or superlative form you are comparing a common
property, usually as defined by the base adjective. The assumption here
was the property in question "great-ness" was the value, but that may be a
wrong assumption. If so the fault is in the question for not being clear.

Lawrence
Nov 14 '05 #76

P: n/a
Richard Bos wrote:
mw*****@newsguy.com (Michael Wojcik) wrote:
Preferring the comparative when only two items are being compared
is certainly longstanding and widespread among those who advocate
specific usages in English.


Preferring it, yes. Demanding it of others, no. "The greater of the
two" is correct. "The greatest of the two" is not wrong.


Go ahead. You have my permission (if not blessing) to cavalierly
use 'greatest' as you wish. Do not misconstrue this as a license
to use the broken google usenet interface, nor to top-post, nor to
fail to snip, nor to 'edit' quoted material, nor to use
'doodspeak', nor to attach binaries. :-) I am sure you have been
eagerly awaiting this permission.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #77

P: n/a

In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin <ke**@hpl.hp.com> writes:
Michael Wojcik wrote:
In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin
<ke**@hpl.hp.com> writes:

You might accept that as a grammatical constraint; I don't.
It's a question of usage, not grammar. No matter of grammar is under
debate. English grammar tells us that "greatest" is the superlative
form of the adjective "great", and that "greater" is the comparative
form. Whether the superlative is properly used for a set of fewer
than three items is a matter of usage.


Well, I'm not convinced, but that's partly because my notion of
grammar for natural language is as a description of usage,


Exactly. English grammar is descriptive (specifically, of syntax),
not proscriptive.
so I'm not making the distinction in the same way you are.
Clearly, but that doesn't follow from grammar being a description
of usage. If grammar describes usage, then an error of grammar
can only be an error of description, not an "error" in the actual
use of language in the statement in question.
I think my position here is that I'd use "greatest" and "greater" without
organised preference, for any non-empty collections of items,
I wouldn't try to stop you. (I actually do possess a secret doomsday
weapon with which I could enforce my language preferences on everyone,
but I don't wield it for trifles.) I prefer the comparative when
dealing with two items, but the use of the superlative in that context
is pretty widespread. The comparative for more than two ("he was the
wittier of the three"?) is, I think, less common, but understanding
probably won't be dramatically impaired by it.
but of course I might be empirically wrong
In a matter of usage? I'm not sure empiricism applies.
and in any case my usage has been heavily influenced by mathematics.
Where I agree that distinguishing the dual case would be a pointless
inconvenience.
*I* am happy to say "all my helicopters are heavily armed" and "our
biggest cat is on the bed". (But I might not be so happy to say that
to a policeman.)


Those cat police are a caution, aren't they?

--
Michael Wojcik mi************@microfocus.com

They had forgathered enough of course in all the various times; they had
again and again, since that first night at the theatre, been face to face
over their question; but they had never been so alone together as they were
actually alone - their talk hadn't yet been so supremely for themselves.
-- Henry James
Nov 14 '05 #78

P: n/a
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41****************@news.individual.net...
mw*****@newsguy.com (Michael Wojcik) wrote:
Preferring the comparative when only two items are being compared is
certainly longstanding and widespread among those who advocate
specific usages in English.


Preferring it, yes. Demanding it of others, no. "The greater of the two"
is correct. "The greatest of the two" is not wrong.


The greatest of 2 is 2.

Now someone please write a C expression which
proves this. :-)

-Mike
Nov 14 '05 #79

P: n/a
In article <8a******************************@localhost.talkab outprogramming.com>,
purifier <cn******@gmail.com> wrote:
It's a real sad thing to know, Sir... I didn't expect that my post would
make you think that it was my homework... I'm an Electronics Engineer, but
i'm interested a lot in programming... One of my friends asked me this
question in a mail. I just pasted that mail here... For your information i
even said "Can you please help me solve the problem" Why would I say huh
and all kinds of stuff and then ask people to solve the problem? I could
have directly given the problem...
Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
if possible, please give me some ideas...
The following is not correct. But there is an idea inside.
Though it is not correct, it works.

x = -(a<b);
y = -!x;
max = (b&x)|(y&a);

(You may want to take a course DEC Alpha assembler programming.)
Thank You


--

Groetjes Albert
--
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
One man-hour to invent,
One man-week to implement,
One lawyer-year to patent.
Nov 14 '05 #80

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
mw*****@newsguy.com (Michael Wojcik) wrote:
Preferring the comparative when only two items are being compared is
certainly longstanding and widespread among those who advocate
specific usages in English.


Preferring it, yes. Demanding it of others, no. "The greater of the two"
is correct. "The greatest of the two" is not wrong.


I think the former example is the best usage.
--
<g,d&r>
Nov 14 '05 #81

P: n/a

"Albert van der Horst" <al****@spenarnc.xs4all.nl> wrote in message
news:ia********@spenarnc.xs4all.nl...
In article <8a******************************@localhost.talkab outprogramming.com>, purifier <cn******@gmail.com> wrote:
It's a real sad thing to know, Sir... I didn't expect that my post would
make you think that it was my homework... I'm an Electronics Engineer, buti'm interested a lot in programming... One of my friends asked me this
question in a mail. I just pasted that mail here... For your information ieven said "Can you please help me solve the problem" Why would I say huh
and all kinds of stuff and then ask people to solve the problem? I could
have directly given the problem...
Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleastif possible, please give me some ideas...


The following is not correct. But there is an idea inside.
Though it is not correct, it works.

x = -(a<b);
y = -!x;
max = (b&x)|(y&a);

(You may want to take a course DEC Alpha assembler programming.)


Since I think enough time has passed :-), and I haven't
seen the 'array method' posted yet, here it is (if the
inputs are equal, returns the first.):

int greater(int n1, int n2)
{
int a[2] = {n1, n2};
return a[n1 < n2];
}

-Mike
Nov 14 '05 #82

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
<SNIP>

Incidentally, I can think of a reasonable reason to want to find the
greater of two numbers without using certain constructs. Conditional
branches, on some systems, can cause performance problems by messing
with pipelining, instruction caches, and other off-topic stuff. If
performance is sufficiently important, it just might be worthwhile to
micro-optimize such an operation. If you can state the restrictions
in portable C terms (even if the motivation for the restrictions is
off-topic), you might have an interesting question.
Like this?
------------
int test(int a, int b)
{
return a<b? b: a;
}
int test2(int a, int b)
{
int c;

if (a<b)
c=b;
else
c=a;

return c;
}
-----------------------
This is what the gnu c-compiler delivers on DEC alpha
(-O3). There is no jump in sight. The code is the same
for both functions.

For the uninitiated: I have commented the code. .file 1 "aap.c"
.version "01.01"
.set noat
gcc2_compiled.:
__gnu_compiled_c:
..text
.align 3
.globl test
.ent test
test:
$test..ng:
.frame $30,0,$26,0
.prologue 0
cmple $17,$16,$1 r1 = (a<=b);
bis $16,$16,$0 r0 = b; (literally r0 = b|b; )
cmoveq $1,$17,$0 if (r1) r0 = a;
ret $31,($26),1
.end test
.align 3
.globl test2
.ent test2
test2:
$test2..ng:
.frame $30,0,$26,0
.prologue 0
cmple $16,$17,$1
bis $17,$17,$0
cmoveq $1,$16,$0
ret $31,($26),1
.end test2
.ident "GCC: (GNU) egcs-2.90.27 980315 (egcs-1.0.2 release)"
-------------

Or is this what you mean by off topic?
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


--
--
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
One man-hour to invent,
One man-week to implement,
One lawyer-year to patent.
Nov 14 '05 #83

P: n/a
Albert van der Horst <al****@spenarnc.xs4all.nl> writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
<SNIP>

Incidentally, I can think of a reasonable reason to want to find the
greater of two numbers without using certain constructs. Conditional
branches, on some systems, can cause performance problems by messing
with pipelining, instruction caches, and other off-topic stuff. If
performance is sufficiently important, it just might be worthwhile to
micro-optimize such an operation. If you can state the restrictions
in portable C terms (even if the motivation for the restrictions is
off-topic), you might have an interesting question.


Like this?

[snip example showing generated code with no jumps]

Good point; most compilers do a pretty good job of optimizing jumps
away if it makes sense to do so for a given processor (and, just as
important, not doing so if it doesn't make sense).

I'll amend my above statement to say that this kind of
micro-optimization might make sense given the above circumstances *if*
you don't have a compiler that can do the optimization for you. (That
may not be an issue these days.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #84

P: n/a
Michael Wojcik wrote:

In article <cs**********@malatesta.hpl.hp.com>, Chris Dollin
<ke**@hpl.hp.com> writes:

I think my position here is that I'd use "greatest" and "greater" without
organised preference, for any non-empty collections of items,


I wouldn't try to stop you. (I actually do possess a secret doomsday
weapon with which I could enforce my language preferences on everyone,
but I don't wield it for trifles.) I prefer the comparative when
dealing with two items, but the use of the superlative in that context
is pretty widespread. The comparative for more than two ("he was the
wittier of the three"?) is, I think, less common, but understanding
probably won't be dramatically impaired by it.
but of course I might be empirically wrong


In a matter of usage? I'm not sure empiricism applies.


Sorry, I mean that I might be empirically wrong about my usage patterns.
(Self-observation is tricky, even more subject to selection effects than
observation of others.)
*I* am happy to say "all my helicopters are heavily armed" and "our
biggest cat is on the bed". (But I might not be so happy to say that
to a policeman.)


Those cat police are a caution, aren't they?


There appearance can be arresting, also.
--
Chris "electric hedgehog" Dollin
Nov 14 '05 #85

P: n/a
"purifier" <cn******@gmail.com> wrote in message
news:5c******************************@localhost.ta lkaboutprogramming.com...
Enough of those insults... I'm sorry for bothering you guys... I guess this forum is filled with all time intellectuals... I already explained that I'm an Electronics Engineer just beginning to learn C... Now why would I lie about my profession? Come on...there is no reason to... Ok...if it was one post insulting me then it's ok but it seems that everyone who posts a reply just insults in some or the other way... It is a pity that you guys are so rude to beginners like me... Now, how do you want me to prove that i'm not asking you to do my homework...? Do you want me to sign a bond or what? The only reason i came here was thinking that this is an open forum... and thought i could ask for some help... Now, how do i show you my work if I didn't even understand the problem... Anyway, I won't bother you guys
again... You can talk all the hi fi stuff...

And Admin Sir, Let me finally congratulate you in having such a wonderful forum... All the above things were my personal opinions because i was hurt a lot after posting here but your board is definitely helpful for many
experienced programmers to interact... Keep it up Sir...

purifier


douche-bag!

--
Mabden
Nov 14 '05 #86

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:5f****************@newsread1.news.pas.earthli nk.net...

The greatest of 2 is 2.


The greatest of 2 is 3, for relatively large values of 2...

--
Mabden
Nov 14 '05 #87

P: n/a
"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
Since all potential homework problems are immeditately rejected or
argued over for a week or so, the mechanism ensures that any
homework answers arrive too late for submission, and thus do not
exacerbate the flood of incompetent programmers.


Except those who research the archives!

....which has never happened, or they would have found last year's crop
of "Solve cold fishions without using fish or ions" problems'
"solutions".

Ah, reasearch. What ever happened to Rea, anyway?

--
Mabden
Nov 14 '05 #88

P: n/a
"Chris Dollin" <ke**@hpl.hp.com> wrote in message
news:cs**********@malatesta.hpl.hp.com...
Kenneth Brody wrote:
purifier wrote:

The problem is to write a program in 'C' to find the greatest of 2 given numbers...


By definition, you cannot find the "greatest" of two things.


That's a pretty useless definition, then. Me, I can find the greatest
of *one* thing. It's the greatest of *no* things that's tricky.


What is the greatest thing about pissing on your shoes?

The only thing I can think of is me doing it...

--
Mabden
Nov 14 '05 #89

P: n/a
On Tue, 18 Jan 2005 15:58:57 +0000, Chris Dollin <ke**@hpl.hp.com>
wrote:
Kenneth Brody wrote:

<snip>
By definition, you cannot find the "greatest" of two things.


That's a pretty useless definition, then. Me, I can find the greatest
of *one* thing. It's the greatest of *no* things that's tricky.


This is the difference between people and mathematicians. :-)

In Fortran MAXVAL of an empty (sub)array produces the identity, -huge,
and MINVAL respectively +huge. ISTR APL did essentially the same, but
that memory is faint and I have no reference at hand to check.

Now, the greatest (or whatever) of a *negative number* of things ...

And in those situations where one person says to another "I have a
little nothing for you" it definitely isn't no thing and usually is a
thing nowhere near least by the relevant metric(s). <G>

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #90

89 Replies

This discussion thread is closed

Replies have been disabled for this discussion.