Hi all
In a colelge test we were asked to write a Program that prints nums from 1 - 100 and 100 - 1 without using loops( if, while, or). Am unable to think a good solution to it.
any punters in this groups got solutions ?
TIA
~codo
Hi all
In a colelge test we were asked to write a Program that prints nums from 1 - 100 and 100 - 1 without using loops( if, while, or). Am unable to think a good solution to it.
any punters in this groups got solutions ?
TIA
~codo
if is not a loop.Originally Posted by codomaniac
The one who says it cannot be done should never interrupt the one who is doing it.
Oh yes right. Bad connection @ my place means typos while posting hastengly. Yes IF is not of LOOPing category.
Use a whole lot of copying and pasting.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
And I assume you can figure out the way for downward So I guessed you missed that question Unless you used XSquared's version
Code:#include <stdio.h> void print_up(int num){ if(num<=100){ printf("%d\n",num); num++; print_up(num); } } int main(void){ int i=1; print_up(i); return 0; }
Slight variation to the above...Code:int print_up( int num ) { return num < 101 ? printf("%d\n", num ) + print_up( num + 1 ) : 0; }
Quzah.
Hope is the first step on the road to disappointment.
TO: codomaniac. This is a tertinary opertor the same as an if statementBUTCode:(x>22) ? printf("%d",x) : exit(0);
How does that + sign work?? Is it the same as a semicolon or somethingCode:int print_up( int num ) { return num < 101 ? printf("%d\n", num ) + print_up( num + 1 ) : 0; }
Last edited by linuxdude; 05-29-2004 at 07:49 PM.
it works just like normal addition. printf() returns the number of characters outputted. So it'll add that value to the return of print_up
Quzah wants is to evaluate both printf and print_up in one statement . By returning their sum he does both, he could also have use the comma operator. Not a pretty code, but it gets the work done.Originally Posted by linuxdude
The one who says it cannot be done should never interrupt the one who is doing it.
No, it does what it's supposed to do. It adds 1, the return value of printf, to the return value of all the other function calls. Thus, you could modify it slightly, and have it take two arguments, and return to the origional caller the number of actual printed lines:
The comma operator wouldn't do what I had in mind when I wrote it. I wrote it to do exactly what it does for a reason. If you wanted a better syntax for it, you'd reverse the two parameters, thus it would be:Code:int print_up( int to, int num ) { return num < to ? printf("%d\n", num ) + print_up( to, num + 1 ) : 0; }
Quzah.Code:print_up( 1, 400 );
Hope is the first step on the road to disappointment.
> printf("%d\n", num ) + print_up( to, num + 1 )
But this assumes an order of evaluation which C does not guarantee
The compiler could easily call print_up() before printf(), then perform the addition.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Why should the order matter? Either way you're getting a return value from both functions. Both return values are added together, and returned at the same time. I can't see how the order has any effect. It doesn't matter if printf or print_up is evaluated first. The end result is:
return( valueN + valueX )
Quzah.
Hope is the first step on the road to disappointment.
> 1) isn't the + operator reading from left to right?
Only the compiler parser reads it from left to right
By the time the compiler code generator and optimiser have finished with it, who knows.
So long as it still conforms to the rules of precedence, it should not matter which order your operands are evaluated in.
3.4 3.5 3.8
Any decent C book should explain order of evaluation, in particular that && and || guarantees it, and pretty much everything else doesn't.
> what would the problem be if print_up() was called befor printf() ?
It would then be called print_down()
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
C does not guartnee the order in which functions in the same command are called.
for example:
given
thanCode:int x(void); int y(void);could be compiled as (quasi psudo here)Code:int z = x() + y()
Code:int hidden_temp_variable = x(); int z = hidden_temp_variable + y();Which one is used is up to the compiler to decide.Code:int hidden_temp_variable = y(); int z = x() + hidden_temp_variable;
edit: Dang I should stop queuing up messages when I'm gonna take my time on them