Find the real roots of ax² + bx + c = 0. The discriminant is d = b² − 4ac: if d > 0 there are two roots (−b ± √d) / 2a, if d = 0 one root, if d < 0 no real roots. roots(a, b, c) shows one line: the number of roots, then the roots (2 decimals). For x² − 5x + 6 = 0 it shows 2 3.00 2.00.
2 3.00 2.00 2 0.28 -1.78 1 -1.00 0
#include <stdio.h>
#include <math.h>
void roots(double a, double b, double c) {
double d = 0;
// your code here: work out the discriminant d
if (d > 0) {
double x1 = 0, x2 = 0;
// your code here: use the formula to get x1 (with +) and x2 (with −)
printf("2 %.2f %.2f\n", x1, x2);
} else if (d == 0) {
printf("1 %.2f\n", -b / (2 * a));
} else {
printf("0\n");
}
}
int main(void) {
roots(1, -5, 6);
roots(2, 3, -1);
roots(1, 2, 1);
roots(1, 1, 1);
return 0;
}
Run tries the examples. When they look right, Submit.