C++ Program with quadratic equation, for unreal and real solutions

closed account (oN3k92yv)
I made a quadratic formula code that works - it accepts the numbers for A B and C, but it doesn't tell you when a solution is real, or unreal. How do I make it do that?

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
  #include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


int main ()
{
	double a = 0;
	double b = 0;
	double c = 0;
	float x1, x2 = 0;
	double disc = 0;

	cout << "===============================================================================" << endl;
	cout << "This program will provide solutions for an equation of the form A*x^2 + B*x + C where A, B, an C are intergers, and A is not equal to zero." << endl;
	cout << "Please enter value of a: " << endl;
	cin >> a;
	cout << "Please enter value of b: " << endl;
	cin >> b;
	cout << "Please enter value of c: " << endl;
	cin >> c;
	disc = ((b * b) - (4 * a* c)); 

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

	if (disc>0)
	{
		x1 = (-1 * b + sqrt(disc)) / (2 * a);
		x2 = (-1 * b - sqrt(disc)) / (2 * a);
	}
	else if (disc == 0)
	{
		x1 = x2 = (-1 * b) / (2 * a);
	}
	else
	{
		disc = -1 * disc;
		cout << "x1=" << "(" << (-1 * b) << "+ i" << sqrt(disc) << ")/" << 2 * a;
		cout << "x1=" << "(" << (-1 * b) << "+ i" << sqrt(disc) << ")/" << 2 * a;
		exit(0);
	}
	cout << "x1=" << x1 << "\n";
	cout << "x2=" << x2 << "\n";



	return 0;
}
The solution is unreal when it includes imaginary number(s), or when the radicand is negative. This is the case in your else statement.
Last edited on
closed account (48T7M4Gy)
the solution is unreal when the dicriminant is negative
closed account (oN3k92yv)
Okay, how does this look?
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
53
54
55
56
57
58

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


int main ()
{
	double a = 0;
	double b = 0;
	double c = 0;
	double x1, x2, x3 = 0;
	double disc = 0;

	cout << "===============================================================================" << endl;
	cout << "This program will provide solutions for an equation of the form A*x^2 + B*x + C where A, B, an C are intergers, and A is not equal to zero." << endl;
	cout << "Please enter value of a: " << endl;
	cin >> a;
	cout << "Please enter value of b: " << endl;
	cin >> b;
	cout << "Please enter value of c: " << endl;
	cin >> c;
	disc = ((b * b) - (4 * a* c)); 

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

	if (disc>0)
	{
		cout << "This equation has 2 solutions." << endl;
		x1 = (0 - b + sqrt(disc)) / (2 * a);
		cout << "The first solution is: " << x1 << endl;
		x2 = (0 - b - sqrt(disc)) / (2 * a);
		cout << "The second solution is: " << x2 << endl;
	
	}
	else if (disc == 0)
	{
		cout << "This equation has one real solution." << endl;
		x1 = x2 = (-1 * b) / (2 * a);
	}
	else if (disc<0)
	{
		cout << "This equation has 2 unreal solutions." << endl;
		disc = -1 * disc;
		cout << "x1=" << "(" << (-1 * b) << "+ i" << sqrt(disc) << ")/" << 2 * a;
		cout << "x1=" << "(" << (-1 * b) << "+ i" << sqrt(disc) << ")/" << 2 * a;
		exit(0);
	}

	


	return 0;
}
Last edited on
closed account (oN3k92yv)
When I ran it, instead of only saying it won't calculate a value that has A = 0, it still displayed the other things. Look

http://i.imgur.com/OwxddKo.png

How do I stop that?
Stick a return 0; in the if statement for if (a == 0). That way, once it tells you that A can't be zero, it exits main.
closed account (oN3k92yv)
You're fucking great, that fixed that. Which data type will make the answers be something like x= 3.0000+E00? Is it float? Should I add setprecision in there? Alright, I'm going to try to make it output if there is only one real solution as well. BRB
closed account (oN3k92yv)
updated below
Last edited on
closed account (48T7M4Gy)
@aixth Sounds like your code supplier has let you down. If you wrote the original I doubt whether you'd be asking these sort of questions. Ask for your money back. :)
closed account (oN3k92yv)
I fixed it, however it the program doesn't work if there is only one solution or two imaginary ones. this is what I got

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


