By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,373 Members | 2,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,373 IT Pros & Developers. It's quick & easy.

Check if variable is defined

P: 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
Share this Question
Share on Google+
2 Replies


risby
P: 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

P: 2
Yeah, that was a big help. Thanks.
Sep 18 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.