Monday 20 December 2010

Program to implement the Newton Gregory forward interpolation

Program:

#include<stdio.h>
#include<math.h>
void main()
{
int  i, j, n, k, l;
float sumy, h, term, p, z, pvalue;
float x[25], y[25], d[25][25], factvalue; printf(“enter the value of n”);
scanf(“%d”,&n);
printf(“enter %d values for x, y \n”,n); for(i=0;i<n;i++)
scanf(“%f %f”, &x[i], &y[i]);
printf(“\n enter z”);
scanf(“%f”,&z);
h = x[1] – x[0];
p = (z - x[0] )/ h;

    for(j=0; j<n-2; j++)
    d[i][j] =y[j+1] – y[j];
    k=n-2;
for(i=2; i<n; i++)
{
k++;
for(j=0; j<=k; j++)
d[i][j] =d[i-1][j+1] – d[i-1][j];
}
for(l=1; l<n; l++)
{
pvalue *= (p-(l - 1));
factvalue *= 1;
term = pvalue* d[l][0] / factvalue;
sumy += term;
}
printf(“\n y value at z = %f is %f”, z, sumy);
getch();
}



Input/Output:


Enter n 7
Enter 7 data values for x, y

1921 35
1931  42
1941 58
1951 84
1961 120
1971  165
1981 220

Enter z 1925
Y value at z = 1925.000000 is 36.756710

Conclusion:

 The program is error free


VIVA QUESTIONS

1) What is the use of goto statement ?
Ans: The goto statement is used to alter the normal sequence of the program execution by unconditionally transferring control to some other part of the program.


2) What is the use of continue statement ?
Ans: The continue statement is used to bypass the remainder of the current pass through a loop unconditional statements


Algorithm


Data structures using Java

No comments:

Post a Comment