// Exercise 18: Solving a quadratic function
#include "std_lib_facilities.h"
#include <cmath>
int main()
{
double a = 0;
double b = 0;
double c = 0;
double x1 = 0;
double x2 = 0;
cout << "Enter coefficients a, b, and c: ";
cin >> a >> b >> c;
if (a == 0 || (b*b - 4*a*c) < 0)
cout << "No real solution!\n";
else {
x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
}
On Dec 24, 2009, at 5:32 AM, pantera wrote:
> Why is this exercise relatively easier than the likes of Exercise 13
> or 15? Not in increasing order of difficulty, or did I miss something
> in the program?
It's not clear to me whether Bjarne had necessarily listed each set of
exercises in increasing order.
> // Exercise 18: Solving a quadratic function
> #include "std_lib_facilities.h"
> #include <cmath>
>
> int main()
> {
> double a = 0;
> double b = 0;
> double c = 0;
> double x1 = 0;
> double x2 = 0;
>
> cout << "Enter coefficients a, b, and c: ";
> cin >> a >> b >> c;
>
> if (a == 0 || (b*b - 4*a*c) < 0)
> cout << "No real solution!\n";
> else {
> x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
> x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
> cout << "x1 = " << x1 << endl;
> cout << "x2 = " << x2 << endl;
> }
> }
I don't have the book with me here at home, so I can't see exactly how
the problem was originally phrased. Keeping that in mind, here are
some suggestions:
(1) a == 0 doesn't mean that there's no real solution. It means that
the "quadratic" equation is really linear. You can print an error msg
if you like, but it should be a different error msg. Alternatively,
you can solve this as a linear equation.
(2) Here's a basic rule: Never compute anything moderately complicated
more than once. So put
double disc = b*b - 4*a*c; // discriminant of quadratic
and now do x1 and x2 in terms of disc. (OTH, I wouldn't store 2*a in
a variable!)
(3) If disc == 0, then there's one real solution (a double root). So
maybe your pseudocode could be
if (disc < 0)
cout << "No real solution\n";
else if (disc == 0)
handle the case of one real root
else
handle the case of two distinct real roots
(4) If you feel adventurous, figure out how you could do the complex
conjugate roots that arise when disc < 0. There's a complex class in
the C++ standard library. Alternatively, set up variables (of type
double) for the real part and the imaginary part of the conjugate roots.
(5) If you feel really adventurous, you could worry about the
numerical instability of the traditional quadratic formula. (George
E. Forsythe wrote on the topic. Do a Google search if you're
interested.)
Looking back at all this, maybe this problem isn't so simple after all!
Art Werschulz
207 Stoughton Avenue, Cranford NJ 07016-2838
(908) 272-1146
I did miss things in this exercise. Thanks a lot for the hint. Merry X-
mas & Happy New Year!!
Here are some minor comments:
... deletia ...
> return (b*b) - (4*a*c);
The parentheses are unnecessary. I'd delete them. One can get into a
very long discussion of when one should retain redundant parentheses,
but I'd say that the expression
b*b - 4*a*c
doesn't require deep knowledge of operator precedence, simply that
multiplicative operations have a higher priority than additive
operations.
> cout << "Real root x1 = " << x1 << "\tReall root x2 = " << x2 << endl;
Fix the spelling of "Reall". You might also want to discriminate (so
to speak) between the case delta = 0 and delta > 0. The two cases
should maybe have different printouts; moreover, you don't need to
compute a square root when delta = 0.