int main ()
{
	float a = 0;
	float b = 0;
	float c = 0;
	float x1, x2, x3 = 0;
	int disc = 0;

	cout << "===============================================================================" << endl;
	cout << "This program will provide solutions for an equation of the form A*x^2 + B*x + C where A, B, an C are intergers, and A is not equal to zero." << endl;
	cout << "Please enter value of a: " << endl;
	cin >> a;
	cout << "Please enter value of b: " << endl;
	cin >> b;
	cout << "Please enter value of c: " << endl;
	cin >> c;
	disc = ((b * b) - (4 * a* c)); 
	cout.precision(4);
	cout << std::scientific;

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

	if (a == 0)
	{
		cout << "No solutions will be calculated for the leading coefficient of 0!" << endl;
		return 0;
	}

	if (disc>0)
	{
		cout << "This equation has 2 solutions." << endl;
		x1 = (0 - b + sqrt(disc)) / (2 * a);
		cout << "The first solution is: " << x1 << endl;
		x2 = (0 - b - sqrt(disc)) / (2 * a);
		cout << "The second solution is: " << x2 << endl;
	
	}
	else if (disc == 0)
	{
		cout << "This equation has one real solution." << endl;
		x1 = x2 = (0 - b - sqrt(disc)) / (2 * a);
	}

	else if (disc<0)
	{
		cout << "This equation has 2 unreal solutions." << endl;
		disc = -1 * disc;
		cout << "x1=" << "(" << (-1 * b) << "+ i" << sqrt(disc) << ")/" << 2 * a;
		cout << "x1=" << "(" << (-1 * b) << "+ i" << sqrt(disc) << ")/" << 2 * a;
		exit(0);
	}

	


	return 0;
}
Last edited on
closed account (48T7M4Gy)
Well done!
closed account (oN3k92yv)
I'm a student currently learning programming. Should I change the way I made the (disc<0) to match the way if disc was >0?
closed account (48T7M4Gy)
There's no reason why not. Complex numbers can have a 0i component
closed account (oN3k92yv)
I tried it, that did not work.

http://i.imgur.com/DpfhXbW.png

closed account (48T7M4Gy)
Hint 1: if the discriminant is negative then there is only one solution to it's square root.

Hint 2: There are always two solutions to a quadratic equation whether they are real or complex. And a real number is just a special case anyway.

if discriminant is negative
x1 = ...
x2 = ...

if discriminant is zero
x1 = ...
x2 = ... (x1 and x2 could be the same but who cares?)

if discriminant is positive (because zero is neither positive or negative)
x1 = ...
x2 = ...

display x1 and x2


http://www.purplemath.com/modules/quadform2.htm
Last edited on
closed account (oN3k92yv)
the discriminant is negative, but I'm trying to find the two imaginary solutions. that's the only part left
closed account (48T7M4Gy)
http://www.purplemath.com/modules/quadform2.htm
closed account (oN3k92yv)
That's not what I'm having a problem with, I know how the quadratic formula works. Nevermind.
closed account (48T7M4Gy)
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
53
54
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


int main ()
{
    float a = 0;
    float b = 0;
    float c = 0;
    float x1 = 0, x2 = 0;
    int disc = 0;
    double Im = 0;
    
    cout << "===============================================================================" << endl;
    cout << "This program will provide solutions for an equation of the form A*x^2 + B*x + C where A, B, an C are intergers, and A is not equal to zero." << endl;
    cout << "Please enter value of a: " << endl;
    cin >> a;
    cout << "Please enter value of b: " << endl;
    cin >> b;
    cout << "Please enter value of c: " << endl;
    cin >> c;
    
    disc = ((b * b) - (4 * a* c));
    
    if (disc > 0)
    {
        cout << "This equation has 2 solutions." << endl;
        
        x1 = (- b + sqrt(disc)) / (2 * a);
        x2 = (- b - sqrt(disc)) / (2 * a);
    }
    
    if (disc == 0)
    {
        cout << "This equation still has two real solutions, both are the same :)." << endl;
        x1 = x2 = -b / (2 * a);
    }
    
    if (disc < 0)
    {
        cout << "This equation has 2 complex solutions." << endl;
        disc = -1 * disc;
        x1 = x2 = b / (2 * a);
        Im = sqrt(disc)/(2 * a);
    }
    
    cout << " First: x2 = " << x1 << " + " << Im << "i" << endl;
    cout << "Second: x1 = " << x2 << " - " << Im << "i" << endl;
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.