This commit is contained in:
Rudolf Zambory 2025-03-07 11:10:32 +01:00
parent 381896727e
commit 39fdd51cc9

39
du2/program.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define LINE_SIZE 100
int horner(int coeffs[], int n, int x) {
int result = coeffs[0];
for (int i = 1; i < n; i++) {
result = result * x + coeffs[i];
}
return result;
}
int main() {
char line[LINE_SIZE];
memset(line, 0, LINE_SIZE);
char* r = fgets(line, LINE_SIZE, stdin);
assert(r != NULL);
char* endptr = NULL;
int x = strtol(line, &endptr, 10);
if (endptr == line) {
fprintf(stderr, "Error: Invalid input.\n");
exit(EXIT_FAILURE);
}
int coeffs[] = {2, 3, 4};
int n = sizeof(coeffs) / sizeof(coeffs[0]);
int result = horner(coeffs, n, x);
printf("The result of the polynomial evaluation is: %d\n", result);
return 0;
}