Author |
Topic: In three ways (Read 7891 times) |
|
Misha Kruk
Guest
|
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.
|
|
IP Logged |
|
|
|
-D-
Guest
|
Well... since the initial code is obviously wrong.. the first answer would be to "fix" the code... for( i = 0; -i < 20; i-- ) ...
|
|
IP Logged |
|
|
|
-D-
Guest
|
Answer #2... change i-- to n--
|
|
IP Logged |
|
|
|
AJ
Guest
|
Third one: for(i = 0 ; i + n ; i--) Tricy one
|
|
IP Logged |
|
|
|
-D-
Guest
|
good one AJ.... that was tricky. -D-
|
|
IP Logged |
|
|
|
Barukh
Uberpuzzler
Gender:
Posts: 2276
|
|
Re: In three ways
« Reply #5 on: Sep 22nd, 2003, 2:20am » |
Quote Modify
|
This nice puzzle has a variation: Find a way to make this program print 21 copies of '-'. The requirements are the same.
|
|
IP Logged |
|
|
|
Sir Col
Uberpuzzler
impudens simia et macrologus profundus fabulae
Gender:
Posts: 1825
|
|
Re: In three ways
« Reply #6 on: Sep 22nd, 2003, 5:54am » |
Quote Modify
|
Are we supposed to find 3 ways? Here's one: int i, n=20; for (i=0; i < -n; i--) {printf("-");};
|
|
IP Logged |
mathschallenge.net / projecteuler.net
|
|
|
wowbagger
Uberpuzzler
Gender:
Posts: 727
|
|
Re: In three ways
« Reply #7 on: Sep 22nd, 2003, 6:20am » |
Quote Modify
|
Sir Col, your solution doesn't work (as already noted in Misha Kruk's original post), because already the first test of i < -n is false, so there will be no "-" characters at all.
|
|
IP Logged |
"You're a jerk, <your surname>!"
|
|
|
Sir Col
Uberpuzzler
impudens simia et macrologus profundus fabulae
Gender:
Posts: 1825
|
|
Re: In three ways
« Reply #8 on: Sep 22nd, 2003, 8:53am » |
Quote Modify
|
Duh, silly me. I was thinking that it looped until, rather than while. In which case... hmm?
|
|
IP Logged |
mathschallenge.net / projecteuler.net
|
|
|
James Fingas
Uberpuzzler
Gender:
Posts: 949
|
|
Re: In three ways
« Reply #9 on: Sep 22nd, 2003, 10:19am » |
Quote Modify
|
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: could we use a bitwise operator instead of '+'?
|
|
IP Logged |
Doc, I'm addicted to advice! What should I do?
|
|
|
Sir Col
Uberpuzzler
impudens simia et macrologus profundus fabulae
Gender:
Posts: 1825
|
|
Re: In three ways
« Reply #10 on: Sep 22nd, 2003, 2:05pm » |
Quote Modify
|
Ooh, that is very sneaky. I'd better not give too much away. Nice puzzle, Barukh.
|
|
IP Logged |
mathschallenge.net / projecteuler.net
|
|
|
Dudidu
Full Member
Posts: 227
|
|
Re: In three ways
« Reply #11 on: Oct 8th, 2003, 11:00am » |
Quote Modify
|
I'm sorry but I think that the answer should be posted so everybody could learn from it (if they want to). So... for (i = 0 ; ~i < n ; i--)
|
|
IP Logged |
|
|
|
Niharika Jeena
Guest
|
U can print it 21 times by adding "~" before i in condition.. for(i=0;~i<n;i--) this will give the solution
|
|
IP Logged |
|
|
|
d_ram22
Newbie
Coder
Gender:
Posts: 2
|
|
Re: In three ways
« Reply #13 on: Apr 7th, 2009, 8:36am » |
Quote Modify
|
Good puzzle there 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: hidden: | #include<stdio.h> main() { int i, n=20; for (i=0; i ^ n; i++) {printf("-");}; } |
|
|
IP Logged |
|
|
|
Eigenray
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 1948
|
|
Re: In three ways
« Reply #14 on: Apr 7th, 2009, 12:17pm » |
Quote Modify
|
on Apr 7th, 2009, 8:36am, d_ram22 wrote:Good puzzle there 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).
|
|
IP Logged |
|
|
|
|