pvjc24/cv3/program.c
2024-03-08 08:21:46 +00:00

38 lines
927 B
C

#include <stdio.h>
#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];
}
return result;
}
int main() {
double x, coef;
double coefficients[MAX_COEFFICIENTS];
int count = 0;
printf("Enter the value of x: ");
if (scanf("%lf", &x) != 1) {
printf("Invalid input for x.\n");
return 1;
}
printf("Enter coefficients from highest to lowest degree, finish with an empty line:\n");
while (scanf("%lf", &coef) == 1) {
if (count >= MAX_COEFFICIENTS) {
printf("Maximum number of coefficients exceeded.\n");
return 1;
}
coefficients[count++] = coef;
}
double result = evaluatePolynomial(x, coefficients, count);
printf("Vysledok je: %.2f\n", result);
return 0;
}