Comparing two numbers

L

Lawrence Kirby

Sorry - I only top-post because of the fact that it avoids excessive
scrolling to read just the newest addition, especially in messages that
contain a lot of code. Just a personal preference, that's all.

....

If there is excessive scrolling then you haven't trimmed the article you
are replying to properly. That is important (perhaps the more serious
issue) whether you top or bottom post. And it is *not* the common case
that articles you are replying to contain lots of code.

Lawrence
 
P

pete

Chris said:
<fx:whistling/>

max = (a > b ? a : b);

no ifs, no conditional statements.

That is the most conditional that a statement can be.
It uses the conditional operator.
 
C

Chris Dollin

pete said:
That is the most conditional that a statement can be.
It uses the conditional operator.

Which is an *expression*, not a statement. The *statement*
is unconditionally an assignment.

Of course

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

avoids even that nitpick.
 
V

vsk

Hai All,

int main()
{
int a = 10;
int b = -12;
printf ("Bigger is : %d",find_big(a,b));
}
int find_big(int a, int b)
{
return a>b?a:b;
}

Thanks,
VSK
 
O

osmium

Chris Dollin said:
Which is an *expression*, not a statement. The *statement*
is unconditionally an assignment.

Of course

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

avoids even that nitpick.

K&R describe a conditional expression in the text on p. 51. But look at the
BNF in the same book. As soon as you put a semi-colon after it it becomes a
conditional *statement*. And without the semi-colon the compiler will barf.
 
P

purifier

Hi vsk,
but that uses the ternary operator right? That one, if i'm not mistaken is
just another form of if-else...
Please have a look at the solution given by Mr.Kishore and Mr.Chris
Dollin...

I got one idea... why not use the absolute value function..
(|a+b|+|a-b|/2)will give out the greater of the two numbers but only when
both the numbers are positive...
 
K

Kenneth Brody

purifier said:
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.

[... Remainder of homework problem deleted ...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

Chris Dollin

osmium said:
[snip]
Which is an *expression*, not a statement. The *statement*
is unconditionally an assignment.

Of course

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

avoids even that nitpick.

K&R describe a conditional expression in the text on p. 51. But look at
the
BNF in the same book. As soon as you put a semi-colon after it it becomes
a
conditional *statement*. And without the semi-colon the compiler will
barf.

(a) The semi-colon converts the *assignment* into a statement, not the
immediately-preceeding conditional expression. "Conditional statement"
usually refers to an if-then-[else], not to a statement that happens
to embed an expression that happens to use a conditional expression.

(b) consider
int max = a > b ? a : b;

There's no *statement* there at all (since it's a declaration), so
there isn't a *conditional* statement either.

<fx:stillWhistling/>
 
P

purifier

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

Now, what in the world do you mean by that...
[... 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...
 
K

Kenneth Brody

purifier said:
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:[email protected]>
 
C

Chris Dollin

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

Randy Howard

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

purifier

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

infobahn

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

Richard Bos

Keith Thompson said:
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
 
M

Michael Wojcik

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 (e-mail address removed)

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_
 
B

Ben Pfaff

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

Default User

purifier said:
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
 
R

Richard Bos

Ben Pfaff said:
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
 
C

Chris Dollin

Kenneth said:
"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
?
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top