Comparing two numbers

M

Mike Wahler

Bonj said:
People always like to think that the help they're giving is of commercial
value. If they think otherwise, they'll jump on the 'homework' bandwagon.
Happens every time.

No, I disagree. Look again at the original question. It's
so obviously a contrived question, and its solution is never
needed in a C program (except for perhaps as a diversionary
'puzzle', but OP didn't label it as such). So I for one,
took it as a general 'problem solving' challenge which might
be a useful learning device (in the context of problem solving,
but imo not for learning C).

Also see Keith T.'s reply, which imo is the best answer to
OP so far.

-Mike
 
M

Mike Wahler

À̱â¿î wrote:

Please don't top post in comp.lang.c.
Hi~ all

i think that this is the problem about getting sign bit of numbers...
this is very easy... see follow code lines...
you can simplfy multiple lines to a line...

int main(int argc, char *argv[])
{
int a = 2.0;
int b = -23.1;

This is not _wrong_ wrong, just wrong. ;)
int lagestNumber = 0;
int signBitCount = (sizeof(int) * 8) - 1;

A byte may have more than 8 bits. An int may have padding bits.
So, your shift count can be wrong in two ways.
int signBitMask = 0x1 << signBitCount;

Potential undefined behaviour. Even if the behaviour is defined for
some implementations, it may still be the wrong bit mask.
int a_minus_b = a - b;
int b_minus_a = b - a;

Potential overflow for arbitrary a and b.
int signBitA = 0;
int signBitB = 0;

signBitA = ((unsigned)a_minus_b & signBitMask) >> signBitCount;
signBitB = ((unsigned)b_minus_a & signBitMask) >> signBitCount;

An unsigned int need not use the corresponding int sign bit as a
value bit. So your cast may produce zero if a_minus_b is INT_MIN.
x = (signBitA ^ 0x1) * a + (signBitB ^ 0x1) * b;
printf("lagestNumber = %d\n", lagestNumber);
lagestNumber is 0 here.
return 0;
}

Also, À̱â¿î:

Your errors aside, please don't try to provide solutions
here to folks who've given no evidence of their own efforts
first. This issue may or may not apply to the OP, but here
we like to try to protect ourselves from cheating students
getting a foothold in the ranks of the professionals[*], making
our work unnecessarily more difficult.


[*] by passing a CS course without really learning.

-Mike
 
P

purifier

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
 
M

Mike Wahler

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

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

No need to prove that. But please prove that what you're asking
has any practical use in a C program.
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...


What you seem to be missing is that their is NO problem.
You want to compare two values, use 'if'.
Anyway, I won't bother you guys
again... You can talk all the hi fi stuff...

OK, bye then.
And Admin Sir, Let me finally congratulate you in having such a wonderful
forum...

This is a public, unmoderated newsgroup. There is no 'admin'.
All the above things were my personal opinions because i was hurt
a lot after posting here

IMO you need to develop a thicker skin.
but your board is definitely helpful for many
experienced programmers to interact...

It's also been proven to be very helpful to beginners.
But it's not a 'gimme some code' or 'gimme the answer'
forum. We *help* people with what *they* are doing,
we don't simply do it *for* them. I (and others) did
give you some useful hints.


If you really want to figure out the answer to the proposed
(imo ridiculous) question, study some C books, and *give it
a try*. Then I'm sure you'll be pleased with the help you
get.

-Mike
 
D

Default User

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


Someone needs a Usenet 101 primer. There's no Admin. There's no
moderator. There's no board. There's no "here" here. There's just a
bunch of heres here and there, connected up into a loose confederation.
And we like it that way.

I see that you post from talkaboutprogramming.com, that's fooled you
into thinking it's a web forum with a small community of participants.
In fact, that's just a portal into a far-flung distributed network
encompassing thousands of servers around the world.

Very few, if any other posters are coming in through your service. Most
ISPs have their own news servers (although some do not). Another
popular way to access usenet is through Google, as I am doing until
such time as they fix our newsfeed.



Brian
 
M

Mike Wahler

Default User said:
ISPs have their own news servers (although some do not).

Another
popular way to access usenet is through Google, as I am doing until
such time as they fix our newsfeed.

My condolences. :)

-Mike
 
D

Default User

Mike said:
My condolences. :)


It's certainly made me more sympathetic towards those struggling with
using grouple. They sure made a lot of bad decisions. It's hard to
believe that the developers working on the interface could have
actually used usenet much at all.

There's a guy who developed a web-based news service that works much
better than google's. http://www.recgroups.com/?force=1#

It's not perfect, but sure is a lot better interface than google.


Brian
 
M

Mike Wahler

Default User said:
It's certainly made me more sympathetic towards those struggling with
using grouple. They sure made a lot of bad decisions. It's hard to
believe that the developers working on the interface could have
actually used usenet much at all.

They probably didn't. They probably take the view of Usenet
as "just another data source". And perhaps they're stripping
whitespace in a misguided attempt to conserve bandwitdth.
But this is just speculation of course.

-Mike

There's a guy who developed a web-based news service that works much
better than google's. http://www.recgroups.com/?force=1#

It's not perfect, but sure is a lot better interface than google.

There are also public news servers that work with 'real'
news clients (but I realize some organizations limit access
to 'unknown' sources in the interest of security).

-Mike
 
D

Default User

