/* Program to determine the roots of a quadratic equation of the form */ /* coefA * x * x + coefB * x + coefC = 0 */ /* The roots are the values of x for which the equation is true */ /* The user is requested to input the values of coefA, coefB and coefC */ /* The input values will be displayed, then the roots will be calculated */ /* and displayed. */ /* All input and output values are of type double */ /* Program author: Janice Regan: Last update February 1, 2006 */ #include #include /* This will allow us to more accurately identify cases with double roots */ const double DBL_EPSILON = 0.000001; int main ( ) { /* Declare constants */ /* This constant will be used as a tolerance */ /* Remember that floating point numbers have limited accuracy */ /* Each floating number is represented by the closest representable value */ /* It is possible that 0 will be represented by a very small number */ /* but positive, so we don't want to compare to 0.0 in our test statements*/ /* instead we will compare the value (value - 0 ) to a tolerance DBL_EPSILON. */ /* Declare required variables */ double coefA; double coefB; double coefC; double discriminant; double answer1; double answer2; int solutionKind; /* Declare user defined functions for use in this main program */ void quadraticSolver( double coefSq, double coefLin, double coefConst, double *answer1, double *answer2, int *solutionKind ); /* Initialize required variables */ coefA = 0.0; coefB = 0.0; coefC = 0.0; discriminant = 0.0; answer1 = 0.0; answer2 = 0.0; solutionKind= 0; /* Read in coefA, coefB, coefC */ /* the coefficients of the quadratic equation */ printf(" Enter the value of the coefficient of "); printf("the second order (x squared) term: "); (void)scanf("%lf", &coefA); printf("Enter the value of the coefficient of the first order term: "); (void)scanf("%lf", &coefB); printf("Enter the value of the constant term: "); (void)scanf("%lf", &coefC); /* Label and print the values of each of the coefficients */ /* The labeled values of the coefficients are followed by a blank line */ printf("\n\n"); printf("The coefficients of the quadratic equation are:\n"); printf("The coefficient of the second order term is %8.3f \n", coefA); printf("The coefficient of the first order term is %8.3f \n", coefB); printf("The constant term is %8.3f \n\n\n\n", coefC); quadraticSolver( coefA, coefB, coefC, &answer1, &answer2, &solutionKind); if(solutionKind == 0) { printf("There are no real roots.\n\n"); } else if(solutionKind == 1) { printf("There is one real root, answer1 = %4.2f \n\n", answer1); } else if(solutionKind == 3) { printf("There is one double root, answer1 = answer2 = %4.2f \n\n", answer1); } else if(solutionKind == 2) { printf("There are two real roots, answer1 = %4.2f and answer2 = %4.2f\n\n", answer1, answer2); } else { printf("Illegal number of roots\n"); } return 0; } void quadraticSolver( double coefSq, double coefLin, double coefConst, double *root1p, double *root2p, int *solutionTypep ) { /* Declare variables local to this function */ double root1; double root2; double discriminant; int solutionType; /* Initialize local variables */ root1 = *root1p; root2 = *root2p; discriminant = 0.0; solutionType = *solutionTypep; /* Calculate the discriminant of the quadratic equation */ /* The discriminant is coefLin*coefLin - 4*coefSq*coefConst */ discriminant = coefLin*coefLin - 4*coefSq*coefConst; if(fabs(coefSq) < DBL_EPSILON) { /* Solve the quadratic equation for the case of the value of coefSq is 0*/ /* This makes the equation a linear equation not a quadratic. However, */ /* because the user can enter 0 for coefSq we must consider this case. */ /* There are two solutions for this case: */ /* If the coefficient coefLin is 0 than there are no roots */ /* Otherwise there is a single real root -coefConst/coefLin */ /* */ /* Display labelled values of the root */ if(fabs(coefLin) < DBL_EPSILON) { solutionType = 0; } else { root1 = -coefConst/coefLin; solutionType = 1; } } else if(fabs(discriminant) < DBL_EPSILON) { /* Solve the quadratic equation for two identical roots */ /* Display labelled values of the roots */ root1 = -coefLin/(2*coefSq); solutionType = 3; } else if(discriminant < DBL_EPSILON) { /* The quadratic equation has no real roots. */ /* Both roots of the quadratic equation are complex */ solutionType = 0; } else { /* Solve the quadratic equation for two roots */ /* Display labelled values of the roots */ root1 = (-coefLin + sqrt(discriminant) ) / (2 * coefSq); root2 = (-coefLin - sqrt(discriminant) ) / (2 * coefSq); solutionType = 2; } *solutionTypep = solutionType; *root1p = root1; *root2p = root2; }