m3ph1st0s's programming puzzle 3 (C): “Easy bug”

11

This is the 3rd of my series of C/C++ puzzles; in case you missed the first 2 they are here: (1) m3ph1st0s's programming puzzle 1 (C++) (2) m3ph1st0s's programming puzzle 2 (C++): "Call hard!"

I must say that my puzzles are 100% original. If not, I will always state so in the text. My 3rd puzzle has 2 parts as follows:

Puzzle 3.1

This part (3.1) is not an original puzzle of mine, it is collected from some internet page I've read a while ago. I use it here as a starting point and a warm-up for you. Solve this one and then move on to the 2nd part.

Some one tried to print the "+" sign 20 times and came up with the following program:

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

The fact that it did not have the expected result is obvious - the program never ends. Fix it! Easy? Now fix the program by changing ONLY ONE CHARACTER - non-space character of course! For this challenge there are 3 solutions. Find all 3 of them. Just to make it clear: the program must output 20 "+" signs and must end fast. Before criticizing me on what "fast" means, I'll say it means at most a couple of seconds (which by the way is too much but just to make it crystal clear).

Puzzle 3.2

EDITED It was pointed to me earlier that the solution for the 3.2.2 puzzle might be compiler-dependent. In order to eliminate any possible discussion on the subject, I'll modify the idea and improve it on a next puzzle when I'll take extra care not to generate controversy. However, in order to keep this puzzle going, I'll make a small modification for 3.2.2 (the solution will be easier but cleaner).

When I first saw the puzzle I found it pretty awesome. I did manage to solve it but not immediately since it requires some careful attention. If you are here it means you too solved it. If you did so by writing a program to replace all possible characters with all possible values and test every solution, you are lost. Hard working guy though. Now having corrected the program that writes 20 "+" signs:

3.2.1: Insert one single letter and nothing more in the code so that the result is valid and outputs the same thing in all 3 corrected programs. Needless to say, the letter must be before the enclosing } of main (I say that because I don't want to hear people who just put a letter after the program and somehow their compiler was very friendly).

EDITED (see bellow) - For these final questions consider that the counter i starts from -1 instead of 0.

3.2.1.5: Repeat all previous problems with the condition that the output is at least 19 "+" signs (but still a finite output). Changing spaces is allowed. Now you might have found more solutions than in the first case. Some of these will most definitely fit for the 3.2.2 question.

3.2.2: Choose another value to initialize the variable n so that the resulting output will remain the same for at least one corrected programs in 3.2.1.5(not necessarily for all of them).

LAST EDIT1: changing the program so that it outputs 21 "+" signs is still a good solution, as the original text did not say "exactly" 20 signs. However, the infinite output is forbidden. Obviously this doesn't mean let's all start outputting hundreds of "+"signs since it's not forbidden. But eliminating a beautiful 21 output would not be in the spirit of this competition.

LAST EDIT2: considering LAST EDIT1 and accepting space changing it seems that now we have 5 possible solutions, four of which have already been pointed out in the responses. The last challenge however hasn't been touched and I must make it clear once more: n must be assigned another value, solutions that assign 20 to n by some tricks won't do it (like n=20L). Also I prefer to see the 3rd solution that does not change spaces.

LAST EDIT3: I've edited the last questions, please read!

The challenge is to solve both parts of the puzzle. The first one to do it wins.

I hope it's all clear, if not please post any questions and I'll edit as quickly as possible. Cheers. emphasized text

Bogdan Alexandru

Posted 2012-10-01T19:39:38.363

Reputation: 653

Question was closed 2016-01-21T16:56:18.060

I assume changing one character includes changing any spaces to non-space characters? If so, I think I have found all 3 solutions for part 1. – mellamokb – 2012-10-01T20:17:49.017

oh..sorry..I had in mind to deny that explicitely but I forgot. I'll edit now. Thx for asking. – Bogdan Alexandru – 2012-10-01T20:35:35.570

Oh good. Because I can't find any answer for part 3.2.2 for my current 3 solutions... I guess that means I need to look for one more :) – mellamokb – 2012-10-01T20:36:26.723

yes :) good luck on that – Bogdan Alexandru – 2012-10-01T20:37:21.277

I am not real strong on C, so I'm attacking this from a Java/C# background. Does one of the three clever answers use a C-specific "hack" that I will have trouble finding? – mellamokb – 2012-10-01T20:40:40.387

I can't really give up on any clue for the third solution since I think the first two are easier to see. – Bogdan Alexandru – 2012-10-01T20:54:12.387

anyway, keep those 3 solutions that allow spaces to be changed in mind. if eventually nobody comes up with the good solution I might allow the spaces to be converted in order to give a starting point to competition here :) – Bogdan Alexandru – 2012-10-01T20:59:34.457

Feel free to create a new challenge, but please stop continuously changing the criteria. Spend some time analyzing your own questions before posting them. – ardnew – 2012-10-02T20:17:16.777

1@ardnew: I don't believe the OP has once changed the original intent of the question. I agree there are better ways to fix the question than plumping a bunch of Edit's at the end... but it's still at the core the same question, with some things clarified. – mellamokb – 2012-10-02T21:32:23.480

