Обновить du2/program.c

This commit is contained in:
Bohdana Marchenko 2025-03-06 11:14:11 +00:00
parent b5ed7a6c61
commit 0941ced661

View File

@ -26,6 +26,7 @@ int read_double(double *value, int coef_index) {
// If no number was read or there are invalid characters
if (endptr == line || *endptr != '\0') {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index); // Correct error message
return -1; // Indicate error
}
@ -47,12 +48,11 @@ int main() {
double coef;
double result = 0;
int coef_count = 0; // Start counting coefficients from 0
int coef_count = 1; // Start from the first coefficient (x^1)
// Loop to read coefficients
while (1) {
coef_count++; // Increase coefficient index for each input
status = read_double(&coef, coef_count);
status = read_double(&coef, coef_count + 1); // coef_count + 1 for correct indexing
if (status == -1) {
return 0; // Exit program if there's invalid input
@ -66,6 +66,7 @@ int main() {
}
result = result * x + coef; // Apply Horner's method for polynomial evaluation
coef_count++;
}
// Print the result