5

Possible Duplicate:
Help with C puzzle

The intention of the program was to print a minus sign 20 times, but it doesn't work.

  #include <stdio.h>
  int main()
  {
      int i;
      int n = 20;
      for( i = 0; i < n; i-- )
          printf("-");
      return 0;
  }
Community
  • 1
  • 1
SurajRk
  • 87
  • 1
  • 1
  • 8
  • 1
    it gives infinite minus sign... – SurajRk Aug 11 '10 at 18:30
  • 5
    @SurajRk It won't actually give infinite. It will eventually wraparound to being positive once you try to decrement when i = -2^31 – Jamie Wong Aug 11 '10 at 18:31
  • 13
    Only *unsigned* arithmetic is defined to wrap by the standard; *signed* overflow is undefined behavior. Therefore the compiler is allowed to emit a true infinite loop here. Compare what gcc 4.4 does to this program with `-S -O2` and `-S -O2 -fwrapv`, for instance. – zwol Aug 11 '10 at 18:33
  • 1
    @Zack +1 Did not know that - very good to know – Jamie Wong Aug 11 '10 at 18:34
  • 1
    @Zack Excellent fact. +1 – James Aug 11 '10 at 18:40
  • I saw this as an interview puzzle. Change the code by only changing/inserting/deleting _exactly one_ character to make it print 20 times. –  Aug 11 '10 at 18:48
  • 2
    @Zack: In practice, most general-purpose computer systems will wrap around. You can't count on it. An infinite loop is entirely fine according to the Standard, as is any other behavior whatsoever. – David Thornley Aug 11 '10 at 19:42

9 Answers9

26

This is a classic puzzle!

The way I saw it was

"You can only change/insert/delete one character in the code to make the - print 20 times".

Some answers are (if I remember them correctly)

1)

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

Here you change the i < n to -i < n

2)

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

Here you change the i-- to n--

3)

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

You change the i < n to i+n.

For a challenge, try changing/inserting/deleting one character to make it print the - 21 times. (Don't read the comments to this answer if you want to try it!)

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

You had -- instead of ++

Jamie Wong
  • 18,104
  • 8
  • 63
  • 81
  • 5
    Yup, that loop will run forever because `i` starts at `0` and keeps getting smaller. It will never be larger than *n* so the loop's exit condition will never be reached. – FrustratedWithFormsDesigner Aug 11 '10 at 18:30
  • 4
    Surely it will be larger than n when the integer wraps around? The loop will just run for a little longer than intended is all. – signine Aug 11 '10 at 22:29
10

Replace i-- with i++.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
4
int main() { 
  int i; 
  int n = 20; 
  for( i = 0; i < n; i++)
    printf("-"); 
  return 0; 
}

You had decrement instead of increment.

James
  • 2,454
  • 1
  • 22
  • 22
2

Have you tried changing the

i--

to

i++

You have the loop to print out a "-" for as long as "i" is less than 20. After every loop you reduce the value of i by 1, it will continue to print for a very long time. Changing the final part of the for loop to "i++" means it will perform one iteration each loop and stop once the twentieth iteration finished.

Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102
2

Change i-- to i++. i-- decrements the value which at start is 0 and with subsequent reductions won't ever reach 20 (or +20).

Dienekes
  • 1,548
  • 1
  • 16
  • 24
1

the i-- needs to be i++

you could also do

 int n = -20;
  for( i = 0; i > n; i-- )

but that is bad coding practice

Forge
  • 501
  • 2
  • 9
  • 4
    Why suggest an answer that's bad coding practice? – Jamie Wong Aug 11 '10 at 18:36
  • 2
    because in other situations you do want to decrement your counter. in this case decrementing the counter doesn't make sense. – Forge Aug 11 '10 at 18:40
  • 2
    @Jamie Creativity ;) One can learn even from something that shouldn't be done a particular way. – Jake Petroules Aug 11 '10 at 18:44
  • 1
    Creativity has its places - suggesting a course of action to someone who is clearly a beginner in c is not one of them. Also, I agree that decrementing counters is occasionally more useful - so suggest it _when_ it is more useful. Again, this is not the case here. – Jamie Wong Aug 11 '10 at 18:46
1

What exactly are you trying to do with this problem??? Here you are trying to decrement the value of a variable..a variable whose value will never reach the condition (i<20) you have provided... hence it will keep on printing '-' until what jamie wong specified, i.e. i= -2^31. It will become +ve. I just tried this program.

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

According to the question you asked, i should be incremented, i.e. i++ instead of i--.

@jamie wong: thanx man..learnt a new thing about tht a wraparound....

John M Gant
  • 18,970
  • 18
  • 64
  • 82
aman_novice
  • 1,027
  • 3
  • 13
  • 31
-1

You'll print no dashes. You can either go with Jaime Wong's solution or do this:

for (i = n; i >= 0; i--)

Ari Roth
  • 5,392
  • 2
  • 31
  • 46