I got this puzzle from a friend today: you have to change only one char, either by replacing or inserting it, to fix the following code to print 42 asterisks:


int i, n=42;
main() {
for(i=0; i<n; i--) {
? ?printf("*");
}
}


There are 3 solutions AFAIK. The first two are silly, but the third one took me a while.

Enjoy! :)
Posted on 2005-05-17 18:48:50 by QvasiModo
<=
Posted on 2005-05-17 19:36:15 by SpooK
inserting = into i<n won't work because the updater is i--.

Interesting puzzle....


replace the updater with n-- (not 100% sure though)

Posted on 2005-05-17 21:09:52 by AmkG
n=-42
n--
abs(i) :P
Posted on 2005-05-17 21:56:35 by SpooK
n--
i+n
-i < n

Thanks for this little puzzle QvasiModo  ;)
Posted on 2005-05-17 22:26:19 by Dr. Manhattan
Got n-- and -i < n, but didn't get i+n.... wow, that is C! because in C, 0 is the false value, so when i hits -42, i+n is -42+42 is 0, which is false


n=-42 won't work because the first iteration of the for will have i=0, n=-42, i<n is 0 < -42 is false, so it won't print anything.

(no offense intended SpooK)

Posted on 2005-05-18 02:22:16 by AmkG
n=sqrt(42)
Posted on 2005-05-18 02:26:49 by SpooK
In reality it doesn't matter, the answer is always "42" ;)
Posted on 2005-05-18 02:31:19 by SpooK
#define SIX 1+5
#define NINE 8+1

n=SIX * NINE;

But.... what's the question?????  LOL
Posted on 2005-05-18 02:32:23 by AmkG
You are only allowed to add or insert 1 character, so here are my three (took me about 1 minute for the first 2, and about 4 more for the last one):


i--  => n--
i < n => -i < n
i < n => i + n


Mirno
Posted on 2005-05-18 06:01:12 by Mirno

n--
i+n
-i < n

Thanks for this little puzzle QvasiModo ;)


You are only allowed to add or insert 1 character, so here are my three (took me about 1 minute for the first 2, and about 4 more for the last one):


i-- => n--
i < n => -i < n
i < n => i + n


Mirno


That's the answer exactly. And it took us about the same time too. :)
Fun, isn't it? Looks much simpler until you actually try to solve it... ;)
Posted on 2005-05-18 10:29:50 by QvasiModo