tags:

views:

344

answers:

10

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

I have been rattling my brain but have not been able to solve this.

Delete any single character or operator from above code and the program should print "-" 20 times

Please help!

+1  A: 
int i,n=20;
for(i=0;i<n;i--) //change i-- to i++
printf("-");

EDIT: You were using a decrement operator instead of increment. So you want it to keep incrementing i till it reaches 20. At which point it will stop because then i would no longer be less than 20 but equal to.

spinon
HOCUS POCUS --The question says Single Character, what you said have come to my mind.. but the person asking me isnt happy.
strut
"Delete any single character or operator from above code" - you replaced two characters.
Jon B
That isn't deleting though. It's changing it to something else.
Daniel Egeberg
he said character or operator. -- is an decrement operator.
spinon
@spinon - good point. However, it wasn't deleting, it was replacing.
Jon B
It's as valid as the other "replace" answer. It *replaces* only one *operator*. If you think it's a bad answer, then why don't you supply an answer that solves the stated question.
Jason Williams
@Jon I agree. But the question says to delete a single character or operator. It doesn't say anything about how many can be added. So I deleted a -- operator and added a ++ operator. So did I really replace or did I delete and add? ;P
spinon
@spinon - we'll have to take this to lawyer-overflow.com :)
Jon B
@Jason - Just because you know an answer is wrong, doesn't mean you know the correct answer.
mphair
A: 

I can do it by adding a single character:

int i,n=20;
for(i=0; - i <n;i--)
    printf("-");
Heath Hunnicutt
What's more, I believe you can mentally attempt to delete each character or each operator, and convince yourself that no solution which strictly deletes one character or operator satisfies the question.
Heath Hunnicutt
This will print '-' about 2 billion times (on a 32 bit processor). -20 is smaller than 20... -21 is smaller than 20...
Jason Williams
Well, if it prints 2 billion times, it'll definitely have printed 20 times...
Thom Smith
@Heath Hunnicutt: ...which is why "there's no solution" is the only correct answer to the problem.
AndreyT
@Jason Williams: Incorrect. `i` will acquire values 0, -1, -2, -3 and so on, which means that `-i` will stand for 0, 1, 2, 3 and so on. The cycle will iterate only 20 times, until `-i` reaches 20.
AndreyT
@Jason Williams: You are wrong and my code is not. AndreyT already took you to school on that. But to reiterate (-(-20)) == 20.
Heath Hunnicutt
Ah, je suis une idiot. I hate double negatives :-)
Jason Williams
@Jason Williams: Don't be so hard on yourself! Just try to think positive about double negatives! ;)
Heath Hunnicutt
@Heath: Nah, I just need my 2 year old to let me get more sleep! I've always preferred double positives though - probably why I like C++ better than C ;-)
Jason Williams
+2  A: 
int i,n=20;
for(i=0;i<n;n--)
printf("-");

I don't know if replacing is ok but changing i-- to n-- should do the trick

Eric
I bet this is what the poser seeks. ;)
Heath Hunnicutt
Yeah, that's technically not "deleting", so I'm not sure.
Jon B
@Heath - was "poser" a Freudian slip? :)
Jon B
@Jon B. No; if you are generous, call it a double entendre. :) Though, I do love that book of Freud's: Psychopathology of Everyday Life. :)
Heath Hunnicutt
+4  A: 

The problem, as stated, has no solution. Either you or whoever gave that problem to you have stated it incorrectly.

AndreyT
This answer requires a formal proof.
intuited
@intuited: This answer allows a brute-force proof, which has already been mentioned in some other comment. One can simply try all possible variants. I don't see the point in publishing this kind of proof in its entirety. It is sufficient to say that I've done it :)
AndreyT
I believe that I won't be able to refute your claim that you've done it. :)
Charles Bailey
Fair enough. But technically, as others have pointed out, if you delete the first newline, it will print `-` 20 times. And then continue to do so over and over and over again.
intuited
+16  A: 

I don't think you can do it by deleting a character, but I have three solutions that replace (well, one of them adds a character, but only because you have no whitespace in your program. If you had whitespace, it would replace a space).

Solution 1

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

Solution 2

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

Solution 3

int i,n=20;
for(i=0;i+n;i--) // while i + n is not zero 
    printf("-");
IVlad
+1 - I would have scratched my head for quite a while before coming up with solution 3.
Bill the Lizard
A: 

Change:

for(i=0;i<n;i--)

to:

for(i=0;i<n;n--)

But I don't see how you can only delete a char or operator... You have to modify an operator or character.

NinjaCat
+2  A: 

The puzzle is supposed to allow for "changing one character".

The solutions are to change < to +, change i to n, or change the space before i in the middle of the for loop to a - (there's supposed to be spaces.)

Your friend doesn't get the question. :-)

glowcoder
Changing < to > does not work...
Heath Hunnicutt
You're right, I was confusing it with a different, but similar teaser. :) whoops! fixed
glowcoder
+9  A: 

I found a reference to the problem on C Puzzles. (It's in a comment, so it's surely not the original source.)

The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work.

#include <stdio.h> 
int main() 
{ 
int i; 
int n = 20; 
for( i = 0; i < n; i-- ) 
printf("-"); 
return 0; 
}

Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.

Note that the instructions say:

...you have to fix the above code, by changing exactly one character.

One solution is to change i-- to n-- in the header of the for loop.

Bill the Lizard
That, of course, is a different problem. In this problem we are only allowed to "delete one character or operator".
AndreyT
Nice find. I was trying to search for it as well but couldn't find anything.
spinon
@spinon: I had a pretty good idea where to start looking. It's terribly hard to search for specific C code on the wide open internet. :)
Bill the Lizard
@Bill Yeah that is for sure. I was trying the term puzzle, any character or operator, stuff like that.
spinon
A: 

It's been years since I did c, and I'm pedantic, so please forgive me, but... doesn't the program print "-" 20 times already? And then some?

If you delete the "f" from "printf", doesn't it continue to print "-" 20 times? At least?

If it's a trick question, maybe this is the trick...

Lunivore
+1  A: 

The program already prints - 20 times — and then it continues to print it a lot more afterward. The puzzle didn't say it needed to print it exactly 20 times.

If you really must delete something, then you can get similar behavior by deleting the -- operator.

int i,n=20;
for(i=0;i<n;i) // no more decrement
printf("-");

Other characters that are candidates for deletion are the line breaks.

Rob Kennedy
why not delete the first 0?
mvds
That's indeed another possibility.
Rob Kennedy