Re: Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki



[excuse the previous double-posting - a glitch in the initial posting
combined with my unfamiliarity with a new test version of the newsreader
that I use led me to believe that the initial post failed and I didn't
wait long enough to check for its non-appearance to confirm this]
On Sat, 20 May 2006 18:26:46 -0700, Mike S wrote:
Netocrat wrote:
Mike S wrote:

I noticed a very slight logic error in the solution to K&R Exercise
1-22 on the the CLC-Wiki, located at

http://www.clc-wiki.net/wiki/KR2_Exercise_1-22

My fix is now reflected at the above address.

Are you going to add your analysis too?

Your fix looks correct to me at least. How about you edit the code on
the wiki and add your analysis to the discussion page?

I've spotted another bug that I won't point out in case readers would
like to find it for themselves.

Your code answering the challenge includes the obvious fix for this bug -
partial confirmation that you have, as you wrote in your previous post,
found the same bug.

The exercise reads as follows:

"Write a program to 'fold' long input lines into two or more shorter
lines after the last non-blank character that occurs before the n-th
column of input. Make sure your program does something intelligent
with very long lines, and if there are no blanks or tabs before the
specified column."

Aside from that, I don't think that overwriting the n-th character with
a newline when a line contains no spaces before the n-th character
conforms to the spirit or the letter of the exercise.

So, a small challenge for c.l.c readers: what's the smallest possible
change that could be made to this code that would preserve all input
characters in the output for over-length words without breaking the
code's existing conformance to the exercise's wording?

To fully complete the challenge, the adjustment should retain the fix
suggested by Mike S as well as fix the other unidentified bug.

Here is my answer to the challenge, though it is only guaranteed to work
if the input is 7-bits-packed-in-8-bits ASCII, because I take advantage
of the sign bit in order to fit the notion of a printable char and a
line-break into one array location when no spaces occur in a line. The
reasoning behind this design choice was that I didn't want to go through
the trouble (and the extra code) of making room in the array for the
line break in this case, so I thought why not squeeze it into a single
char location by letting a set sign bit indicate that a line break is
needed?

Here's an insight that could be used to avoid both choices: nothing in the
exercise requires output to occur in one final step.

I also added a line or two to handle tabs correctly; it assumes a
tabstop of 8 (as the "de facto" tabstop, it's good enough for me...).

That change goes beyond the challenge, but it seems sensible. It would be
useful to make tab width configurable through a macro though.

[...(including code)...]
--
http://members.dodo.com.au/~netocrat
.



Relevant Pages

  • Subterrane v0.194 Alpha Released
    ... system, a character sheet, a ton of new spells, new monsters, item ... Added a character sheet that displays your character's ... Fix: Fixed a bug in the encumbrance calculation and status display ...
    (rec.games.roguelike.announce)
  • [Un] Unangband 0.6.2-wip7b has been released
    ... compilation bug, so that this should be straight forward now. ... etc. a little bit (Mikolaj Konarski) ... Always dump character file at death. ... Fix problems with use_trackmouse causing messages to be lost. ...
    (rec.games.roguelike.angband)
  • [Un] Unangband 0.6.3 released
    ... Allow player to assemble friendly monsters and carry eggs to hatch ... Updated druidic spells to use new region code. ... Fix lockup bugs generating the Old Forest. ... Fix bug where items dropped by monster death would infinitely ...
    (rec.games.roguelike.announce)
  • please pull from the trivial tree
    ... Fix spelling in E1000_DISABLE_PACKET_SPLIT Kconfig description ... +- Finding patch that caused a bug ... +Always try the latest kernel from kernel.org and build from source. ... Length of input string in bytes ...
    (Linux-Kernel)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... If they did not produce a product with *adequate* quality then customers would not buy it and the company would not make a profit. ... it is to change a product in the field, and Y axis is bug density. ... but when the in service fix is almost free to the supplier then they will exploit that to their advantage. ... On-screen programming is pretty much type and ignite and see what ...
    (sci.electronics.design)