#include #include #include #define DEGREE 10 int readPoly(int coef[], int *degree) { int i; printf("Degree of polynomial: "); if ( 1 == scanf("%d", degree) ) { if (*degree <= 0 || *degree > DEGREE) { fprintf(stderr, "Bad degree value %d\n", *degree); return 0; } } else { fprintf(stderr, "Bad format...\n"); return 0; } while (getchar() != '\n'); // ignore all other chars on this line getchar(); // swallow new line printf("Type the coefficients separated by a space, starting with the free term.\n"); printf("End with ENTER.\n"); for (i = 0; i <= *degree; i++) { if ( 1 != scanf("%d", &coef[i]) ) { fprintf(stderr, "Bad format for coeficient %d.", i); return 0; } } //check for free term and last degree if (coef[0] == 0) { fprintf(stderr, "Free term is 0.\n"); return 0; } if (coef[*degree] == 0) { fprintf(stderr, "Polynomial is not of degree %d.\n", *degree); return 0; } return 1; } int evalPoly(int coef[], int degree, int val) { int i, sum = 0; for (i = 0; i <= degree; i++) { sum += coef[i] * (int)pow(val, i); } return sum; } int main() { int coef[DEGREE + 1]; int degree, i; if ( readPoly(coef, °ree) ) { for (i = 0; i <= degree; i++) { if (evalPoly(coef, degree, i) == 0) printf("%d is a root\n", i); if (evalPoly(coef, degree, -i) == 0) printf("%d is a root\n", -i); } } else printf("Could not read polynomial."); return 0; }