// Demo program for CS 149. v. Nov. 10, 2000 // Find roots of quadratic equation. If b^2 -4ac is // approximately zero, then the solutions printed with // the standard formuation of the quadratic formula may // not be accurate. However with reformulation, the // problem may be avoided. // Input: Three real coefficients of a quadratic equation: // in the order a, b, c for the equation // a*x^2 + b*x + c = 0. // Prompts for input and repeats until EOF. // Output: Roots of the polynomial. // Assumptions: Three numberic inputs will be provided. #include #include #include int main() { // Functions to compute real roots of quadratic equation. double Root1Eq1( double a, double b, double c ); double Root2Eq1( double a, double b, double c ); double Root1Eq2( double a, double b, double c ); double Root2Eq2( double a, double b, double c ); double CompDisc( double a, double b, double c ); void CheckReport( double a, double b, double c, double root1, double root2 ); double CheckAns( double a, double b, double c, double ans ); double x1, // First real root of equation x2, // Second real root of equation a, // Coefficient of x^2 b, // Coefficient of x c, // Constant term Disc; // Discriminant: sqrt(b^2 - 4ac) while( !cin.eof() ) { // Read, echo and check input. } cout << "Enter coefficients of quadratric equation a, b, and c:" << endl; cin >> a >> b >> c; cout << "Finding roots of equation:" << endl; cout << " " << a << " x^2 + " << b << " x + " << c << " = 0" << endl; Disc = CompDisc( a, b, c ); // Standard formulation of quadratic formula x1 = Root1Eq1( a, b, c ); x2 = Root2Eq1( a, b, c ); CheckReport( a, b, c, x1, x2 ); // Alternate formulation of quadratic formula x1 = Root1Eq2( a, b, c ); x2 = Root2Eq2( a, b, c ); CheckReport( a, b, c, x1, x2 ); // Try branching on sign of b cout << "Roots computed based on sign of b" << endl; if ( b > 0 ) { x1 = Root1Eq2( a, b, c ); x2 = Root2Eq1( a, b, c ); } else { x1 = Root1Eq1( a, b, c ); x2 = Root2Eq2( a, b, c ); } // else CheckReport( a, b, c, x1, x2 ); } // while !eof return 0; } // main // Function definitions // Disc computes the discriminant if it is real. double CompDisc( double a, double b, double c ) { double ans; ans = b*b -4.0*a*c; if ( ans < 0.0 ) { cout << "Error in function Disc: imaginary roots" << endl; exit( EXIT_FAILURE ); } else return sqrt( ans ); } // Compute one real root using standard formula // a, b, and c are coefficients of quadratic. // This function assumes real, not imaginary, roots double Root1Eq1( double a, double b, double c ) { double CompDisc( double a, double b, double c ); return (-b + CompDisc(a,b,c))/ ( 2.0 * a ); } // Compute second real root using standard formula // a, b, and c are coefficients of quadratic. // This function assumes real, not imaginary, roots double Root2Eq1( double a, double b, double c ) { double CompDisc( double a, double b, double c ); return (-b - CompDisc(a,b,c))/ ( 2.0 * a ); } // Compute one real root using alternate formula // a, b, and c are coefficients of quadratic. // This function assumes real, not imaginary, roots double Root1Eq2( double a, double b, double c ) { double CompDisc( double a, double b, double c ); return ( 2.0 * c ) / ( -b - CompDisc(a,b,c) ); } // Compute second real root using alternate formula // a, b, and c are coefficients of quadratic. // This function assumes real, not imaginary, roots double Root2Eq2( double a, double b, double c ) { double CompDisc( double a, double b, double c ); return ( 2.0 * c ) / ( -b + CompDisc(a,b,c) ); } // Check answer. // a, b, and c are coefficients of quadratic, x is value to be checked. double CheckAns( double a, double b, double c, double x ) { return a*x*x + b*x + c; } // Check Report. // a, b, and c are coefficients of quadratic, x is value to be checked. void CheckReport( double a, double b, double c, double x1, double x2 ) { cout << "Roots using alternate formulation" << endl; cout << " First root: " << x1 << endl; cout << " Second root: " << x2 << endl; cout << "Checking answer: " << endl; cout << " First root: ax^2 + bx + c = " << CheckAns( a, b, c, x1 ) << endl; cout << " Second root: ax^2 + bx + c = " << CheckAns( a, b, c, x2 ) << endl; }