473,657 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1925

<ma************ *@hotmail.com> wrote in message
news:33******** *************** **@posting.goog le.com...
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.supernew s.com>, wi*****@toomuch spam.net
says...

<ma************ *@hotmail.com> wrote in message
news:33******** *************** **@posting.goog le.com...
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>Actua lly, 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)cybers pace.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>Actua lly, 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**********@h otmail.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
Christopher Benson-Manica <at***@nospam.c yberspace.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>Actua lly, 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_Keit h) 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.cy berspace.org
Date: 9/1/04 7:17 AM Hawaiian Standard Time
Message-id: <ch*********@ch essie.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>Actu ally, 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"
"Creationis ts aren't impervious to Logic: They're oblivious to it."
Nov 14 '05 #9

"anonymous" <iu*********@ya hoo.co.in> wrote in message
news:f7******** *************** ***@posting.goo gle.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.


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

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

Similar topics

3
1653
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 the base class of other containers. So why not use private instead of protected?
28
2840
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 below: The following C program segfaults of IA-64, but works fine on IA-32. int main() { int* p; p = (int*)malloc(sizeof(int));
5
5274
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
1653
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
2207
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 little advanced for me, and I've also read that it focuses too much on encryption and security issues and doesn't really have coding problems, exactly. But something like that book would be fun to have.
6
1448
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 ......htdocsD -- the DOCUMENT_ROOT ...........index.html ...........topicsD ................topic1.html
2
5164
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 using c#. 1 2 3 4 5 6 7 8
25
5448
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 looking out for language specific puzzles that can make me a top-notch JavaScript programmer. a) Any puzzles you can recommend? b) Any programs that you can suggest that can make me learn JavaScript internals in greatest depth. Please recommend...
62
12192
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 ones that my program cannot solve. However, most hard puzzles in the newspapers are solved within 2-3 cycles so far. The ones I've found on google which are said to be hard, get solved in 3 cycles. Any help would be very much appreciated!...
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.