Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Puzzle (coding)
#1
from XKCD wiki thingy

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

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

I've only found one, but I'm stupid Rolleyes

Spoilerplz.

Also:
* Find a way to get it to print exactly 21 dashes (changing 1 character).
* Find a way to get it to print exactly 1 dash (changing one character).
* Print 20 dashes with the given code, without changing it at all, but adding some preceding code.
* Print 1 dash with the given code, without changing it at all, but adding some preceding code.
Reply
#2
i only see one too Sad
Reply
#3
hm.
well...
 Solution 1
Reply
#4
Doesn't it already print one dash without changing the code at all?
Reply
#5
Dusk Wrote:Doesn't it already print one dash without changing the code at all?

 Spoiler

EDIT:
 The possible solutions?

EDIT2: To do 21 dashes... you have to change 1 character from the original, not 1 from one of the 20-dash solutions, right? Since the latter is a lot easier...

EDIT3:
 OHWAIT

EDIT4: Nvm, my EDIT1 solutions don't work, because then there'd be more than 3 solutions...
Reply
#6
I have the second solution for part 1
 Spoiler

EDIT: nvm, I was beaten to it Tongue
Reply
#7
 One dash solution

Eek, sorry, Heidi. =/

EDIT: Crap, scratch that one-dash solution. Seems like it should work, but it doesn't when I compile it. =/
Reply
#8
Morgana Wrote:EDIT: Crap, scratch that one-dash solution. Seems like it should work, but it doesn't when I compile it. =/

Remember that 0 < 0, is not true, so the loop goes false straight away
Even if it was <=, decreasing 0 starting at 0 is always going to be less than or equal to 0, resulting in an infinite loop

Edit: Sad Can't continue doing this, have to go to mall with my father for a bit (Who is in a bad mood Tongue)
Reply
#9
Yeah, i < -n and n = -20 don't work because you'd have 0 < -20 which is false..
Reply
#10
This would be so much easier if it were a do-while loop. Then like everything I thought of would work. =P

Yeah, I was having fun working on it, despite my for loop failures, then I realized I need to study for my Data Structures and Algorithms final (in Java) at 7:30 tomorrow morning, and it's almost 11:30. =/
Reply
#11
 Spoiler
Reply
#12
Russt Wrote:Find three ways to make the C program below print 20 copies of the dash character '-' by changing/adding one character:
Code:
int i, n = 20;
for (i = 0; i < n; i--)
{
    printf("-");
}
Also:
* Find a way to get it to print exactly 21 dashes (changing 1 character).
* Find a way to get it to print exactly 1 dash (changing one character).
* Print 20 dashes with the given code, without changing it at all, but adding some preceding code.
* Print 1 dash with the given code, without changing it at all, but adding some preceding code.

I've never worked with C before xD

 Solution 1

 Solution 2

 Solution 3
Reply
#13
 1 dash, preceding code (solution by GummyBear)
 20 dashes, preceding code?

Oh, and the lulz solution:
 Spoiler
Reply
#14
dont forget the
* Find a way to get it to print exactly 1 dash (changing one character).

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


Forum Jump:


Users browsing this thread: 1 Guest(s)