Write a C++ program that solves quadratic equation to find its roots

Ok, so I am in my first c++ college course. I have done programming before in java, html, sql and some basic c++ stuff before. I find the class to be easy so far but I am actually stuck on this homework assignment.

The assignment is as follows :

Write a C++ program that solves quadratic equation to find its roots. The roots of a quadratic equation ax2+bx+c=0 (where a is not zero) is given by the formula:

x=(-b±√(b^2-4ac))/2a


Note:To find the square root of a number , use the function sqrt() and include the math library.

where (b^2-4ac) is the discriminant.

If the value of the discriminant is zero then the equation has a single real root. If the value of the discriminant is positive then the equation has two real roots. If the value of the discriminant is negative, then the equation has two complex roots.

Here is the sample output that they gave me to outline my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
example 1:

Enter the values for a, b and c
1
3
-4

The discriminant is 25
The equation has two real roots.

The roots of the quadratic equation are x=-4,1

example 2:

Enter the values for a, b and c
2
-4
-3

The discriminant is 40
The equation has two real roots.

The roots of the quadratic equation are x=-0.58, 2.58


example 3:

Enter the values for a, b and c
3
4
2

The discriminant is -8
The equation has two complex roots.

The roots of the quadratic equation are 

 x = -0.67 + 0.47i, -0.67 - 0.47i


Now I have gotten through the homework relatively easy but i have hit a snag. If you look at the last example the answers are complex roots that use i (sqrt(-1)) and i cant seem to figure out how to get that to show. right now when i enter the numbers for "example three" my two roots that show up are "x = -nan, -nan". If you have any ideas on how i can get the complex roots to show up with the i then i would very much appreciate the help.

Here is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
// Variable Declerations
double a, b, c;

//Variable Inputs
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;

//Computations
double discriminant = (pow(b,2) - 4*a*c);
double positive_root = (((-b) + sqrt(discriminant))/(2*a));
double negative_root = (((-b) - sqrt(discriminant))/(2*a));

//Output
if (discriminant == 0)
    {
    cout << "\n\nThe discriminant is ";
    cout << discriminant << endl;
    cout << "The equation has a single root.\n\n";
    }
else if (discriminant < 0)
    {
    cout << "\n\nThe discriminant is ";
    cout << discriminant << endl;
    cout << "The equation has two complex roots.\n\n";
    }
else
    {
    cout << "\n\nThe discriminant is ";
    cout << discriminant << endl;
    cout << "The equation has two real roots.\n\n";
    }

//Final Root Values
cout << "The roots of the quadratic equation are x = ";
cout << negative_root;
cout << ", ";
cout << positive_root << endl << endl;

return 0;


}


Please let me know if anything is unclear or any questions I have not answered.

Thanks,
LiverpoolFTW
I think you have to have a custom calculation for the case where the discriminant is negative, manually separating the real and imaginary parts of the root.
You would have to check the discriminant yourself and factor out the i if you have a negative root.

EDIT: Damn, ninja'd!
Last edited on
Sounds annoying but do-able. Thanks for the help.
Liverpool? *shudder* jk =P
Got it to work great. Thanks again for all the help.

And Liverpool is the best! especially without Nando!
haha
Topic archived. No new replies allowed.