#include
<
stdio.h
>
#include
<
conio.h
>
#include < alloc.h >
void difference( float * x, float * y, int n)
{ float * f;
int k,i;
f = ( float * )malloc(n * sizeof ( float ));
for (k = 1 ;k <= n;k ++ )
{ f[ 0 ] = y[k];
for (i = 0 ;i < k;i ++ )
f[i + 1 ] = (f[i] - y[i]) / (x[k] - x[i]);
y[k] = f[k];
}
return ;
}
main()
{ int i,n;
float x[ 20 ],y[ 20 ],xx,yy;
printf( " Input n: " );
scanf( " %d " , & n);
if (n >= 20 ) {printf( " Error! The value of n must in (0,20). " ); getch(); return 1 ;}
if (n <= 0 ) {printf( " Error! The value of n must in (0,20). " );getch(); return 1 ;}
for (i = 0 ;i <= n - 1 ;i ++ )
{ printf( " x[%d]: " ,i);
scanf( " %f " , & x[i]);
}
printf( " /n " );
for (i = 0 ;i <= n - 1 ;i ++ )
{ printf( " y[%d]: " ,i);scanf( " %f " , & y[i]);}
printf( " /n " );
difference(x,( float * )y,n);
printf( " Input xx: " );
scanf( " %f " , & xx);
yy = y[ 20 ];
for (i = n - 1 ;i >= 0 ;i -- ) yy = yy * (xx - x[i]) + y[i];
printf( " NewtonInter(%f)=%f " ,xx,yy);
getch();
}
#include < alloc.h >
void difference( float * x, float * y, int n)
{ float * f;
int k,i;
f = ( float * )malloc(n * sizeof ( float ));
for (k = 1 ;k <= n;k ++ )
{ f[ 0 ] = y[k];
for (i = 0 ;i < k;i ++ )
f[i + 1 ] = (f[i] - y[i]) / (x[k] - x[i]);
y[k] = f[k];
}
return ;
}
main()
{ int i,n;
float x[ 20 ],y[ 20 ],xx,yy;
printf( " Input n: " );
scanf( " %d " , & n);
if (n >= 20 ) {printf( " Error! The value of n must in (0,20). " ); getch(); return 1 ;}
if (n <= 0 ) {printf( " Error! The value of n must in (0,20). " );getch(); return 1 ;}
for (i = 0 ;i <= n - 1 ;i ++ )
{ printf( " x[%d]: " ,i);
scanf( " %f " , & x[i]);
}
printf( " /n " );
for (i = 0 ;i <= n - 1 ;i ++ )
{ printf( " y[%d]: " ,i);scanf( " %f " , & y[i]);}
printf( " /n " );
difference(x,( float * )y,n);
printf( " Input xx: " );
scanf( " %f " , & xx);
yy = y[ 20 ];
for (i = n - 1 ;i >= 0 ;i -- ) yy = yy * (xx - x[i]) + y[i];
printf( " NewtonInter(%f)=%f " ,xx,yy);
getch();
}