473,395 Members | 1,464 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,395 software developers and data experts.

some C puzzles

I was going through the following code:

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 sol is to use n-- instead of i, so it will print minus sign 20
times.
Nov 14 '05 #1
10 1586
In <33*************************@posting.google.com> ma*************@hotmail.com writes:
I was going through the following code:

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.


This is not possible, in general, because the code must output a newline
character after the last minus sign (otherwise it might not produce any
output at all) and this cannot be achieved by changing exactly one
character.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #2
Ash
1. we can change i-- to i++
2. we can change i-- to n--
3. ??
Da*****@cern.ch (Dan Pop) wrote in message news:<ch***********@sunnews.cern.ch>...
In <33*************************@posting.google.com> ma*************@hotmail.com writes:
I was going through the following code:

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.


This is not possible, in general, because the code must output a newline
character after the last minus sign (otherwise it might not produce any
output at all) and this cannot be achieved by changing exactly one
character.

Dan

Nov 14 '05 #3
Please don't top post. Look at the posts within the last day or so for
the arguments against it.

On 2 Sep 2004 01:21:16 -0700
am****@yahoo.com (Ash) wrote:
Da*****@cern.ch (Dan Pop) wrote in message
news:<ch***********@sunnews.cern.ch>...
In <33*************************@posting.google.com>
ma*************@hotmail.com writes:
I was going through the following code:

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.
This is not possible, in general, because the code must output a
newline character after the last minus sign (otherwise it might not
produce any output at all) and this cannot be achieved by changing
exactly one character.

1. we can change i-- to i++
That is changing 2 characters.
2. we can change i-- to n--
3. ??


Neither of your changes causes a new line to be output before the
program terminates. As Dan correctly stated (and he is generally correct
even if some find him abrasive) programs must output a newline before
terminating if they want the last line to be guaranteed to be output.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4
ma*************@hotmail.com wrote in message news:<33*************************@posting.google.c om>...
I was going through the following code:

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 sol is to use n-- instead of i, so it will print minus sign 20
times.


simple one...
for( i = 0; i + n; i-- )

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

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

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

4 is enough, but why do you want this ;-)

-Paul.
Nov 14 '05 #5
Flash Gordon <sp**@flash-gordon.me.uk> wrote in message news:<70************@brenda.flash-gordon.me.uk>...
Please don't top post. Look at the posts within the last day or so for
the arguments against it.

On 2 Sep 2004 01:21:16 -0700
am****@yahoo.com (Ash) wrote:
Da*****@cern.ch (Dan Pop) wrote in message
news:<ch***********@sunnews.cern.ch>...
In <33*************************@posting.google.com>
ma*************@hotmail.com writes:

>I was going through the following code:
>
>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.

This is not possible, in general, because the code must output a
newline character after the last minus sign (otherwise it might not
produce any output at all) and this cannot be achieved by changing
exactly one character.

1. we can change i-- to i++


That is changing 2 characters.
2. we can change i-- to n--
3. ??


Neither of your changes causes a new line to be output before the
program terminates. As Dan correctly stated (and he is generally correct
even if some find him abrasive) programs must output a newline before
terminating if they want the last line to be guaranteed to be output.

hi all,
thanks for ur help. but i can't get what flash has said. when
the c program exits, all the file handles are closed. this causes them
to be autoflushed. so, even if we don't explicitly print the newline,
the code should work. this is what i think.but please clarify me on
this issue.
Madhav.
Nov 14 '05 #6
ma*************@hotmail.com writes:
[...]
thanks for ur help. but i can't get what flash has said. when
the c program exits, all the file handles are closed. this causes them
to be autoflushed. so, even if we don't explicitly print the newline,
the code should work. this is what i think.but please clarify me on
this issue.


C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

C90 7.9.2p2 says the same thing.

--
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 #7
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
ma*************@hotmail.com writes:
[...]
thanks for ur help. but i can't get what flash has said. when
the c program exits, all the file handles are closed. this causes them
to be autoflushed. so, even if we don't explicitly print the newline,
the code should work. this is what i think.but please clarify me on
this issue.
C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

C90 7.9.2p2 says the same thing.


Concrete example:

fangorn:~/tmp 85> zsh
[fangorn] ~/tmp % prompt='> ' cat test.c #include <stdio.h>

int main()
{
printf("Hello world");
return 0;
} gcc test.c
./a.out


I have explicitly set the prompt myself, to show that no magic was used
inside to clear the line before displaying the prompting text. Other
Unix shells behave differently, the last line of program output being
concatened with the prompting text, giving something like:

fangorn:~/tmp 86> ./a.out
Hello worldfangorn:~/tmp 87>

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
Da*****@cern.ch (Dan Pop) writes:
[...]
Concrete example:

fangorn:~/tmp 85> zsh
[fangorn] ~/tmp % prompt='> '
> cat test.c

#include <stdio.h>

int main()
{
printf("Hello world");
return 0;
}
> gcc test.c
> ./a.out
>


I have explicitly set the prompt myself, to show that no magic was used
inside to clear the line before displaying the prompting text.


What's really happening here (or at least what happened when I tried
the same thing) is that the program prints "Hello world", and the
shell then clears the line and writes its prompt in the same location.
A sufficiently perverse shell could as easily erase the output of
printf("Hello world\n"); (just as all the output of a console-based
program is typically erased on Windows unless you take steps to keep
the window open). As far as the C implementation is concerned, the
trailing newline is not required; the output appears anyway.

But it's certainly another good reason to provide a newline at the end
of your output.

--
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 #9
bg*****@yahoo.com (Paul) wrote:
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.
for( i = 0; ~i < n; i-- )


As has been mentioned before (but not on this thread), this
one doesn't work on 2's complement systems (which is the vast
majority of all conforming systems).
Nov 14 '05 #10
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
Da*****@cern.ch (Dan Pop) writes:
[...]
Concrete example:

fangorn:~/tmp 85> zsh
[fangorn] ~/tmp % prompt='> '
> cat test.c

#include <stdio.h>

int main()
{
printf("Hello world");
return 0;
}
> gcc test.c
> ./a.out
>


I have explicitly set the prompt myself, to show that no magic was used
inside to clear the line before displaying the prompting text.


What's really happening here (or at least what happened when I tried
the same thing) is that the program prints "Hello world", and the
shell then clears the line and writes its prompt in the same location.


Which, from the user's point of view, is undistinguishable from not having
the last line displayed at all, as long as stdout is connected to an
interactive terminal.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

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...
17
by: madhav_a_kelkar | last post by:
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() {...
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...
3
by: Sambo | last post by:
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print...
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...
0
by: prabhabalakrishnan | last post by:
Hi friends, I m prabha,i m new to this forum.......i m very interested in solving C puzzles......i dont know any links which has a collection of cpuzzles along with their...
1
by: jcarthur | last post by:
Does anyone know the solution to the following Rebus puzzles? SOMething and M M A P
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.