Hi Doug,
int base[N][N+2];
#define binom(n,i) base[n][(i)+1]
Thanks for the interesting prompt. I've been having a think about it
along the lines of sliding the ints in memory.
Have you considered having a single dimension of ints and then accessing
them as if a two-dimensional array, with [0][0] being offset?
$ cat offset.c
#include <stdio.h>
#define N 4 // N×N values
#define L 3 // L columns of 0 to the left
#define U 2 // U columns of 0 upwards
int main()
{
char m[(U + N) * (L + N)] =
"abcdefg"
"hijklmn"
"opq+BCD" // The '+' is a[0][0].
"rstEFGH"
"uvwIJKL"
"xyzMNOP";
char (*a)[L + N] = (char (*)[L + N])&m[U * (L + N) + L];
for (int y = -U; y < N; y++) {
if (!y)
putchar('\n');
for (int x = -L; x < N; x++) {
if (!x)
putchar(' ');
putchar(a[y][x]); // y,x relative to ‘+’
}
putchar('\n');
}
}
$ gcc -Wall -Wextra -Werror offset.c
$ ./a.out
abc defg
hij klmn
opq +BCD
rst EFGH
uvw IJKL
xyz MNOP
$
--
Cheers, Ralph.