int i ; n = 20 ;
for ( i = 0 ; i < n ; i--)
   printf('x');

Find 3 ways to make this code print x 20 times by ONLY CHANGE ONE CHARACTER in this code. Have fun guy!

Are the errors intentional?

Are the errors intentional?

Of course, it is intentional. You need to change ONE CHARACTER in that code to make that program right. This program is supposed to print x 20 times.

NOTE: If you found the answer, don't post the solutions here. Send private message to me. I will tell you whether your solution is correct or not.

>If you found the answer, don't post the solutions here.
I have, and I wasn't going to.

>Send private message to me. I will tell you whether your solution is correct or not.
Send me one of yours and I'll send you one of mine. Too many people try this as a trick to get people to do their homework.

>If you found the answer, don't post the solutions here.
I have, and I wasn't going to.

>Send private message to me. I will tell you whether your solution is correct or not.
Send me one of yours and I'll send you one of mine. Too many people try this as a trick to get people to do their homework.

Why would I trick you? and how could this kind of problem is a homework?

>Why would I trick you?
Don't get all defensive. I'm just being cautious because I've seen this trick countless times. I don't benefit at all from knowing the answer, so what's stopping you from proving to me that you already have it? You can send it to me through PM and I'll verify that you're not tricking anyone. Hell, I'll even abstain from participating in the rest of the thread if my current solution happens to be the same as yours.

>and how could this kind of problem is a homework?
Trust me, this problem reeks of homework. Not good homework, mind you, but teachers give out all kinds of stupid problems that teach nothing about programming.

int i ; n = 20 ;
for ( i = 0 ; i < n ; n--)
   printf('x');

Well theres the simple one.

>Well theres the simple one.
That fails to compile, much less run.

[edit]
By the way, I'm assuming an implied framework so that these examples can be tested:

#include <stdio.h>

int main ( void )
{
  /* Begin exercise code */
  int i ; n = 20 ;
  for ( i = 0 ; i < n ; i--)
    printf('x');
  /* End exercise code */

  return 0;
}

[/edit]

This is verification that the puzzle is legitimate. invisal really does have the three answers he thinks solves the problem, but the code is broken such that those three answers aren't valid. Here's the correct starting code:

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

And for those who are interested, a solution for the original code with errors intact and only one character change within the exercise area:

#include <stdio.h>

#define printf i += 2, putchar

int main ( void )
{
  /* Begin exercise code (changed first ; to ,) */
  int i , n = 20 ;
  for ( i = 0 ; i < n ; i--)
    printf('x');
  /* End exercise code */

  return 0;
}

Personally, I think working around multiple errors makes for a more entertaining problem. ;)

thats embarrassing. I didn't try it but i assume the compiler complains about n being undeclared then. in which case change the semi colon to a comma, but then you need more than 1 change.. im lost

Ok looks like that was one of the (unintentional) problems i overlooked. Also missed the printf char problem.

With those two changes in place my original change on i-- -> n-- works

I was mistakenly type int i ; n = 20;. Actually, it was supposed to be int i , n = 20. It was very embarassing for me to post this puzzle with typo. Anyway, the actual puzzle start up code is:

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

You can change only one character in order to make this program print 'x' 20 times. InfiNate have gotten the first solution done.

>Anyway, the actual puzzle start up code is:
That's still wrong. Refer to my post of the correct code.

I suggest next time you make a puzzle like this do it in pseudo code as to not get hung on on syntax. You were obviously looking for a logic change in your code and we all could have avoided the confusion with psuedo code.

That being said, I loved your solution to the originally stated problem narue :)

Really apologize about that one. Anyway, you may begin now. Only one solution was found so far.

Besides the obvious solution[EDIT]s[/EDIT] I gave you, I don't see any way to do it without playing with the preprocessor (which, to my mind, is actually messing with more than one character...)

Well, my blinders are intact...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.