wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> In three ways
(Message started by: Misha Kruk on Aug 1st, 2002, 8:50pm)

Title: In three ways
Post by Misha Kruk on Aug 1st, 2002, 8:50pm
How well do you know C? "for" loops? ;)

Find three ways to make the program below to print 20 copies of the dash character '-' by changing/adding one character:

int i, n=20;
for (i=0; i < n; i--) {printf("-");};

Wrong solutions:

   * Changing "n=20" into "n=-20" won't work.
   * Changing "i--" into "i++" doesn't satisfy the one character condition.
   * Changing "i=0" into "i=40" won't work.
   * Changing "i < n" into "i < -n" wont' work.

Title: Re: In three ways
Post by -D- on Aug 1st, 2002, 10:24pm
Well... since the initial code is obviously wrong.. the first answer would be to "fix" the code...


for( i = 0; -i < 20; i-- ) ...


Title: Re: In three ways
Post by -D- on Aug 1st, 2002, 10:26pm
Answer #2... change    i--   to    n--


Title: Re: In three ways
Post by AJ on Aug 4th, 2002, 7:49am
Third one:
for(i = 0 ; i + n ; i--)

Tricy one ;)

Title: Re: In three ways
Post by -D- on Aug 4th, 2002, 11:51am
good one AJ.... that was tricky.
-D-

Title: Re: In three ways
Post by Barukh on Sep 22nd, 2003, 2:20am
This nice puzzle has a variation:

Find a way to make this program print 21 copies of '-'. The requirements are the same.

Title: Re: In three ways
Post by Sir Col on Sep 22nd, 2003, 5:54am
Are we supposed to find 3 ways?

Here's one:
[hide]int i, n=20;
for (i=0; i < -n; i--) {printf("-");};[/hide]

Title: Re: In three ways
Post by wowbagger on Sep 22nd, 2003, 6:20am
Sir Col, your solution doesn't work (as already noted in Misha Kruk's original post), because [hide]already the first test of i < -n is false, so there will be no "-" characters at all[/hide].

Title: Re: In three ways
Post by Sir Col on Sep 22nd, 2003, 8:53am
Duh, silly me. I was thinking that it looped until, rather than while.

In which case... hmm?  ???

Title: Re: In three ways
Post by James Fingas on Sep 22nd, 2003, 10:19am
Haha! Very very sneaky! The answer is too beautiful to give away, but here's what I was thinking about when I happened to stumble on the answer:[hide] could we use a bitwise operator instead of '+'? [/hide]

Title: Re: In three ways
Post by Sir Col on Sep 22nd, 2003, 2:05pm
Ooh, that is very sneaky. I'd better not give too much away. ;)

Nice puzzle, Barukh.

Title: Re: In three ways
Post by Dudidu on Oct 8th, 2003, 11:00am
I'm sorry but I think that the answer should be posted so
everybody could learn from it (if they want to).
So...
 ;) [hide] for (i = 0 ; ~i < n ; i--) [/hide] ;)




Title: Re: In three ways
Post by Niharika Jeena on Apr 25th, 2005, 2:47am
U can print it 21 times by adding "~" before i in condition..
 for(i=0;~i<n;i--)
 this will give the solution

Title: Re: In three ways
Post by d_ram22 on Apr 7th, 2009, 8:36am
Good puzzle there ;D
the fixed questions is :
Make following program print - 20 times by making one character change (add/modify).

#include<stdio.h>
main() {

int i, n=20;

for (i=0; i < n; i++) {printf("-");};
}

Third Answer:
[hideb]
#include<stdio.h>
main() {

int i, n=20;

for (i=0; i ^ n; i++) {printf("-");};
}
[/hideb]

Title: Re: In three ways
Post by Eigenray on Apr 7th, 2009, 12:17pm

on 04/07/09 at 08:36:54, d_ram22 wrote:
Good puzzle there ;D
the fixed questions is :
Make following program print - 20 times by making one character change (add/modify).

#include<stdio.h>
main() {

int i, n=20;

for (i=0; i < n; i++) {printf("-");};
}

Well that's silly.  For example, you could replace any of the spaces with a \t or a \n, or most of the \ns with spaces.  Or add/remove a space.  Or remove the last semicolon.  Or add a semicolon.  And of course you can also change i < n to i - n.

With one character change, what possible numbers of '-'s can you print?
0, 16, 20, 21, 40, infinity, ...?
Though technically undefined, with gcc<3 or gcc-4 -O(0,1) (or gcc-4 -(fwrapv, fno-strict-overflow), etc.), there are also 2147483648 and 2147483649, if sizeof(int)==4.  And then there is undefined (but probably positive and finite on a 32-bit machine).



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board