small C puzzles

  • Thread starter madhav_a_kelkar
  • Start date
M

madhav_a_kelkar

i was browsing this problem:

The following is a piece of C code, whose intention was to print a
minus sign 20 times. But you can notice that, it doesn't work.

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

Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are three known solutions. See if you can get all
those three.


one solution is to use n-- instead of i--. please let me know the
other two.
 
W

William L. Bahn

i was browsing this problem:

The following is a piece of C code, whose intention was to print a
minus sign 20 times. But you can notice that, it doesn't work.

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

Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are three known solutions. See if you can get all
those three.


one solution is to use n-- instead of i--. please let me know the
other two.

Sure....Tell the obvious one...;)

One is to change i < n to -i < n

Using ~i will almost work - and would on an implementation that
uses one's compliment.

The other is to change the test to i + n

Good little logic puzzle.
 
R

Randy Howard

Sure....Tell the obvious one...;)

One is to change i < n to -i < n

Using ~i will almost work - and would on an implementation that
uses one's compliment.

The other is to change the test to i + n

Good little logic puzzle.

Except, as Dan pointed out in the "earlier" version of this same thing
it still is guaranteed to work in the form above.
 
C

Christopher Benson-Manica

(e-mail address removed) spoke thus:
Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are three known solutions. See if you can get all
those three.

<pedantry>Actually, as stated, the problem is impossible - the
solutions involve *inserting* a character; I read "changing" to be an
operation that can be done in replace mode in one's editor of
choice.</pedantry>
 
R

Robert Gamble

(e-mail address removed) spoke thus:


<pedantry>Actually, as stated, the problem is impossible - the
solutions involve *inserting* a character; I read "changing" to be an
operation that can be done in replace mode in one's editor of
choice.</pedantry>

Which of the 3 solutions requires inserting a character instead of
changing a chracater, or can your editor not change a space character to
something else?

Rob Gamble
 
C

Christopher Benson-Manica

Robert Gamble said:
Which of the 3 solutions requires inserting a character instead of
changing a chracater, or can your editor not change a space character to
something else?

Being pedantic is no fun when you turn out to be wrong... although
changing a space to a non-space would destroy the stylistic unity of
the program, so I'll take a smidgen of solace from that. Sorry.
 
K

Keith Thompson

Christopher Benson-Manica said:
(e-mail address removed) spoke thus:


<pedantry>Actually, as stated, the problem is impossible - the
solutions involve *inserting* a character; I read "changing" to be an
operation that can be done in replace mode in one's editor of
choice.</pedantry>

The solutions involving changing "i" to "-i" or "~i" can be done by
replacing the preceding space.
 
B

Bigdakine

Subject: Re: small C puzzles
From: Christopher Benson-Manica (e-mail address removed)
Date: 9/1/04 7:17 AM Hawaiian Standard Time
Message-id: <[email protected]>

(e-mail address removed) spoke thus:


<pedantry>Actually, as stated, the problem is impossible - the
solutions involve *inserting* a character; I read "changing" to be an
operation that can be done in replace mode in one's editor of
choice.</pedantry>

Now you tell me.

Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up requires a creationist"


"Creationists aren't impervious to Logic: They're oblivious to it."
 
W

William L. Bahn

anonymous said:
One more:

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

Using bitwise negation.

On the vast majority of machines, this will not produce the
intended results since bitwise negation and multiplying by
negative one are not the same thing.
 
W

William L. Bahn

Christopher Benson-Manica said:
(e-mail address removed) spoke thus:


<pedantry>Actually, as stated, the problem is impossible - the
solutions involve *inserting* a character; I read "changing" to be an
operation that can be done in replace mode in one's editor of
choice.</pedantry>
welcome.

Huh?

Can't changing i-- to n-- can be done in replace mode?
Can't replacing a blank space with a minus sign be done in
replace mode?
Can't replacing a < sign with a + sign be done in replace mode?
 
W

William L. Bahn

Randy Howard said:
says...

Except, as Dan pointed out in the "earlier" version of this same thing
it still is guaranteed to work in the form above.

I guess I'm a bit dense, but when told that the purpose of a
piece of code is to print a minus sign 20 times I take that to
mean that printing it only 19 times or 21 times or 32769 times
doesn't qualify as working.

And, are the signed integers guaranteed to wrap around? The
unsigned ones are, but are the signed ones? Would anything
prevent the expression INT_MIN - 1 from evaluating to INT_MIN? I
don't have the standard handy.
 
C

CBFalconer

William L. Bahn said:
.... snip ...

And, are the signed integers guaranteed to wrap around? The
unsigned ones are, but are the signed ones? Would anything
prevent the expression INT_MIN - 1 from evaluating to INT_MIN? I
don't have the standard handy.

No. That is overflow and undefined behaviour.
 
R

Randy Howard

I guess I'm a bit dense, but when told that the purpose of a
piece of code is to print a minus sign 20 times I take that to
mean that printing it only 19 times or 21 times or 32769 times
doesn't qualify as working.

Sorry, I made a rather bad typo above. It should read
"not guaranteed to work". Which, would match Dan's post.
 
W

William L. Bahn

Randy Howard said:
says...

Sorry, I made a rather bad typo above. It should read
"not guaranteed to work". Which, would match Dan's post.

So then is "the form above" referring to the original code or one
of the three proposed solutions? If so, which one? Why wouldn't
it work?
 
B

ByteSurfer

i was browsing this problem:

The following is a piece of C code, whose intention was to print a
minus sign 20 times. But you can notice that, it doesn't work.

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

Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are three known solutions. See if you can get all
those three.


one solution is to use n-- instead of i--. please let me know the
other two.

Sure it was obvious that the looping you do was always will decrement
by one so . how on earth will be the looping exit when all decrement
of 1 from zero will become more negative . so. just by letting the n
to be n-- will work.

if not you can think of putting the condition to be n<=i then the
updating you put the increment of n++ then surely at one time the the
n will bigger than the value i so it will be exiting the loop.

If not you can try this method. of putting negative in front of n or
even i.
But this was not that practical, cos it just will make your coding
more complicated. So, poosibly stick to the more simple alogarithm.

Regard,
ByteSurfer
 
B

ByteSurfer

One more:

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

Using bitwise negation.

GOod... this also can work. revesing the i to positive so that it will
test all the positive and compare to the n in positive form.
 
R

Randy Howard

Randy Howard said:
So then is "the form above" referring to the original code or one
of the three proposed solutions? If so, which one? Why wouldn't
it work?

I no longer have the thread in my newsreader, but it referred
specifically to the lack of '\n' or fflush(stdout) in the code,
IIRC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top