Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing a broken loop by changing exactly one character

I found a site with some complicated C puzzles. Right now I'm dealing with this:

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.

I cannot figure out how to solve. I know that it can be fixed by changing -- to ++, but I can't figure out what single character to change to make it work.

like image 374
Javier Avatar asked Mar 23 '10 20:03

Javier


1 Answers

Here is one solution:

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

Here is a second one, thanks to Mark for helping me!

for( i = 0; i + n; i-- )     printf("-"); 

And Mark also had the third one which is

for( i = 0; i < n; n-- )     printf("-"); 
like image 149
Gab Royer Avatar answered Sep 29 '22 18:09

Gab Royer