Southperry.net
Puzzle (coding) - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: The Speakeasy (https://www.southperry.net/forumdisplay.php?fid=54)
+--- Thread: Puzzle (coding) (/showthread.php?tid=10684)



Puzzle (coding) - Russt - 2009-04-27

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.


Puzzle (coding) - XTOTHEL - 2009-04-27

i only see one too Sad


Puzzle (coding) - Russt - 2009-04-29

hm.
well...
 Solution 1



Puzzle (coding) - Dusk - 2009-04-29

Doesn't it already print one dash without changing the code at all?


Puzzle (coding) - Morgana - 2009-04-29

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...


Puzzle (coding) - Heidi - 2009-04-29

I have the second solution for part 1
 Spoiler

EDIT: nvm, I was beaten to it Tongue


Puzzle (coding) - Morgana - 2009-04-29

 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. =/


Puzzle (coding) - Heidi - 2009-04-29

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)


Puzzle (coding) - Russt - 2009-04-29

Yeah, i < -n and n = -20 don't work because you'd have 0 < -20 which is false..


Puzzle (coding) - Morgana - 2009-04-29

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. =/


Puzzle (coding) - Cactuar - 2009-04-30

 Spoiler



Puzzle (coding) - KajitiSouls - 2009-04-30

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



Puzzle (coding) - Russt - 2009-05-01

 1 dash, preceding code (solution by GummyBear)
 20 dashes, preceding code?

Oh, and the lulz solution:
 Spoiler



Puzzle (coding) - GummyBear - 2009-05-02

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("-");
}