pvjc24/cv3/program.c

37 lines
792 B
C
Raw Normal View History

2024-03-08 08:17:54 +00:00
#include <stdio.h>
2024-03-08 08:21:46 +00:00
#define MAX_COEFFICIENTS 100
double evaluatePolynomial(double x, double coefficients[], int n) {
double result = 0.0;
for (int i = 0; i < n; ++i) {
result = result * x + coefficients[i];
2024-03-08 08:17:54 +00:00
}
return result;
}
int main() {
2024-03-08 08:21:46 +00:00
double x, coef;
double coefficients[MAX_COEFFICIENTS];
int count = 0;
2024-03-08 08:25:05 +00:00
if (scanf("%lf", &x) != 1) {
2024-03-08 08:29:31 +00:00
printf("Nepodarilo sa nacitat zaklad x\n");
2024-03-08 08:27:27 +00:00
return 0;
2024-03-08 08:17:54 +00:00
2024-03-08 08:21:46 +00:00
while (scanf("%lf", &coef) == 1) {
coefficients[count++] = coef;
2024-03-08 08:17:54 +00:00
}
2024-03-08 08:29:31 +00:00
2024-03-08 08:25:05 +00:00
if (!feof(stdin)) {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1);
2024-03-08 08:27:27 +00:00
return 0;
2024-03-08 08:25:05 +00:00
}
2024-03-08 08:21:46 +00:00
double result = evaluatePolynomial(x, coefficients, count);
printf("Vysledok je: %.2f\n", result);
2024-03-08 08:17:54 +00:00
2024-03-08 08:30:16 +00:00
return 1;
2024-03-08 08:17:54 +00:00
}