#include #include void polValue(FILE* f, double *p, double *dp); int main() { FILE* in = fopen("input.txt", "r"); if (in == NULL) { perror("Could not open input file"); return (-1); } double p, dp; polValue(in, &p, &dp); fclose(in); FILE* out = fopen("output.txt", "w"); if (in == NULL) { perror("Could not open output file"); return (-1); } fprintf(out, "%f %f\n", p, dp); fclose(out); return 0; } // // Coefficients of polynomial are given in // ascending order. Calculate its value // in the given point and the value of its derivative // void polValue(FILE* f, double *p, double *dp) { double x; if (fscanf(f, "%lf", &x) != 1) { fprintf(stderr, "Incorrect input file.\n"); exit(-1); } // pv = a0 + a1*x + a2*x^2 + ... + an*x^n // dpv = a1 + 2*a2*x + ... + n*an*x^(n-1) double pv = 0.; // Value of polynomial double dpv = 0.; // Value of its derivative double n = 0.; // Length of coefficients sequence double r; // == x^(n-1) double a; while (fscanf(f, "%lf", &a) == 1) { if (n <= 0.) { pv += a; r = 1.; } else { dpv += n*a*r; r *= x; pv += a*r; } n += 1.; } *p = pv; *dp = dpv; }