Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a function int QuadRoots( double a, double b, double c, double &r1;, do

ID: 3655404 • Letter: 1

Question

1. Write a function int QuadRoots( double a, double b, double c, double &r1;, double &r2;) that takes the coefficients of a polynomial (a, b, c) and, if possible, calculates the roots (r1 and r1). If the polynomial has real roots, return 0 to indicate success; and if not, return an error code as described below. The formula for the roots of a quadratic is: Write a program that opens an input text file called "coefficients.txt" containing the coefficients and a text file "quadout.txt" for output, loops reading values from the input file until end-of-file, and passes the coefficients to QuadRoots() to do all processing on the coefficients and printing aeither of two different error messages if -1 or -2 is returned or the resulting values with reasonable test if O.K. (0 returned). Your main() function that drives the QuadRoots() function may do none of the calculations, and the QuadRoots() function may not read nor display ANY values. You may assume the file containing the coefficients is correctly formatted, but you may not assume that the file open() works. The function Quadroots() will calculate roots and return 0 if the roots exist as real roots. If there are no roots, return -1; and if there are complex roots, return -2. If a == 0, the formula describes a line, so just say it has no roots (you may ignore the x-intercept of the line, if that exists), and if (b2-4ac) < 0 it has only complex roots. Test with values to check all these conditions (several of each). Attach your file to the email with the source

Explanation / Answer

#include <cmath>
#include <iostream>
#include <fstream>

const double epsilon = 0.000001;

using namespace std;

int QuadRoots( double a, double b, double c,
                double& r1, double& r2 ) {

    // Note: It is generally a very bad idea to test
    // floating point numbers for equality...
    if ( fabs( a ) < epsilon ) {
        r1 = -c/b;
        return -1;
    }

    double disc = b*b - 4 * a * c; // Discriminant

    // If the discriminant < 0, then the roots are complex
    if ( disc < 0 ) {
        r1 = r2 = NAN;   // NaN (Not a Number)
        return -2;
    }

    disc = sqrt( disc );
    r1 = (-b + disc) / (2 * a);
    r2 = (-b - disc) / (2 * a);
    return 0;
}

int main( int argc, char** argv ) {
    ifstream ifs;
    ofstream ofs;
    double a, b, c, r1, r2;

    ifs.open( "coefficients.txt" );

    if ( ifs.fail() ) {
        cerr << "Error opening coefficients.txt: " << strerror( errno ) << endl;
        exit( 1 );
    }

    ofs.open( "quadout.txt" );
    if ( ofs.fail() ) {
        cerr << "Error opening quadout.txt: " << strerror( errno ) << endl;
    }

    ifs >> a >> b >> c;
    // Note: We need to check for EOF *after* we read a, b, and c.
    while( !ifs.eof() ) {
        int ret = QuadRoots( a, b, c, r1, r2 );
        if ( ret == 0 ) {
            ofs << r1 << " " << r2 << endl;
        } else if ( ret == -1 ) {
            ofs << "No roots" << endl;
        } else {
            ofs << "No real roots." << endl;
        }
        ifs >> a >> b >> c;
    }
}