Sadique
Sadique

Reputation: 22841

Not able to solve the puzzle regarding this code


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!

Upvotes: 5

Views: 1432

Answers (10)

Rob Kennedy
Rob Kennedy

Reputation: 163357

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.

Upvotes: 1

corsiKa
corsiKa

Reputation: 82599

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

Upvotes: 2

Lunivore
Lunivore

Reputation: 17677

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

Upvotes: 0

Bill the Lizard
Bill the Lizard

Reputation: 406125

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.

Upvotes: 10

AnT stands with Russia
AnT stands with Russia

Reputation: 320777

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

Upvotes: 5

NinjaCat
NinjaCat

Reputation: 10224

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.

Upvotes: 0

IVlad
IVlad

Reputation: 43517

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

Upvotes: 24

Eric
Eric

Reputation: 19873

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

Upvotes: 2

Heath Hunnicutt
Heath Hunnicutt

Reputation: 19467

I can do it by adding a single character:

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

Upvotes: 0

spinon
spinon

Reputation: 10857

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.

Upvotes: 1

Related Questions