Hey guys I'm new to programing and I made a program to solve quadratic formula but it doesn't work with imaginary numbers when the number is negative I get something like this -1.#IND. What can I add or adjust in my code to help get the program to show imaginary 'complex numbers'. Thanks!

Edit: Also is there a way to do this without the #include <complex>\

#include <iostream>
#include <cmath>
using namespace std;

double a, b, c, rootvalue1, rootvalue2;

int main()
{

    cout <<"This program will have you enter coefficients a,b, and c.\n"                  //Explains what the program will do.
        "This will calculate two root values using the quadratic formula." << endl;
    cout <<"Enter Coefficient a: ";
    cin >> a;

    if (a == 0)                                                                             //If user enters 0 it will quit the program and give them error message.
    {
        cout << "0 is not a sufficient coefficient for this program.\n";
        cout <<"Restart now and try again." << endl;
        exit(0);
    }

    cout <<"Enter Coefficient b: ";
    cin >> b;
    cout <<"Enter Coefficient c: ";
    cin >> c;

    rootvalue1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);         // Quadratic formula
    rootvalue2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);          


    cout <<"The first Root Value = " << rootvalue1  << endl;          //Roo Answer
    cout <<"The second Root Value = " << rootvalue2  << endl;

    return 0;
}

If you want to compute complex numbers, then you should use the <complex> header and use the std::complex<double> type to represent your numbers. However, if you want to avoid it (why?), then you can simply check the value inside the square-root to see if it is negative (thus, leading to an imaginary component if the square root is taken). The sqrt function (when applied to double variables) will produce a NaN if the number is negative (btw, NaN means "Not a Number").

So, simply compute the square-rooted part separately, check it for negativity, and if it is negative than calculate the real and imaginary parts separately. If positive, then just use the code you have now.

This is why not the complex include these were the instruction I was given but doesn't quite make sense to me. I got it to work with the complex include but I don't think that's what is wanted.

  1. The quadratic formula fails if coefficient ais 0. If the user enters a 0 for a, print an appropriate error message and end the program (skip all calculations, do not loop).
    You may use exit(0);if you want. If you do, #include <stdlib.h>

  2. The discriminant, b^2 - 4ac, describes the roots of a quadratic equation: if it is greater than or equal to 0, there are two real roots; and if it is less than 0, there are two complex roots.

a. Complex numbers are displayed with a real part and an imaginary part: e.g., 6 +
3iand 6 - 3i

b. The real part (6 in the example) is calculated by the formula -b/2a

c. The imaginary part (3i in the example) is calculated by the formula
sqrt(descriminant)/2a with an ‘i’ character printed at the end

d. Taking the square root of a negative number with the sqrt function causes a
runtime error, so the discriminant must be negated (i.e., multiplied by -1) before
taking the square root

  1. Your program should find the roots in all three situations (the lab 1 version should work when the discriminant = 0 or > 0); your modifications for lab 2 should handle the case when the discriminant is < 0.

  2. Follow the link in the lab 2 notes to see some formulas and discussion that might be
    useful.

Those instructions are pretty detailed. Do you have any specific problem in implementing them? You cannot just say "how do I do it" and expect complete code for it.

well I don't want the whole code I'm trying to figure out I guess maybe somewhere to start would help I tried putting in those equation into the program but don't seem to get the results I'm looking for. Our practice test is
a=2
b=2
c=2
x1= -0.5 + 0.866025i
x2= -0.5 - 0.866025i

and -0.5 is supposed to be the real and I'm getting -2 and nothing near the 0.866025

As said in your instructions, the real part is calculated as:

double real_part = -b / (2.0 * a);

So, you say you calculated that value, from a = 2, b = 2, and c = 2, and got -2? How can -2 / (2 * 2) == -2? You must have inputted the formula wrong. What code do you have so far?

This is what I got.

#include <iostream>
#include <cmath>
using namespace std;

double a, b, c, rootvalue1, rootvalue2, real, descriminant, complex;

int main()
{

    cout <<"This program will have you enter coefficients a,b, and c.\n"                  
        "This will calculate two root values using the quadratic formula." << endl;
    cout <<"Enter Coefficient a: ";
    cin >> a;
    if (a == 0)                                                                             
    {
        cout << "0 is not a sufficient coefficient for this program.\n";
        cout <<"Restart now and try again." << endl;
        exit(0);
    }

    cout <<"Enter Coefficient b: ";
    cin >> b;
    cout <<"Enter Coefficient c: ";
    cin >> c;

    descriminant = pow(b,2) - (4 * a * c);
    rootvalue1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);         
    rootvalue2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);  
    real = -b / (2 * a);
    complex = sqrt(descriminant * -1) / (2 * a);

        if (descriminant < 0)
    {
    cout <<"The first Root Value = " << real  << " + " << complex << "i" << endl;
    cout <<"The second Root Value = " << real  << " - " << complex << "i" << endl;
    }
        if (descriminant > 0)
        {
    cout <<"The first Root Value = " << rootvalue1  << endl;          
    cout <<"The second Root Value = " << rootvalue2  << endl;
        }

    return 0;
}

Thanks for your help :)

Well, it seems you have it solved now. May I suggest you put the calculations of the roots inside the if-else blocks, as so:

descriminant = pow(b,2) - (4 * a * c);
if (descriminant < 0)
{
    real = -b / (2 * a);
    complex = sqrt(descriminant * -1) / (2 * a);
    cout <<"The first Root Value = " << real  << " + " << complex << "i" << endl;
    cout <<"The second Root Value = " << real  << " - " << complex << "i" << endl;
}
else
{
    rootvalue1 = (-b + sqrt(descriminant)) / (2 * a);         
    rootvalue2 = (-b - sqrt(descriminant)) / (2 * a);  
    cout <<"The first Root Value = " << rootvalue1  << endl;          
    cout <<"The second Root Value = " << rootvalue2  << endl;
}

So, this is solved right?

Yes this is solved thank you!

As usual, mike_2000_17 gives awesome advice! :-) Mike, you have a LOT more patience than I do!

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.