C examples for Data Structure:Algorithm
To Construct the New Data Points Using Newton's Forward Method of Interpolation
#include<stdio.h> #define MAX 20/*from ww w.j a va 2 s . co m*/ void main() { float ax[MAX], ay[MAX], diff[MAX][5]; float nr = 1.0, dr=1.0, x, p, h, yp; int terms = 10, i, j, k; printf("\nEnter the values of x upto 2 decimal points.\n"); for (i=0; i<terms; i++) { printf("Enter the value of x%d: ", i+1); scanf("%f",&ax[i]); } printf("\nNow enter the values of y upto 4 decimal points.\n"); for (i=0; i<terms; i++) { printf("Enter the value of y%d: ", i+1); scanf("%f", &ay[i]); } x = 20; h = ax[1] - ax[0]; for (i = 0; i < terms-1; i++) diff[i][1] = ay[i+1] - ay[i]; for (j=2; j<=4; j++) for(i=0; i<=terms-j; i++) diff[i][j] = diff[i+1][j-1] - diff[i][j-1]; i=0; do { i++; } while (ax[i] < x); i--; p = (x - ax[i]) / h; yp = ay[i]; for (k=1; k <= 4; k++) { nr *= p - k + 1; dr *= k; yp += (nr/dr) * diff[i][k]; } printf("\nFor x = %6.2f, y = %6.4f",x,yp); }