48 lines
1.2 KiB
C
48 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define MAX_COEFFICIENTS 100
|
|
|
|
double evaluate_polynomial(double x, double coefficients[], int degree) {
|
|
double result = coefficients[degree];
|
|
for (int i = degree - 1; i >= 0; i--) {
|
|
result = result * x + coefficients[i];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int main() {
|
|
int degree;
|
|
printf("Zadajte stupeň polynómu: ");
|
|
if (scanf("%d", °ree) != 1 || degree < 0) {
|
|
printf("Chyba: Neplatný stupeň polynómu.\n");
|
|
return 1;
|
|
}
|
|
|
|
if (degree >= MAX_COEFFICIENTS) {
|
|
printf("Chyba: Príliš vysoký stupeň polynómu.\n");
|
|
return 1;
|
|
}
|
|
|
|
double coefficients[MAX_COEFFICIENTS];
|
|
printf("Zadajte koeficienty polynómu: ");
|
|
for (int i = 0; i <= degree; i++) {
|
|
if (scanf("%lf", &coefficients[i]) != 1) {
|
|
printf("Chyba: Neplatný koeficient polynómu.\n");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
double x;
|
|
printf("Zadajte hodnotu x: ");
|
|
if (scanf("%lf", &x) != 1) {
|
|
printf("Chyba: Neplatná hodnota x.\n");
|
|
return 1;
|
|
}
|
|
|
|
double result = evaluate_polynomial(x, coefficients, degree);
|
|
printf("Výsledok je: %.2f\n", result);
|
|
|
|
return 0;
|
|
}
|