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!
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!
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.
I can do it by adding a single character:
int i,n=20;
for(i=0; - i <n;i--)
printf("-");
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
The problem, as stated, has no solution. Either you or whoever gave that problem to you have stated it incorrectly.
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("-");
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.
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. :-)
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.
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...
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.