tags:

views:

449

answers:

9

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;
  }
+20  A: 
 #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
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
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
+8  A: 

Replace i-- with i++.

Kirk Woll
+3  A: 
int main() { 
  int i; 
  int n = 20; 
  for( i = 0; i < n; i++)
    printf("-"); 
  return 0; 
}

You had decrement instead of increment.

James
+1  A: 

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
A: 

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

Why suggest an answer that's bad coding practice?
Jamie Wong
because in other situations you do want to decrement your counter. in this case decrementing the counter doesn't make sense.
@Jamie Creativity ;) One can learn even from something that shouldn't be done a particular way.
Jake Petroules
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
A: 

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
You'll print SO MANY dashes. Also, that's kind of a terrible way to do it. Very much not the norm, and is pointlessly slightly obscure.
Colin DeClue
That won't print no dashes, as everyone else explained.Also, you spelled my name wrong.
Jamie Wong
Whoops, you're right. Sorry about that, on both counts.
Ari Roth
+1  A: 

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
+19  A: 

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!)

Moron
+1 for making this a puzzle. Well thought answer.
Praveen S
The challenge was fun ;) Glad I didn't look at the comments before trying to answer it though.
Jonathan Sternberg
A: 

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

aman_novice