Check if variable is defined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tycoonius
    New Member
    • Sep 2006
    • 2

    #1

    Check if variable is defined

    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:
    Code:
    double a, b, c, root1, root2;
    	cout << "Enter the 3 coefficients of the quadratic equation." << endl;
    	cin >> a >> b >> c;
    	if(cin)
    	{
    		cout << "Last input was successful." << endl;
    		cout << a << "x" << (char)253 << " + " << b << "x + " << c << endl;
    		root1 = (-b + sqrt(pow(b,2)+4*a*c)) / (2*a);
    		root2 = (-b - sqrt(pow(b,2)+4*a*c)) / (2*a);
    		if(root1 && root2)
    			cout << "There are two real roots." << endl;
    		else if(root1 || root2)
    			cout << "There is one real root." << endl;
    		else cout << "All roots are complex numbers." << endl;
    		if(root1)
    			cout << "Root 1 = " << root1 << endl;
    		if(root2)
    			cout << "Root 2 = " << root2 << endl;
    	}
    	else
    		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?
  • risby
    New Member
    • Sep 2006
    • 30

    #2
    Originally posted by tycoonius
    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.

    Comment

    • tycoonius
      New Member
      • Sep 2006
      • 2

      #3
      Yeah, that was a big help. Thanks.

      Comment

      Working...