Computer Old Farts Forum
 help / color / mirror / Atom feed
* [COFF] Re: [TUHS] Maximum Array Sizes in 16 bit C
@ 2024-09-21  0:22 Douglas McIlroy
  2024-09-23 12:12 ` [COFF] " Ralph Corderoy
  0 siblings, 1 reply; 4+ messages in thread
From: Douglas McIlroy @ 2024-09-21  0:22 UTC (permalink / raw)
  To: COFF, Dan Cross

[-- 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 --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-09-29  0:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAKH6PiWvuxfa_ek=L=n=mgovFk_ms37u6F2N1qcMLs6RXLRWvA@mail.gmail.com>
     [not found] ` <CAEdTPBfPcmZybb0CVLxhzB-nLTQb3aohWV-o_8ZqQJXmUc4yPA@mail.gmail.com>
     [not found]   ` <CAFH29to4_DFdx=U=ZRVY_HE0YGR8tw00Ya4PJhLGR4Z1UMF6_g@mail.gmail.com>
     [not found]     ` <CABH=_VR6=wJdgCLArHLd8q3N+BGj=5m16X4WBkB+o5XksbSsbw@mail.gmail.com>
     [not found]       ` <11d46ab4-b90c-83fe-131a-ee399eebf342@horsfall.org>
2024-09-20 15:56         ` [COFF] Re: [TUHS] Re: Maximum Array Sizes in 16 bit C Stuff Received
2024-09-20 17:15           ` segaloco via COFF
     [not found]         ` <20240920171126.vgtl23xwj37kardb@illithid>
     [not found]           ` <69643008-F7FE-4AC7-8519-B45E4C1CEA66@iitbombay.org>
     [not found]             ` <CANCZdfpy9AsOfE+mvt+DkBYXiwfENLjscMHCT0nnU7WGz54T_Q@mail.gmail.com>
     [not found]               ` <0B54746F-504A-40E0-AFFD-4486167FBAD5@iitbombay.org>
2024-09-29  0:17                 ` [COFF] " Aron Insinga
2024-09-21  0:22 [COFF] Re: [TUHS] " Douglas McIlroy
2024-09-23 12:12 ` [COFF] " Ralph Corderoy

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).