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. 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.
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.
> 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. 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.
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
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.
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.
>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."
"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.
"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?
"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.
"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?
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.
"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? 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 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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...)
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
| |