For the if discriminant < 0 statement, I need to not only display the error message, but I must display the complex roots in the correct format for complex numbers. How should I go about that?

#include <iostream>
#include <cmath>

using std::cout;
using std::cin;

int main()
{
    int a = 0,
        b = 0,
        c = 0,
        x1 = 0,
        x2 = 0,
        discriminant = 0;

    cout <<"Enter value for A: ";
    cin >> a;
    cout <<"Enter value for B: ";
    cin >> b;
    cout <<"Enter value for C: ";
    cin >> c;

    x1 = (-b + sqrt ((b * b) - 4 * a * c)) / (2 * a);
    x2 = (-b - sqrt ((b * b) - 4 * a * c)) / (2 * a);
    discriminant = ((b * b) - 4 * a * c);

    if ( a > 0 && discriminant > 0 )
        cout <<"Value X equals: " << x1 <<" or "<< x2 <<'\n';

        else
            if ( a == 0 )
            cout <<"Error"<<'\n';

            else
                if ( discriminant < 0 )
                {
                cout << "Error, roots will be complex, imaginary numbers." <<'\n';
                }

                else
                    if ( discriminant == 0)
                    cout <<"Roots will be identical: " << a << b << c << '\n';

    return 0;
}

Recommended Answers

All 2 Replies

This is what I did in C#, the code should be easy to follow.

 // ******************************************************************
        // Return the discriminant b*b-4*a*c
        // ******************************************************************
        public double Discriminant()
        {
            return cB * cB - 4 * cA * cC;
        }
        // ******************************************************************
        // Solve the equation and fill the roots struct with the results
        // ******************************************************************
        public void Solve()
        {
            // Preliminary calculations to avoid numbercrunching overhead
            double D = Discriminant();
            double twoA = cA + cA;
            double B2A = -cB / twoA;
            if (D == 0)             // roots are equal and real
            {
                MyRoot.R1_Re = MyRoot.R2_Re = B2A;
                MyRoot.R1_Im = MyRoot.R2_Im = 0.0;
            }
            else if (D > 0)         // roots are distinct and real
            {
                MyRoot.R1_Re = B2A + Math.Sqrt( D ) / twoA;
                MyRoot.R2_Re = B2A - Math.Sqrt( D ) / twoA;
                MyRoot.R1_Im = 0.0;
                MyRoot.R2_Im = 0.0;
            }
            else if (D < 0)         // no real roots, 2 complex conjugate roots
            {
                MyRoot.R1_Re = MyRoot.R2_Re = B2A;
                D = -D;
                MyRoot.R1_Im = Math.Sqrt(D) / twoA;
                MyRoot.R2_Im = -Math.Sqrt(D) / twoA;
            }        
        }

I considered every being complex, the real and equal roots having an Imaginary part of zero. To calculate the negative discriminant, I changed the sign. My complete code can be found here.

You should check to see if the discriminant is negative before calculating x1 and x2. And if you want to deal with possible imaginary components of results, you will need two more variables (for the imaginary parts of x1 and x2.)

Actually, I posted a small sub-routine in the "Code Snippets" section several months ago that is relevant to your problem:
http://www.daniweb.com/software-development/cpp/code/455970/quadratic-equation-solver-c-sub-routine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.