The quadratic function ax^2 + bx + c achieves the value 0 for certain values of x (called the roots of the quadratic) – these depend on the parameters a, b, and c. the function will have either one root (repeated twice) or two separate real (i.e., not complex) roots. Your job is to numerically find one root of the quadratic using a “brute-force” method. Specifically, you should evaluate ) (xf over a large range of values of x, and select the value of x which sets ) (xf closest to zero. For example if 1 a , 1 b and 6 c , then a search over the range 0 to 10 will show that values of x near 3 will yield ) (xf to be close to zero. You can then output 3 x as being an estimated root of the quadratic. Your program should also check that the condition ac b 4 2 holds, and if not, it should print “Complex Roots – cannot estimate with this program
#include <stdio.h>
#include <math.h>
int main(){
printf("************************************************\nWelcome to the quadratic root estimator\nThis estimates the value of one root of\nf(x)=ax^2+bx+c\n************************************************\n");
printf("Enter the coefficients in the form 'a b c':");
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
if((b*b)<(4*a*c))
printf("Complex Roots-cannot estimate with this program");
else{
float x, y;
printf("%f %f %f", a, b, c);
/* Evaluate x values and find f(x) closest to zero */
for(x=-20;x<20;x++){
y=a*x*x + b*x +c;
printf("%f", y);
}
}
}