470,850 Members | 1,306 Online

# Check if variable is defined 2
I need help with this program and I'm not sure how to do something. The program is supposed to take 3 doubles to make a quadratic equation, then calculate and display all real roots. Here's the problem: If one or more of the roots is unreal or complex, it tries to display them anyway because I don't know how to test for that.

Here's the relevant parts:
Expand|Select|Wrap|Line Numbers
1. double a, b, c, root1, root2;
2.     cout << "Enter the 3 coefficients of the quadratic equation." << endl;
3.     cin >> a >> b >> c;
4.     if(cin)
5.     {
6.         cout << "Last input was successful." << endl;
7.         cout << a << "x" << (char)253 << " + " << b << "x + " << c << endl;
8.         root1 = (-b + sqrt(pow(b,2)+4*a*c)) / (2*a);
9.         root2 = (-b - sqrt(pow(b,2)+4*a*c)) / (2*a);
10.         if(root1 && root2)
11.             cout << "There are two real roots." << endl;
12.         else if(root1 || root2)
13.             cout << "There is one real root." << endl;
14.         else cout << "All roots are complex numbers." << endl;
15.         if(root1)
16.             cout << "Root 1 = " << root1 << endl;
17.         if(root2)
18.             cout << "Root 2 = " << root2 << endl;
19.     }
20.     else
21.         cout << "Last input was unsuccessful. Check your data and try again." << endl;
Obviously, those if statements aren't of much use. So, is there some method or something to test the variables to see whether they're defined?
Sep 18 '06 #1
2 10668 risby
30 I need help with this program and I'm not sure how to do something. The program is supposed to take 3 doubles to make a quadratic equation, then calculate and display all real roots. Here's the problem: If one or more of the roots is unreal or complex, it tries to display them anyway because I don't know how to test for that.
the equation you are solving is ax^2 + bx + c = 0 and you are using the formula -b +/- sqrt(b^2 - 4ac) / 2a to solve it

but wait, if a = 0 there is no ^2 term so the equation is the rather simpler linear equation bx + c = 0

if you used the quadratic equation when a = 0, and therefore also 2a = 0, you'd be dividing by 0. this is naughty!

so when a=0 use:

bx + c = 0
or
bx = 0 - c
or
x = -c / b

to find the single real root

otherwise

calculate the determinant, that bit of the formula of which you are going to take the square root. If the determinant is zero the roots are co-incident. Imagine the U shaped curve sitting on the x-axis.

If the determinant is zero the formula is -b +/- sqrt(0) / 2a which simplifies to -b/2a since +/- sqrt(0) is also zero.

So when the determinant = 0 use

x = -b / 2a

to find the single real root.

On the other hand if the determinant is less than zero, because the square root of -1 is i, the roots will have an imaginary component and you will have to use complex numbers in the formula

But if the determinant is positive and non-zero then you can use the formula in the way you are doing already.

Hope this helps.
Sep 18 '06 #2
tycoonius
2 Yeah, that was a big help. Thanks.
Sep 18 '06 #3

 reply views Thread by Thomas Scheffler | last post: by 5 posts views Thread by Brandolon Hill | last post: by 26 posts views Thread by JGH | last post: by 15 posts views Thread by Rey | last post: by 5 posts views Thread by DJTB | last post: by 7 posts views Thread by Wessel Troost | last post: by 10 posts views Thread by Patrick B | last post: by 51 posts views Thread by moosdau | last post: by 8 posts views Thread by Antony | last post: by 3 posts views Thread by ferg | last post: by reply views Thread by ryjfgjl | last post: by reply views Thread by AlexandraMT | last post: by 1 post views Thread by DANILIN | last post: by 1 post views Thread by MarkDoronin | last post: by reply views Thread by sjain6 | last post: by reply views Thread by shivajikobardan | last post: by reply views Thread by gglobus | last post: by reply views Thread by Ravipg | last post: by reply views Thread by DJRhino1175 | last post: by