By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

Comparing two numbers

P: n/a
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 #1
Share this Question
Share on Google+
89 Replies


P: n/a
purifier <cn******@gmail.com> scribbled the following:
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?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #2

P: n/a

"purifier" <cn******@gmail.com> wrote in message
news:19******************************@localhost.ta lkaboutprogramming.com...
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
Nov 14 '05 #3

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:rw*****************@newsread3.news.pas.earthl ink.net...

"purifier" <cn******@gmail.com> wrote in message

news:19******************************@localhost.ta lkaboutprogramming.com...
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy?


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

P: n/a
"purifier" <cn******@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

P: n/a
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....


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

%

--
Peter

Nov 14 '05 #6

P: n/a
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 #7

P: n/a
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.

Nick.

Nov 14 '05 #8

P: n/a

"purifier" <cn******@gmail.com> wrote in message
news:8a******************************@localhost.ta lkaboutprogramming.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...


I gave you one good clue already:

0 evaluates to false, 1 evalutates to true.

Another:

0 + x == x

-Mike
Nov 14 '05 #9

P: n/a
purifier wrote:

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

P: n/a
"infobahn" <in******@btinternet.com> wrote in message
news:41***************@btinternet.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

P: n/a
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******************************@localhost.ta lkaboutprogramming.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

P: n/a
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******************************@localhost.ta lkaboutprogramming.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

P: n/a
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

P: n/a
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;
}
"purifier" <cn******@gmail.com> wrote in message
news:19******************************@localhost.ta lkaboutprogramming.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

P: n/a
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

P: n/a
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.com, 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

P: n/a
이기운 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_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;
}


--
Peter

Nov 14 '05 #18

P: n/a
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

P: n/a
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

P: n/a
"Bonj" <a@b.com> wrote in message news:35*************@individual.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

P: n/a

"Peter Nilsson" <ai***@acay.com.au> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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_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


--
Peter
Nov 14 '05 #22

P: n/a
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

P: n/a

"purifier" <cn******@gmail.com> wrote in message
news:5c******************************@localhost.ta lkaboutprogramming.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

P: n/a
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 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

Nov 14 '05 #25

P: n/a
"Default User" <de***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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

P: n/a
Mike Wahler wrote:
"Default User" <de***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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

P: n/a

"Default User" <de***********@yahoo.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
Mike Wahler wrote:
"Default User" <de***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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

P: n/a
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

P: n/a
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

P: n/a
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".
"Lawrence Kirby" <lk****@netactive.co.uk> wrote in message
news:pa***********************@netactive.co.uk...
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 #31

P: n/a
"Bonj" <a@b.com> wrote in message news:35*************@individual.net...
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
Nov 14 '05 #32

P: n/a

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

P: n/a
On Tue, 18 Jan 2005 02:19:58 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:

"Bonj" <a@b.com> wrote in message news:35*************@individual.net...
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.


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


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #34

P: n/a
Mike Wahler wrote:
"Bonj" <a@b.com> wrote
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'll just let the links in my sig do the explaining.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #35

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:6t********************************@4ax.com...
On Tue, 18 Jan 2005 02:19:58 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c:
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.


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

P: n/a
"Bonj" <a@b.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #37

P: n/a
#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

Nov 14 '05 #38

P: n/a
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....


<fx:whistling/>

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

no ifs, no conditional statements.

--
Chris "letter, not spirit" Dollin
Nov 14 '05 #39

P: n/a
On Mon, 17 Jan 2005 20:25:01 +0000, Mike Wahler wrote:

"purifier" <cn******@gmail.com> wrote in message
news:5c******************************@localhost.ta lkaboutprogramming.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'.


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

Nov 14 '05 #40

P: n/a
On Tue, 18 Jan 2005 00:21:58 +0000, Bonj wrote:
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

Nov 14 '05 #41

P: n/a
Chris Dollin wrote:

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


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

--
pete
Nov 14 '05 #42

P: n/a
pete wrote:
Chris Dollin wrote:

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


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


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.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #43

P: n/a
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

purifier wrote:
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 #44

P: n/a
"Chris Dollin" writes:
pete wrote:
Chris Dollin wrote:

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

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


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

P: n/a
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...

Nov 14 '05 #46

P: n/a
osmium wrote:
"Chris Dollin" writes:
pete wrote:
Chris Dollin wrote:

purifier wrote:
[snip]
<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.


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

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #47

P: n/a
purifier wrote:

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:Th*************@gmail.com>

Nov 14 '05 #48

P: n/a
>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...

Nov 14 '05 #49

P: n/a
"purifier" <cn******@gmail.com> writes:
but that uses the ternary operator right? That one, if i'm not mistaken is
just another form of if-else...

[...]

The ternary operator has similar semantics to if-then-else, but it's
not another form of it. For one thing, it's an operator, not a
statement.

The original problem statement said:

do not use 'if' or any conditional statements

If you want to restrict the use of the ternary operator, you need to
say so in the problem definition. It would also be good to define
exactly what you mean by "conditional statements".

If, as you say, this is meant to be a fun little puzzle, we're free to
exploit any possible nitpicking interpretation of the question
(reasonableness doesn't apply since the whole idea is not particularly
reasonable).

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. If you can state the restrictions
in portable C terms (even if the motivation for the restrictions is
off-topic), you might have an interesting question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #50

89 Replies

This discussion thread is closed

Replies have been disabled for this discussion.