Update 'cv3/program.c'
This commit is contained in:
parent
4694d6cc27
commit
affd4a5ae5
@ -1,50 +1,37 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
double hornerScheme(double x, double* coef, int n) {
|
#define MAX_COEFFICIENTS 100
|
||||||
double result = coef[0];
|
|
||||||
for (int i = 1; i < n; i++) {
|
double evaluatePolynomial(double x, double coefficients[], int n) {
|
||||||
result = result * x + coef[i];
|
double result = 0.0;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
result = result * x + coefficients[i];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
double x;
|
double x, coef;
|
||||||
|
double coefficients[MAX_COEFFICIENTS];
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
printf("Enter the value of x: ");
|
printf("Enter the value of x: ");
|
||||||
if (scanf("%lf", &x) != 1) {
|
if (scanf("%lf", &x) != 1) {
|
||||||
printf("Invalid input for x.\n");
|
printf("Invalid input for x.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int capacity = 10, count = 0;
|
printf("Enter coefficients from highest to lowest degree, finish with an empty line:\n");
|
||||||
double* coefficients = (double*)malloc(capacity * sizeof(double));
|
while (scanf("%lf", &coef) == 1) {
|
||||||
if (coefficients == NULL) {
|
if (count >= MAX_COEFFICIENTS) {
|
||||||
printf("Failed to allocate memory.\n");
|
printf("Maximum number of coefficients exceeded.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
coefficients[count++] = coef;
|
||||||
printf("Enter coefficients (end with non-numeric input):\n");
|
|
||||||
while (scanf("%lf", &coefficients[count]) == 1) {
|
|
||||||
count++;
|
|
||||||
if (count >= capacity) {
|
|
||||||
capacity *= 2;
|
|
||||||
double* temp = (double*)realloc(coefficients, capacity * sizeof(double));
|
|
||||||
if (temp == NULL) {
|
|
||||||
free(coefficients);
|
|
||||||
printf("Failed to reallocate memory.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
coefficients = temp;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double result = evaluatePolynomial(x, coefficients, count);
|
||||||
|
printf("Vysledok je: %.2f\n", result);
|
||||||
|
|
||||||
while (getchar() != '\n');
|
|
||||||
|
|
||||||
double result = hornerScheme(x, coefficients, count);
|
|
||||||
printf("The result is: %.2f\n", result);
|
|
||||||
|
|
||||||
free(coefficients);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user