Re: C Programming: A Modern Approach - Chapter 15 Exercise 5
- From: richard@xxxxxxxxxxxxxxx (Richard Tobin)
- Date: 22 Jul 2005 12:22:30 GMT
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
.
- Follow-Ups:
- Re: C Programming: A Modern Approach - Chapter 15 Exercise 5
- From: Tim Rentsch
- Re: C Programming: A Modern Approach - Chapter 15 Exercise 5
- References:
- C Programming: A Modern Approach - Chapter 15 Exercise 5
- From: Simon Morgan
- C Programming: A Modern Approach - Chapter 15 Exercise 5
- Prev by Date: Re: Help on an "if" statement on a numerical value
- Next by Date: Re: List of all functions in a C files
- Previous by thread: Re: C Programming: A Modern Approach - Chapter 15 Exercise 5
- Next by thread: Re: C Programming: A Modern Approach - Chapter 15 Exercise 5
- Index(es):
Relevant Pages
|