C Puzzle!

C Puzzle!

SuperJer
Websiteman

2005 Mar 20 • 6299
This code is intended to print 20 minus signs, but it does not work:

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


There are at least 4 ways to fix it by changing exactly one character. Find!
 
 
2009 Jul 1 at 16:30 PDT — Ed. 2009 Jul 5 at 13:14 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Change the i-- to n--? I don't know why that's a question, it works. But it should return an int :p

Not so sure what the others are; I shall have a little more think-time.
 
 
2009 Jul 1 at 17:09 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
*** You have found 1 of the 4 solutions. ***

This is just a puzzle, don't worry about compiler warnings, etc. You don't have to return an int to get it to compile, anyway.
 
 
2009 Jul 1 at 17:18 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Ya, it compiled warning-free, which surprised me. I expected it to complain about my lack of a newline but nope, nothing.
 
 
2009 Jul 1 at 17:25 PDT
Killer-Duck
Homicidal Anatidae

2008 Mar 5 • 1169
633 ₧

...
} while ( -i < n );


Maybe?
QUACK! QUACK!
 
 
2009 Jul 2 at 13:18 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2009 Jul 3 at 14:15 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
*** You have found 2 of the 4 solutions. ***

Good job killer.

Yes, you can change a white-space character. But only ONE!
 
 
2009 Jul 3 at 17:29 PDT
the_cloud_system
polly pushy pants

2008 Aug 1 • 3074
-6 ₧
ok im just gona ask
what
how
txt doc?
i dont know
can you explane
I drink to forget but I always remember.
 
 
2009 Jul 3 at 18:13 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
It helps if you understand the basics of imperative programming. You don't even really need a compiler, thinking through it ought to work.

Uhm, changing the assignment n =-20; might work?
 
 
2009 Jul 5 at 04:00 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
Down Rodeo said:
Uhm, changing the assignment n =-20; might work?


that will only print one minus sign because i is never less than n.
 
 
2009 Jul 5 at 12:50 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2009 Jul 6 at 09:44 PDT
thegreat
2009 Jul 9 • 1
WOW the most funny and newbie c puzzle ever...the increment shuld be
i++;

or make

.
.
.
int i=20,n=0;
do{
.
.
}while(i>n);
 
 
2009 Jul 9 at 08:18 PDT — Ed. 2009 Jul 9 at 08:19 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
But you see, thenotsogreat, neither of those involves changing exactly one character, which means you haven't solved the puzzle, friend.
 
 
2009 Jul 9 at 09:10 PDT
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
code
} while ( i + n );

I don't remember how exactly the while check works (is 0 true or false?), but would this work?
Anyway just bumping this truck.
...and that's the bottom line because Mate de Vita said so.
 
 
2010 Jan 16 at 07:48 PST — Ed. 2010 Jan 16 at 07:49 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2010 Jan 16 at 09:28 PST
SuperJer
Websiteman

2005 Mar 20 • 6299
It took a while but you got it. GG Mate Davate.
 
 
2010 Jan 22 at 18:28 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
So you've resparked my interest (at 3 am, you bas), and I'm not sure I'll ever get the answer. Because: can't change i--; one change is a solution and anything else is not an operator. Can't change the assignment, that's a two-character change. Can't change the assignment of i to anything useful (largest is 9). Can't change the type of loop (obviously). Can't change the loop boundaries (not that I think it would help). Can't change the thing it's printing to anything useful.

Ah...

Legit, you made me break out the pens. I want to say that changing the < to & will work, but it might actually print out 19 or 21 minus signs, I think. It's late! We want the pattern
code
0 0 0 1 0 1 0 0 &
1 1 1 0 1 0 1 1

That means i = -21, so there should be 21 minus signs printed. I am probably wrong somewhere so please let me know!
 
 
2012 Apr 2 at 19:34 PDT — Ed. 2012 Apr 2 at 19:35 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2012 May 22 at 16:14 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
You're right that it prints 21 minus signs.

Are you really not just running it to see??

shell code
jwilson@snickerdoodle ~$ cat >puz.c
#include <stdio.h>
main()
{
int i = 0, n = 20;
do
{
printf("-");
i--;
} while ( i & n );
}
^D
jwilson@snickerdoodle ~$ make puz
cc puz.c -o puz
jwilson@snickerdoodle ~$ ./puz | wc
0 1 21
 
 
2012 May 24 at 16:57 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
It's only 30-odd keystrokes!

cat >puz.c^J^V^Dmake puz^J./puz | wc^J
 
 
2012 May 24 at 16:59 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
Or a paste and a click on I.D.-yo-knee!

http://ideone.com/bNIRE
 
 
2012 May 24 at 17:45 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
I'm on Windows when I visit your site. Sadly, my laptop is poorly right now, as it has a case of the dead fan. So no I'm really not running the code :p
 
 
2012 May 25 at 18:02 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
That's what IDEONE is for!

 
 
2012 May 30 at 15:50 PDT
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
I went through multiple bitwise operators, such as left shift (infinite loop) - you cannot do right shift without changing more than one character, bitwise or and xor (infinite loop and no output), and even tried the unary operator ~ (which, like DR's solution, prints 21 minuses).

But in the end, this:
code
} while ( i % n );
appears to print 20 minuses.
...and that's the bottom line because Mate de Vita said so.
 
 
2012 Jun 23 at 05:54 PDT — Ed. 2012 Jun 23 at 06:02 PDT
SuperJer
Websiteman

2005 Mar 20 • 6299
By jove I think you've got it!
 
 
2012 Jun 23 at 14:33 PDT
Page [1]