473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

small C puzzles

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.
Nov 14 '05 #1
17 1899

<ma*************@hotmail.com> wrote in message
news:33*************************@posting.google.co m...
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.


Nov 14 '05 #2
In article <10*************@corp.supernews.com>, wi*****@toomuchspam.net
says...

<ma*************@hotmail.com> wrote in message
news:33*************************@posting.google.co m...
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.


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

--
Randy Howard
To reply, remove FOOBAR.
Nov 14 '05 #3
> one solution is to use n-- instead of i--. please let me know the
other two.


One more:

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

Using bitwise negation.
Nov 14 '05 #4
ma*************@hotmail.com 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>

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
On Wed, 01 Sep 2004 17:17:22 +0000, Christopher Benson-Manica wrote:
ma*************@hotmail.com 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>


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
Nov 14 '05 #6
Robert Gamble <ro**********@hotmail.com> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
ma*************@hotmail.com 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>


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
>Subject: Re: small C puzzles
From: Christopher Benson-Manica at***@nospam.cyberspace.org
Date: 9/1/04 7:17 AM Hawaiian Standard Time
Message-id: <ch*********@chessie.cirr.com>

ma*************@hotmail.com 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>


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."
Nov 14 '05 #9

"anonymous" <iu*********@yahoo.co.in> wrote in message
news:f7**************************@posting.google.c om...
one solution is to use n-- instead of i--. please let me know the other two.


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.

Nov 14 '05 #10

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote
in message news:ch*********@chessie.cirr.com...
ma*************@hotmail.com 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>

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames

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?

Nov 14 '05 #11

"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:MP************************@news.verizon.net.. .
In article <10*************@corp.supernews.com>, wi*****@toomuchspam.net says...

<ma*************@hotmail.com> wrote in message
news:33*************************@posting.google.co m...
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;
}


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.


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.


Nov 14 '05 #12
"William L. Bahn" wrote:
.... 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.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #13
In article <10*************@corp.supernews.com>, wi*****@toomuchspam.net
says...

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.


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.

--
Randy Howard
To reply, remove FOOBAR.
Nov 14 '05 #14

"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:MP************************@news.verizon.net.. .
In article <10*************@corp.supernews.com>, wi*****@toomuchspam.net says...
>
> 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.


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.


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?


Nov 14 '05 #15
ma*************@hotmail.com wrote in message news:<33*************************@posting.google.c om>...
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
Nov 14 '05 #16
iu*********@yahoo.co.in (anonymous) wrote in message news:<f7**************************@posting.google. com>...
one solution is to use n-- instead of i--. please let me know the
other two.


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.
Nov 14 '05 #17
In article <10*************@corp.supernews.com>, wi*****@toomuchspam.net
says...
"Randy Howard" <ra*********@FOOverizonBAR.net> wrote in message
news:MP************************@news.verizon.net.. .
In article <10*************@corp.supernews.com>,

wi*****@toomuchspam.net
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?


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.

--
Randy Howard
To reply, remove FOOBAR.
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Wan | last post by:
Hi, I have some puzzles about sgistl source code, that is why some methods are placed in protected field. To vector itself, private and protected are the same, and vector isn't desired to be...
28
by: Robert Gamble | last post by:
I was taking a look at some of the C puzzles at: http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have not had any trouble with any of them except for the first one which is reproduced...
5
by: PM | last post by:
Hello!!!! I am com sci student.... I am really interested in C... Does anybody know any free ebooks or websites for puzzles that test the intricacies of C... Please Help.... Casanova
2
by: xPy | last post by:
do you know any links where i can find puzzles, like the 8 queens problem? (I'm not intrested in the solution, only for puzzle...)
11
by: John Salerno | last post by:
Similar to the Python Challenge, does anyone know of any other websites or books that have programming puzzles to solve? I found a book called "Puzzles for Hackers", but it seems like it might be a...
6
by: Steve Brecher | last post by:
Well, they are puzzles for me, anyway! On a linux/Apache shared host, a working web site has this directory structure (for clarity, each directory name ends with "D"): webD -- the FTP root...
2
by: =?Utf-8?B?Q3JtTmV3Ymll?= | last post by:
Hi, 1) I want to hone my problem solving skills and be good at logic. How do I achieve this? 2) Where can I find c# puzzles or c# algorithms. 3) How do I print the values of a N X N matrix...
25
by: Oltmans | last post by:
Hi guys, I'm learning JavaScript and I need some puzzles that can make me a better JavaScript programmer. I mean I'm looking out for programming puzzles (e.g. Project Euler or TopCoder) but I'm...
62
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.