@mellamokb I'm sure the intent hasn't changed, but unfortunately the specifications have. The fundamental task was to output 20 + symbols under certain restrictions. This was the one objective that drove all subsequent tasks. That requirement has apparently been tossed out the window (see: the comments to @mob 's submission, new task 3.2.1.5, and LAST EDIT1). Without that, the "correctness" of a submission is now left to the (clearly indeterminate) opinion of the author. -1 – ardnew – 2012-10-02T23:44:04.013

Answers

8

3.1

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

Any of these changes will make the program output 20 '+' signs. This one is close:

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

It outputs 21 '+' signs.

3.2.1

I found at least 112 ways to solve this problem inserting one letter. Not all of them may work on all compilers.

int n = 20L;
int n = 20f;
int n = 20d;
uint n = 20;

for( i = 0L; ... )
for( i = 0f; ... )
for( i = 0d; ... )

iprintf("+");
printf("+x");
printf("x+");

For the last two, substitute any letter for x to give you 104 possible solutions. Using either of the last two lines will change the output, but the output will still be the same for all 3 corrected programs.

3.2.2

All I've come up with are some things that get cast back to the number 20 on assignment to int.

int n = 20L;
int n = 20.001;
int n = 20.999;
int n = 20 + 0x100000000L;

mob

Posted 2012-10-01T19:39:38.363

Reputation: 2 506

Yep, you have the exact same answers I do (I didn't post them before because I didn't have everything answered). I think the answer to 3.2.2 lies in the third solution to 3.1 that neither one of us has found (and obeys the rule of not allowing the space to be changed). – mellamokb – 2012-10-02T00:24:20.910

On 3.2.1, I'm not sure about the f and d suffixes for int types (well, d for any type for that matter), but there are a few others you've left off: int n = 20l, int n = 20U, and int n = 20u. Also I don't believe uint is a standard type identifier in C or C++. What compiler are you using for these anyway? – ardnew – 2012-10-02T02:48:22.337

You've done a pretty good work here, but not complete! First of all, the ~i solution is still good! The requirement was to output 20 "+" signs so 21 is still a good solution (the only bad solution is infinite output). This means that you have now found 4 solutions! And the funny thing is, I still have one more :) About the 3.2.2, it is bad since I specifically required to change the VALUE of n, not to do some tricks to make it 20 :) – Bogdan Alexandru – 2012-10-02T07:20:39.470

also what with the printf("+x") ??? it will print +X+X+X+X+X... which is not THE SAME output – Bogdan Alexandru – 2012-10-02T07:32:16.730

1and also, both the -i and ~i solutions change spaces so I'll consider them "partial" solutions. the 3rd complete solution must change a non-space character as specified in the quiz text – Bogdan Alexandru – 2012-10-02T08:57:28.810

With printf("+x"), all three programs produce +x+x+x.... The output is valid and produces the same thing in all three programs. – mob – 2012-10-02T14:09:32.677

1you did not understand the problem. I said the modification will produce the same output as the coresponding modified program. that is, I have the corrected programs C1, C2, C3. after the character insertion I have P1, P2, P3. the requirement is: P1 has the same output as C1, P2 has the same output as C2, P3 has the same output as C3. It is NOT P1,P2,P3 having the same output – Bogdan Alexandru – 2012-10-02T17:54:30.033

@BogdanAlexandru Please maintain an objective criteria. If outputting 21 chars is acceptable, what about 22 and 23 chars? Where do we draw the line? – ardnew – 2012-10-02T20:23:36.383

like I said you can output any finite number of chars, but at least 20 – Bogdan Alexandru – 2012-10-03T19:00:12.307

2

3.1

Yet another puzzle. But normal solutions are boring, what about something special?

Solution one:

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

I decided to change ONLY ONE CHARACTER, that is, -. No characters other than - were changed.

Solution two:

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

This changes exactly one character - the semicolon after int i into =printf("++++++++++++++++++++");exit(0);.

Solution three:

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

This loads the stdix.h system header. In the system include path, insert the following file, called stdix.h. It has to contain the following contents.

static inline void printf(const char *string) {
    int i;
    for(i = 0; i < 20; i--)
        putchar('+');
    exit(0);
}

3.2

Now to insert one letter. Well, that's simple, replace int main() with int main(a). This is not valid according to standards, but who cares?

Konrad Borowski

Posted 2012-10-01T19:39:38.363

Reputation: 11 185

0

3.1

  1. Change i-- to n--
  2. i<n to -i<n
  3. (Unfortunately invalid answer because I wasn't checking with compiler before seeing other answers)

3.2.1

int n = 20 

to

uint n = 20

(Compiler dependent...)

3.2.2

int n =-20L;

for(i = -1; i%n; i--)

prints 19 + signs, same as with int n = 20L;. However, I wouldn't have come up with it if I hadn't seen other answers to 3.2.1

Dissident penguin

Posted 2012-10-01T19:39:38.363

Reputation: 103

0

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

CHINMAY AGRAWAL

Posted 2012-10-01T19:39:38.363

Reputation: 11

1Make sure to add the program language and character count into your answer. – Timtech – 2014-09-25T11:01:44.947

1@Timtech why character count? this is not code-golf – proud haskeller – 2014-09-25T12:42:40.847

1@Timtech also why to include the language? this is a language specific challenge. – proud haskeller – 2014-09-25T12:46:05.243

0

Puzzle 3.1 Answers

  1. Change i-- to n-- (demo: http://ideone.com/l0Y10)
  2. Change i < n to i + n (demo: http://ideone.com/CAqWO)
  3. Change [SPACE]i < n to -i < n. (demo: http://ideone.com/s5Z2r)

Puzzle 3.2.1

Change int n = 20 to int n = 20L (add an L to the end).

Puzzle 3.2.2

Haven't found an answer yet...

mellamokb

Posted 2012-10-01T19:39:38.363

Reputation: 5 544

good, standard solution for 3.1 and 3.2.1. – Bogdan Alexandru – 2012-10-02T07:30:45.080