Files
cmlab/5.c
Amneesh Singh be590b5cfd exp 1-6: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-26 19:14:30 +05:30

43 lines
708 B
C

#include <stdio.h>
int main() {
int n, i, j = 1;
double xp, yp, f1, f2 = 0;
printf("Enter the number of inputs: ");
scanf("%d", &n);
double x[n], y[n];
printf("Enter input values:\n");
for (i = 0; i < n; i++) {
printf("x%d=", i);
scanf("%lf", x + i);
printf("y%d=", i);
scanf("%lf", y + i);
}
yp = y[0];
printf("Enter interpolation point x: ");
scanf("%lf", &xp);
do {
for (i = 0; i < n - 1; i++)
y[i] = ((y[i + 1] - y[i]) / (x[i + j] - x[i]));
f1 = 1;
for (i = 0; i < j; i++) {
f1 *= (xp - x[i]);
}
f2 += (y[0] * f1);
j++;
} while ((--n) > 1);
yp += f2;
printf("Interpolated value for %lf is %lf.", xp, yp);
}