34 lines
605 B
C
34 lines
605 B
C
#include <stdio.h>
|
|
|
|
int main() {
|
|
double xp, yp = 0, p;
|
|
int i, j, n;
|
|
|
|
printf("Number of inputs: ");
|
|
scanf("%d", &n);
|
|
|
|
double x[n], y[n];
|
|
printf("Input sample space:\n");
|
|
|
|
for (i = 0; i < n; i++) {
|
|
printf("x%d: ", i);
|
|
scanf("%lf", x + i);
|
|
printf("y%d: ", i);
|
|
scanf("%lf", y + i);
|
|
}
|
|
|
|
printf("Enter interpolation point x: ");
|
|
scanf("%lf", &xp);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
p = 1;
|
|
for (j = 0; j < n; j++) {
|
|
if (i != j) {
|
|
p *= (xp - x[j]) / (x[i] - x[j]);
|
|
}
|
|
}
|
|
yp += p * y[i];
|
|
}
|
|
printf("Interpolated value for %lf is %lf.", xp, yp);
|
|
}
|