C Puzzle!

C Puzzle!

SuperJer
Websiteman

2005 Mar 21 • 6666
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 23:30 UTC — Ed. 2009 Jul 5 at 20:14 UTC
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 2 at 00:09 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
*** 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 2 at 00:18 UTC
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 2 at 00:25 UTC
Killer-Duck
Homicidal Anatidae

2008 Mar 5 • 1169
633 ₧

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


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

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

2005 Mar 21 • 6666
*** You have found 2 of the 4 solutions. ***

Good job killer.

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

2008 Aug 1 • 3081
-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 4 at 01:13 UTC
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 11:00 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
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 19:50 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Jul 6 at 16:44 UTC
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 15:18 UTC — Ed. 2009 Jul 9 at 15:19 UTC
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 16:10 UTC
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 15:48 UTC — Ed. 2010 Jan 16 at 15:49 UTC
Down Rodeo
Cap'n Moth of the Firehouse

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

2005 Mar 21 • 6666
It took a while but you got it. GG Mate Davate.
 
 
 
2010 Jan 23 at 02:28 UTC
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 3 at 02:34 UTC — Ed. 2012 Apr 3 at 02:35 UTC
Down Rodeo
Cap'n Moth of the Firehouse

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

2005 Mar 21 • 6666
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 23:57 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
It's only 30-odd keystrokes!

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

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

http://ideone.com/bNIRE
 
 
 
2012 May 25 at 00:45 UTC
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 26 at 01:02 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
That's what IDEONE is for!

 
 
 
2012 May 30 at 22:50 UTC
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 12:54 UTC — Ed. 2012 Jun 23 at 13:02 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
By jove I think you've got it!
 
 
 
2012 Jun 23 at 21:33 UTC
Page [1]