Problem!!!

Hello! I am a beginning c++ programmer, and I was learning the language by practicing a problem. However, I have a problem in my code and I can't figure out what it is. The problem wants you to find the roots of the quadratic equation, but the output of the roots comes to a funny output like -1.#ND00. Can anybody help me debug this program? Thanks!

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

using namespace std;

int main (){
	
	cout<<fixed<<showpoint<<endl;
	
	cout<<"***********"<<endl;
	cout<<"WELCOME"<<endl;
	cout<<"***********"<<endl;

	cout<<"Please enter coefficients a, b and c from the quadratic expression: "<<endl;
	cout<<endl;
	double a;	
	cout<<"a: ";
	cin>>a;
	cout<<endl;
	double b;
	cout<<"b: ";
	cin>>b;
	cout<<endl;
	double c;
	cout<<"c: ";
	cin>>c;
	cout<<endl;

	double root1;
	double root2;

	root1 = (-b + pow(b*b-4*a*c,0.5))/2*a;
	assert (root2 = (-b - pow(b*b-4*a*c,0.5))/2*a);

	assert(cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl);

	if(root2=0){
		cout<<"The discriminant equals zero, therefore there is only a single (repeated) root"<<endl;
		cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;
		return 0;
	}

	else if(root2>0){
		cout<<"The discriminant is greater than zero, and therefore has two real roots"<<endl;
		cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;
		return 0;
	}

	else
		cout<<"The discriminant is less than zero, and therefore has two complex roots"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;

		return 0;
	}


	
the output of the roots comes to a funny output like -1.#ND00
It means you tried to get the square root of a negative number, which in turn means the equation has no solutions.

The asserts on lines 35 and 37 make no sense.
On line 39 you're using the assignment operator (=) rather than the equality operator (==).
You're checking the sign of the second root, which is incorrect. You should be checking the sign of the discriminant. Furthermore, you should check that before calculating the square root.
Yea, I missed the equality operator and changed the asserts, but what do you mean I should calculate the discriminant before the square root? Am I not doing that now?
but what do you mean I should calculate the discriminant before the square root?
I said "check", not "calculate".
and how do you check it?
and how do you check it?

LOL, you already did that! just copy-paste the checking to before calculating the roots!
Here is the changed code, but everytime the program runs it goes straight to the else statement (the discriminant is less than zero). Why is it still not working?

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

using namespace std;

int main (){
	
	cout<<fixed<<showpoint<<endl;
	
	cout<<"***********"<<endl;
	cout<<"WELCOME"<<endl;
	cout<<"***********"<<endl;

	cout<<"Please enter coefficients a, b and c from the quadratic expression: "<<endl;
	cout<<endl;
	double a;	
	cout<<"a: ";
	cin>>a;
	cout<<endl;
	double b;
	cout<<"b: ";
	cin>>b;
	cout<<endl;
	double c;
	cout<<"c: ";
	cin>>c;
	cout<<endl;

	double root1;
	double root2;

	root1 = (-b + pow(b*b-4*a*c,0.5))/2*a;
	root2 = (-b - pow(b*b-4*a*c,0.5))/2*a;

	if(root2==0)
		cout<<"The discriminant equals zero, therefore there is only a single (repeated) root"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;
		//return 0;
	

	else if(root2>0)
		cout<<"The discriminant is greater than zero, and therefore has two real roots"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;
	
	else
		cout<<"The discriminant is less than zero, and therefore has two complex roots"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;

	cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;

		return 0;
	}


	
why do you use the 2nd root as the discriminant?
First, note your order of operations on the division. As written your code will execute as:

root1 = ((-b + pow(b*b-4*a*c,0.5))/2.0)*a;

use parentheses to force it to evaluate the denominator first:

root1 = (-b + pow(b*b-4*a*c,0.5))/(2.0*a);

I like to write my constants as floats unless I really intend them to be integer. Helps me spot int vs. float errors.

On to the problem you asked about! You need to check the sign of the discriminant (b^2-4ac) *before* you calculate the roots. Because the root calculation will bomb if the discriminant < 0 (sqrt of negative).

Something like this at line 33:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double disc = b*b - 4.0*a*c;
if(disc == 0)
{
 root1 = -b/(2.0*a);
 cout << "The discriminant equals zero, therefore there is only a single (repeated) root:\t" << root1 << endl;
}
else if(disc > 0)
{
 root1 = (-b + pow(disc,0.5))/(2.0*a);
 root2 = (-b - pow(disc,0.5))/(2.0*a);
 cout << "The discriminant  is greater than zero, and therefore has two real roots:\t" << root1 << "\t" << root2 << endl;
}
else
{
 cout<<"The discriminant is less than zero, and therefore has two complex roots"<<endl;
 // add code here for representing and calculating complex roots.
}
Okay, I see what I did wrong. Thanks for the help! However, I still have one problem. When I run the program for equations like 2x^2-11+5 (two real roots) or -4x^2+12x-9 (one real root), the roots of the equation don't come out in the output. What am I still doing wrong? Thanks again for your help! P.S. I know there is a lot of commented out stuff, but bear with me! thanks!

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
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cassert>

using namespace std;

int main (){
	
	cout<<fixed<<showpoint<<endl;
	
	cout<<"***********"<<endl;
	cout<<"WELCOME"<<endl;
	cout<<"***********"<<endl;

	cout<<"Please enter coefficients a, b and c from the quadratic expression: "<<endl;
	cout<<endl;
	double a;	
	cout<<"a: ";
	cin>>a;
	cout<<endl;
	double b;
	cout<<"b: ";
	cin>>b;
	cout<<endl;
	double c;
	cout<<"c: ";
	cin>>c;
	cout<<endl;

	double root1;
	double root2;

	//root1 = (-b + pow(b*b-4*a*c,0.5))/2*a;
	//root2 = (-b - pow(b*b-4*a*c,0.5))/(2*a);

	/*if(root2==0)
		cout<<"The discriminant equals zero, therefore there is only a single (repeated) root"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;
		//return 0;
	

	else if(root2>0)
		cout<<"The discriminant is greater than zero, and therefore has two real roots"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;
	
	else
		cout<<"The discriminant is less than zero, and therefore has two complex roots"<<endl;
		//cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;

	cout<<"The roots of the quadratic expression are: "<<root1<<" "<<root2<<endl;

		return 0;*/

double disc = b*b - 4.0*a*c;
if(disc == 0)
{
 root1 = -b/(2.0*a);
 cout << "The discriminant equals zero, therefore there is only a single (repeated) root:\t" << root1 << endl;
}
else if(disc > 0)
{
 root1 = (-b + pow(disc,0.5))/(2.0*a);
 root2 = (-b - pow(disc,0.5))/(2.0*a);
 cout << "The discriminant  is greater than zero, and therefore has two real roots:\t" << root1 << "\t" << root2 << endl;
}
else
{
 cout<<"The discriminant is less than zero, and therefore has two complex roots"<<endl;
 // add code here for representing and calculating complex roots.
}
	}


	
Dunno - works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ./a.out

***********
WELCOME
***********
Please enter coefficients a, b and c from the quadratic expression: 

a: 2

b: -11

c: 5

The discriminant  is greater than zero, and therefore has two real roots:       5.000000        0.500000
Okay, I see! It works now, my window just wasn't big enough and was cutting out the output! Thanks for your help!
Topic archived. No new replies allowed.