/* * Program to calculate roots of a quadratic equation using * the quadratic formula. For now, input is hard-coded. * This program contains a bug! */ #include #include void solve_and_print(double a, double b, double c); int main(void) { solve_and_print(1, 0, -4); solve_and_print(1, 0, 4); solve_and_print(1, 0, 0); /* uncomment the next line to observe the bug in action */ /* solve_and_print(0, 1, 4); */ return 0; } /* print root(s) of a*x*x + b*x + c = 0, or message if none */ void solve_and_print(double a, double b, double c) { double discrim = b*b - 4*a*c; if (discrim > 0) { printf("two roots: %g %g\n", (-b + sqrt(discrim))/(2*a), (-b - sqrt(discrim))/(2*a)); } else if (discrim == 0) { printf("one root: %g\n", (-b) / (2*a)); } else { printf("no roots\n"); } }