473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing two numbers

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
89 3475
"Bonj" <a@b.com> wrote in message news:35******** *****@individua l.net...
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
Nov 14 '05 #21

"Peter Nilsson" <ai***@acay.com .au> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
À̱â¿î 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...
Missing inclusion of <stdio.h> or prototype for printf.
The behaviour of printf is therefore undefined.
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_mi nus_b & signBitMask) >> signBitCount;
signBitB = ((unsigned)b_mi nus_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("lagestN umber = %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


--
Peter
Nov 14 '05 #22
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

Nov 14 '05 #23

"purifier" <cn******@gmail .com> wrote in message
news:5c******** *************** *******@localho st.talkaboutpro gramming.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'.

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
Nov 14 '05 #24
purifier wrote:
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 talkaboutprogra mming.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

Nov 14 '05 #25
"Default User" <de***********@ yahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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
Nov 14 '05 #26
Mike Wahler wrote:
"Default User" <de***********@ yahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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. :-)

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

Nov 14 '05 #27

"Default User" <de***********@ yahoo.com> wrote in message
news:11******** ************@z1 4g2000cwz.googl egroups.com...
Mike Wahler wrote:
"Default User" <de***********@ yahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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. :-)

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
Nov 14 '05 #28
Mike Wahler wrote:
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

Nov 14 '05 #29
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.
Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1848
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends, other.extends) instead of
11
2397
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different compiler and got the same erroneous result on two different PCs (with OS Win98 & Win98SE), so it appears to be a problem with ANSI C. I thought that negative double variables could be compared as easily and *reliably* as integers, but apparently...
12
6863
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a))) Do the same issues involved in comparing 2 fp types for equality apply to comparing a float to zero? E.g. is if(x == 0.0) considered harmful?
2
3390
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
14
2516
by: nw | last post by:
Hi, I'd like to compare 2 floating point numbers within a given error. I'd rather not use a absolute error but one related to the number of values that can be represented between the two floats. I've been reading: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm where the following function is provided to do this: bool AlmostEqual2sComplement(float A, float B, int maxUlps) { // Make sure maxUlps is...
0
1098
by: shana07 | last post by:
I need help on comparing lines in my output file... How to delete the second line in text file (FileReader) and print out the first line for the same fileno? I put here sample of my output file: 1 : 30 at 2 1 : 134 at 10 2 : 35 at 2 4 : 30 at 2 4 : 80 at 8
27
3872
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually fails if comparing doubles of different magnitude since it's hard or not possible to find a suitable threshold
18
4123
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
18
2896
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
5
4520
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.