diff --git a/du2/program.c b/du2/program.c index 2ee8b90..8c9ca2e 100644 --- a/du2/program.c +++ b/du2/program.c @@ -26,7 +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 + printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index); // Corrected error message return -1; // Indicate error } @@ -36,9 +36,10 @@ int read_double(double *value, int coef_index) { int main() { double x; - // Read the value of x with special error message for invalid input + // Read the value of x int status = read_double(&x, 1); if (status == -1) { + // If x is invalid, print a specific error and exit printf("Nepodarilo sa nacitat zaklad x\n"); return 1; } @@ -48,10 +49,11 @@ int main() { double coef; double result = 0; - int coef_count = 1; // Start from the first coefficient (x^1) + int coef_count = 0; // We will count coefficients starting from 0, so x^0 coefficient will be at index 0 // Loop to read coefficients while (1) { + coef_count++; // Increase coefficient index for each input status = read_double(&coef, coef_count + 1); // coef_count + 1 for correct indexing if (status == -1) { @@ -66,7 +68,6 @@ int main() { } result = result * x + coef; // Apply Horner's method for polynomial evaluation - coef_count++; } // Print the result