Computer Old Farts Forum
 help / color / mirror / Atom feed
From: Douglas McIlroy <douglas.mcilroy@dartmouth.edu>
To: COFF <coff@tuhs.org>, Dan Cross <crossd@gmail.com>
Subject: [COFF] Re: [TUHS] Maximum Array Sizes in 16 bit C
Date: Fri, 20 Sep 2024 20:22:05 -0400	[thread overview]
Message-ID: <CAKH6PiU4rqt1npr8eXcDAZQHvXq+RZauDGzLmG2hM+gnTEQwOA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1570 bytes --]

Moved to Coff, because it's about programming style, not history.

> Perhaps I'm missing something? Clever arithmetic in the index
> calculation aside, this is semantically different than using an actual
> negative integer to index into an array? Moreover, if the intent is to
> start the sequence with 0, why set `fib(0)` to 1? How is this
> substantially different from the usual way of writing this:

I said the Fibonacci example was silly. Maybe you'll be more convinced by
the binomial-coefficient program below.

The array of interest is fib. base is simply scaffolding and doesn't appear
in the working code. You won't find the ith Fibonacci in base[i]; it's in
fib(i). But fib(-1) exists. What's important is that the C convention of
array indexes beginning at 0 has been circumvented.

I could be accused of subterfuge in depending on the semantics of static
storage to initialize fib(-1) to zero. Subterfuge or not, it's customary C
usage. The binomial-coefficient program relies on "out-of-bounds" zeros
abutting two sides of a triangle.

int base[N][N+2];
#define binom(n,i) base[n][(i)+1]

void fill() {
    binom(0,0) = 1;
    for(n=1; n<N; n++)
for(i=0; i<=n; i++)
            binom(n,i) = binom(n-1,i) + binom(n,i-1);
}

I think the offset algorithm above looks better than the more typical one
below.
The two programs happen to have identical character counts.

int binom[N][N+1];

void fill() {
    for(n=0; n<N; n++) {
        binom[n][0] = 1;
for(i=1; i<n; i++)
            binom[n][i] = binom[n-1][i] + binom[n][i-1];
binom[n][n] = 1;
    }
}

Doug

[-- Attachment #2: Type: text/html, Size: 2376 bytes --]

             reply	other threads:[~2024-09-21  0:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-21  0:22 Douglas McIlroy [this message]
2024-09-23 12:12 ` [COFF] " Ralph Corderoy
2024-09-24 16:44 ` [COFF] Re: [TUHS] " Dan Cross
2024-09-21 14:37 Douglas McIlroy
2024-09-23 20:59 Douglas McIlroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKH6PiU4rqt1npr8eXcDAZQHvXq+RZauDGzLmG2hM+gnTEQwOA@mail.gmail.com \
    --to=douglas.mcilroy@dartmouth.edu \
    --cc=coff@tuhs.org \
    --cc=crossd@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).