473,804 Members | 2,124 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
"infobahn" <in******@btint ernet.com> wrote in message
news:41******** *******@btinter net.com...
If an electronic engineer can't get it with all those hints,
there's something wrong with the awards process.


You gave away more than I would have! :-)

The only additional clue I was going to add was
the word "array".

-Mike
Nov 14 '05 #11
Easy - use inline assembly.
Or subtract one from the other and the test the sign bit.
"purifier" <cn******@gmail .com> wrote in message
news:19******** *************** *******@localho st.talkaboutpro gramming.com...
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 #12
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.

"purifier" <cn******@gmail .com> wrote in message
news:8a******** *************** *******@localho st.talkaboutpro gramming.com...
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...
Thank You

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


max(a,b) = (abs(a-b)+a+b)>>1;

this hint has of course some obvious limitations :)
przemek drochomirecki
Nov 14 '05 #14
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;
int lagestNumber = 0;
int signBitCount = (sizeof(int) * 8) - 1;
int signBitMask = 0x1 << signBitCount;
int a_minus_b = a - b;
int b_minus_a = b - a;
int signBitA = 0;
int signBitB = 0;

signBitA = ((unsigned)a_mi nus_b & signBitMask) >> signBitCount;
signBitB = ((unsigned)b_mi nus_a & signBitMask) >> signBitCount;

x = (signBitA ^ 0x1) * a + (signBitB ^ 0x1) * b;
printf("lagestN umber = %d\n", lagestNumber);

return 0;
}
"purifier" <cn******@gmail .com> wrote in message
news:19******** *************** *******@localho st.talkaboutpro gramming.com...
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 #15
Nick Austin wrote:
On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <ai***@acay.com .au>
wrote:

[...]
The following uses strtol() to convert arguments on the command
line, and printf() to output the maximum. Otherwise, it doesn't
use any conditional, logical, equality or relational operators,
nor does it use any selection or iteration statements.
[...]
typedef unsigned long q1;
q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
{
return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;}


I don't think that's portable, namely the use of the bitwise

operator:
( q3 - q4 ) & ( q4 - q3 )

It seems to me that it assumes twos compliment and could give the
wrong answer on other architectures.


"Two's complement" applies to _signed_ integers. The quoted expression
operates on unsigned integers, for which there is only one choice
of representation, namely, pure binary, and for which the behaviour on
overflow is well defined.

--
Peter

Nov 14 '05 #16
Bonj wrote:

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.


Please don't toppost. As you can see, doing so has lost all
context here. As a matter of fact the 'homework' rejection is
designed to avoid creating a class of useless programmers who have
never learned to do anything themselves. Those that make efforts
but run into difficulties, and demonstrate it by posting their
work, usually get help.

--
"If you want to post a followup via groups.google.c om, 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 #17
À̱â¿î 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;
}


--
Peter

Nov 14 '05 #18
Nick Austin wrote:
On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <ai***@acay.com .au>
wrote:

[...]
The following uses strtol() to convert arguments on the command
line, and printf() to output the maximum. Otherwise, it doesn't
use any conditional, logical, equality or relational operators,
nor does it use any selection or iteration statements.
[...]
typedef unsigned long q1;
q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
{
return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;}


I don't think that's portable, namely the use of the bitwise

operator:
( q3 - q4 ) & ( q4 - q3 )

It seems to me that it assumes twos compliment and could give the
wrong answer on other architectures.


If unsigned integers looks like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers. ;)

Unsigned types must use 'pure binary', and the behaviour on overflow
is well defined.

--
Peter

Nov 14 '05 #19
On Mon, 17 Jan 2005 08:02:08 +0000, Bonj wrote:
Easy - use inline assembly.
The question was to write a program in C, and C has no inline assembly.
Or subtract one from the other and the test the sign bit.


Something along those lines may work although making it portable would be
a challenge.

Lawrence

Nov 14 '05 #20

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
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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?
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.