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
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.
2009-04-29, 09:02 PM (This post was last modified: 2009-04-29, 09:12 PM by Morgana.)
Dusk Wrote:Doesn't it already print one dash without changing the code at all?
Spoiler
It prints an infinite number of dashes as-is.
EDIT:
The possible solutions?
1. -i < n (as you said)
2. i < -n
3. n = -20
This is assuming that C can handle negative indices in for loops. I tried compiling it in Java and it won't work. But I barely know anything about C, so if I'm retarded, just let me know.
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
n--
(??!!)
EDIT4: Nvm, my EDIT1 solutions don't work, because then there'd be more than 3 solutions...
2009-04-29, 09:37 PM (This post was last modified: 2009-04-29, 09:41 PM by Heidi.)
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: Can't continue doing this, have to go to mall with my father for a bit (Who is in a bad mood )
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. =/
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
Code:
int i, n = 20;
for (i = 0; i < n; [color=Red]n[/color]--)
{
printf("-");
}
Solution 2
Code:
int i, n = 20;
for (i = 0; [color=Red]-[/color]i < n; i--)
{
printf("-");
}