Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Extrapolation Using a Difference Table

326.c

#include <stdio.h>

int n;
int k;
int mat[10][10];

int
main(void) {
	int i,j;

	while (1) {
		scanf("%d", &n);
		if (n==0) {
			return 0;
		}
		for (i=0; i<n; i++) {
			scanf("%d", &mat[i][0]);
		}
		scanf("%d", &k);
		for (j=1; j<n; j++) {
			for (i=j; i<n; i++) {
				mat[i][j] = mat[i][j-1] - mat[i-1][j-1];
			}
		}

#if DEBUG
		for (i=0; i<n; i++) {
			for (j=0; j<n; j++) {
				printf(" %3d", mat[i][j]);
			}
			printf("\n");
		}
		printf("\n");
#endif

		for (i=0; i<k; i++) {
			for (j=n-2; j>=0; j--) {
				mat[n-1][j] += mat[n-1][j+1];
			}
		}
		printf("Term %d of the sequence is %d\n", n+k, mat[n-1][0]);
	}
}