Comparing two numbers

P

purifier

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

Joona I Palaste

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

How about (1) doing your homework and (2) speaking English instead of
d00dsp33k?
 
M

Mike Wahler

purifier said:
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy?
Yup.

huh
here's the catch
do not use 'if' or any conditional statements

Still easy.
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....

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

OK, I have the solutions all ready to go.

All I need now is your instructor's email
address, and I'll send them off.

-Mike
 
M

Mike Wahler

Mike Wahler said:
news:19793998f87506ee09a36174c4a83f22@localhost.talkaboutprogramming.com...

OK, here's a hint for the first form:
0 evaluates to false, 1 evaluates to true.

BTW did your assignment specify whether the inputs
could be equal? If so, what should the result be?

-Mike
 
K

Keith Thompson

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

Q: How do I drive a screw without using a screwdriver?

A: You don't. Use a screwdriver. That's what they're for.

Now if you actually have some legitimate reason to drive a screw
without using a screwdriver, someone might be able to come up with a
method -- but nobody is going to help you unless you tell them *why*
you want to do such a silly thing, and why your first question isn't
"Where can I find a screwdriver?".

It's the same thing with your question. Without any background, it's
not particularly interesting. If the background is that it's a
homework question, we're not going to do your homework for you. If
it's just a fun little quiz, some of us might be interested, but you
need to say so.
 
P

Peter Nilsson

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

I read a similar (but more restricted) question very recently
in another forum.

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.

Not sure if Google will mangle this...

% type max2.c
typedef unsigned long q1;int printf(const char*,...);long
strtol(const char*,char**,int);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;}q1 q7(long q8){return q2(q8,0,q2(q8/2,0,1
,0),0);}q1 q9(long q8){return q2(q8,-1,q2(q8/2,-1,1,q2(q8
/2,0,1,0) ),0);}q1 q10(long);q1 q11(long q8){return 0;}q1
q12(long q8){return 1;}q1 q13(long q8){return q10(q8/2);}
q1 q14(long q8){q1(*q15[])(long)={q12,q13};return q15[q2(
q9(q8),1,0,1)](q8);}q1 q10(long q8){q1(*q15[])(long)={q11
,q14};return q15[ q2(q7(q8),1,0 ,1)](q8);}q1 q17(long q3,
long q4){return q2(q10( q3),1,q2(q10(q4),1,q10(q3-q4),1),
q2(q10(q4),1 ,0,q10(q3-q4 )));}long q18(long q3,long q4){
return q3;}long q19(long q3,long q4){return q4;}long q20(
long q3,long q4){long(*q15[])(long,long)={q19,q18};return
q15[q2(q17(q3,q4),1,0,1)](q3,q4);}long q21(const char*q2)
{return strtol(q2,0,10);}int q22(char **q23){printf("%ld"
"\n",q20(q21(q23[1]),q21(q23[2])));return 0;}int q24(char
**q23){return 0;}int main(int q25,char**q23){int(*q15[])(
char**)={q24,q22};return q15[q2(q25,3,1,0)](q23);}

% gcc -ansi -pedantic max2.c -o max2.exe

% max2 -5 42
42

%
 
P

purifier

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
 
N

Nick Austin

On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <[email protected]>
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.

Nick.
 
M

Mike Wahler

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

I gave you one good clue already:

0 evaluates to false, 1 evalutates to true.

Another:

0 + x == x

-Mike
 
I

infobahn

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

Then you shouldn't have any trouble deriving a solution yourself.
Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
if possible, please give me some ideas...

Here are some hints: in C, you can turn an expression into a 0 or a 1
using !!(expression), and you can put "first number is bigger" and
"second number is bigger" into an array like this:

char *bigger[] =
{
"first number is bigger",
"second number is bigger"
};

You can demonstrate equality as a separate step (possibly a preceding
step):

char *equality[] =
{
"numbers are equal",
"numbers are not equal"
};

Here's another hint: you can turn a number into a string using
sprintf, you can compare two strings for equality using strcmp,
which rather conveniently returns 0 when the two strings are
equal. !!strcmp, of course, returns either 0 or 1.

Here's yet another hint: you are not limited to storing your
original two numbers in strings. You can also store their
difference. If they weren't equal, one will be bigger than
the other. If the second is bigger than the first, the result
of a subtraction will be negative. Detecting a minus sign
in the first byte of a string is trivial.

If an electronic engineer can't get it with all those hints,
there's something wrong with the awards process.
 
M

Mike Wahler

infobahn said:
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
 
B

Bonj

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

Przemek Drochomirecki

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

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

this hint has of course some obvious limitations :)


przemek drochomirecki
 
À

À̱â¿î

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_minus_b & signBitMask) >> signBitCount;
signBitB = ((unsigned)b_minus_a & signBitMask) >> signBitCount;

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

return 0;
}
 
P

Peter Nilsson

Nick said:
On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <[email protected]>
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.
 
C

CBFalconer

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.

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

Peter Nilsson

À̱â¿î 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.
 
P

Peter Nilsson

Nick said:
On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <[email protected]>
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.
 
L

Lawrence Kirby

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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top