Re: C Programming: A Modern Approach - Chapter 15 Exercise 5



In article <pan.2005.07.22.09.48.30.184627@xxxxxxxxxxx>,
Simon Morgan <me@xxxxxxxxxxx> wrote:

>The task is to modify it so that it alternates between distribution of the
>extra spaces favouring the end of the line and the beginning of the line.

It's not really answering your question, but this is an example of the
large class of problems that can be solved with something equivalent
to Bressenham's algorithm. That algorithm is traditionally used for
drawing straight lines on a pixel display, but can be used for all
kinds of problems where you want to approximate division using integer
increments.

In this case, suppose you want to divide 8 spaces among 5 gaps. Start
with zero. At each gap, then add 8 and subtract off as many 5s as you
can, and for each one add a space to that gap. Then proceed to the
next gap using the remainder as the starting value.

So we have

0+8 = 8 = 1*5 + 3 => 1 space
3+8 = 11 = 2*5 + 1 => 2 spaces
1+8 = 9 = 1*5 + 4 => 1 space
4+8 = 12 = 2*5 + 2 => 2 spaces
2+8 = 10 = 2*5 => 2 spaces

-- Richard
.



Relevant Pages

  • Re: [PATCH] TX algorithms, missetting IFF_OACTIVE and if_timer
    ... less driver code when converting to use the precomputed pointers, ... but this gap is part of the existing algorithm. ... FreeBSD committer ...
    (freebsd-net)
  • Yet Another Factoring Algorithm (yafa)
    ... I would like to have comments on the following algorithm. ... mathematica/maple lingo. ... ranges through the set of prime numbers (until a certain ... Here is the code (in GAP): ...
    (sci.math)
  • Re: C Programming: A Modern Approach - Chapter 15 Exercise 5
    ... > to Bressenham's algorithm. ... > increments. ... At each gap, then add 8 and subtract off as many 5s as you ...
    (comp.lang.c)
  • Re: Most valuable poster
    ... threshold requirements of various types of functions. ... mechanisms to achieve - because of the non-beneficial gap problem. ... are pretty much the same distance away from all other ... Ooh, the Poisson distribution! ...
    (talk.origins)
  • Re: Most valuable poster
    ... threshold requirements of various types of functions. ... mechanisms to achieve - because of the non-beneficial gap problem. ... are pretty much the same distance away from all other ... Ooh, the Poisson distribution! ...
    (talk.origins)