Mike said:
There are also public news servers that work with 'real'
news clients (but I realize some organizations limit access
to 'unknown' sources in the interest of security).


That's our case. The firewall won't allow use of non-approved NNTP
servers. I have an account with the German server which I'd be happy to
use all the time, but no.

Google and ones like I mentioned work because they're actually HTTP. If
I knew of a decent news operation with a good web interface that wasn't
too expensive, I would be interested.



Brian
 
B

Bonj

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.
I see the point about not answering homework, and I agree. But I am quite
sceptical of the notion that as many people as there are that chastise
homework-posters are actually making a valiant attempt to change the world
by implicitly teaching the youth of today to use a fishing rod rather than
give them a fish.
 
B

Bonj

He didn't actually say what you can and can't use - which more importantly
means that it's a pretty pointless exercise if this isn't specified. But he
didn't say you *can't* use inline assembly. To me, "write a program in C"
means code in a file with a *.c extension, compiled with a C compiler. It
would be possible to demonstrate inline assembly within those parameters -
like I say, nothing else is specified, nothing like "must be *portable, ISO*
C".
 
M

Mike Wahler

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

I've done it so many times before, I'll let someone
else try to explain the rationale behind not topposting.
I see the point about not answering homework, and I agree. But I am quite
sceptical of the notion that as many people as there are that chastise
homework-posters are actually making a valiant attempt to change the world
by implicitly teaching the youth of today to use a fishing rod rather than
give them a fish.

Cheaters who manage to pass CS curricula and get hired
pose a very real danger to businesses, valid professionals,
and yes, sometimes human life.

Refusing to help them cheat is a defensive action (at least for me).

-Mike
 
M

Mike Wahler

Bonj said:
He didn't actually say what you can and can't use - which more importantly
means that it's a pretty pointless exercise if this isn't specified. But he
didn't say you *can't* use inline assembly. To me, "write a program in C"

In the context of this newsgroup "C" means ISO standard C.
means code in a file with a *.c extension,

The name or 'extension' (if any) of a file has nothing
to do with whether it contains C source code. Many
implementations do use (or require) a '.C' 'extension'
to cause compilation as C, but the language has nothing
to say about source file names.
compiled with a C compiler.

Well yes, a C compiler is needed to tranlate C source code.

It
would be possible to demonstrate inline assembly within those parameters -

Not portably. C does define a keyword ('asm') to facilitate
inclusion of assembly code, but the assembly code itself is
(necessarily) not at all standard, not part of the C language.
like I say, nothing else is specified, nothing like "must be *portable, ISO*
C".

Those are indeed implied when talking about "C" in this
newsgroup. If your coursework includes things outside
standard C, this is not the place to discuss them.

BTW please do not top-post in comp.lang.c

-Mike
 
J

Jack Klein

In the context of this newsgroup "C" means ISO standard C.


The name or 'extension' (if any) of a file has nothing
to do with whether it contains C source code. Many
implementations do use (or require) a '.C' 'extension'
to cause compilation as C, but the language has nothing
to say about source file names.


Well yes, a C compiler is needed to tranlate C source code.



Not portably. C does define a keyword ('asm') to facilitate
inclusion of assembly code, but the assembly code itself is
(necessarily) not at all standard, not part of the C language.

Sorry, Mike, you're flat-out wrong on this one. There is no 'asm'
keyword in any version of the C standard. It was an extension prior
to ANSI 89/ISO 90, but was not included in those or any later C
standards.

It is a keyword in C++, but of course that's OT here.
 
M

Mike Wahler

Jack Klein said:
Sorry, Mike, you're flat-out wrong on this one. There is no 'asm'
keyword in any version of the C standard. It was an extension prior
to ANSI 89/ISO 90, but was not included in those or any later C
standards.

It is a keyword in C++, but of course that's OT here.

I did find 'asm' in ISO 9899, but failed to note it was in
section J.5, "Common extensions", and that the entire 'J'
section is informative, not normative. Sloppy, sloppy detective
work... :)

Thanks for the correction.


-Mike
 
K

Keith Thompson

Bonj said:
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.
[snip]

A number of people have explained the rationale behind our reasons for
not top-posting. Several links have been posted in this thread.
Please read them. (Basically, it makes it easier to read each article
from top to bottom. If there's too much quoted text, the solution is
to trim it, not to try to hide it at the bottom.)

If you don't believe that, consider the fact that most of us here, and
100% of the regulars, always bottom-post. The only thing worse than
either top-posting or bottom-posting is a mixture of the two. "When
in Rome ...".

And if you don't take that seriously, you need to be aware that people
are going to complain every time you top-post. It's not going to be
the same people every time, and asking us not to complain isn't going
to work. That's not in any way meant as a threat, it's just a fact
about the dynamics of this newsgroup.
 
K

Kishore

#include <stdio.h>
main () {
int a, b ;
printf ("Enter a and b\n") ;
scanf ("%d%d", &a, &b) ;
((a > b) && printf ("a is bigger\n")) || ((b > a) && printf ("b is
Biger\n")) || ((a == b) && printf ("Both r equal\n")) ;
}

This is the program with out using if and ternary operator.if any
mistake is there pls let me know that one.

regards
Kishore Kumar .M.V
 
C

Chris Dollin

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

<fx:whistling/>

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

no ifs, no conditional statements.
 
L

Lawrence Kirby

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.

Lawrence
 

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