Need help with quadratic equation program

Pages: 12
Ok, did all that. but why should i make a function for my output? it now compiles, but after i input a b and c, it won't show the roots (or it does for a split second) and then i try to click 'y' to rerun it, and it like freezes. here is my new 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
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

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

using namespace std;

void quad(double&,double&,double&,double&,double&);

int main()
{
	double a;
	double b;
	double c;
	double x1;
	double x2;
	char choice = 'y';






	cout <<"This program will find the roots of a quadratic"<<endl<<endl;

	do{

	cout <<"Please input the values for the coefficients; a, b, c""(y=ax^2+bx+c)"<<endl<<endl;
cin>>a>>b>>c;
quad(a,b,c,x1,x2);
if(x1 < 0)
{
cout <<setw<<fixed<<setprecision<<x1<<" is an imaginary root"<<endl;

}
else  
{
cout <<setw<<fixed<<setprecision<<x1<<" is a real root"<<endl;

}
if (x2 < 0)
{
cout <<setw<<fixed<<setpresision<<x2<<" is an imaginary root"<<endl<<endl;

}
else
{cout <<setw<<fixed<<setprecision<<x2<<" is a real root"<<endl<<endl;
}
system ("cls");

cout <<"Would you like to run this again? y or Y for yes, anything else for no"<<endl<<endl;
	
	}while (choice =='y' || choice =='Y');

	system ("Pause");
	return 0;

}


void quad(double&a, double&b, double&c, double&x1, double&x2)
{
	

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


}
also, thanks alot for the help. much appreciated.
@chervil yes you are correct sorry I was thinking he was calculation the roots with his quad function for some reason. He should probably put the if statements in the quad function and only assign a value to the real ones. Otherwise yeah do the b2 < 4 * a * c check to see if they are imaginary.
Well, this line is not very helpful, it gets rid of your results.
system ("cls");

After asking the user what they want to do next, you need to input the value of choice.
I also have to account for repeated roots, but otherwise i think this is correct?

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

using namespace std;

void quad(double&,double&,double&,double&,double&);

int main()
{
	double a;
	double b;
	double c;
	double x1;
	double x2;
	char choice = 'y';






	cout <<"This program will find the roots of a quadratic"<<endl<<endl;

	do{

	cout <<"Please input the values for the coefficients; a, b, c""(y=ax^2+bx+c)"<<endl<<endl;
cin>>a>>b>>c;

quad(a,b,c,x1,x2);
if((b * b - 4 * a * c) < 0)
{
cout <<setw<<fixed<<setprecision<<x1<<"\t"<<x2<<" the imaginary root(s)"<<endl<<endl;

}
else  
{
cout <<setw(5)<<fixed<<setprecision(2)<<x1<<"\t"<<x2<<" the real root(s)"<<endl<<endl;

}




cout <<"Would you like to run this again? y or Y for yes, anything else for no"<<endl<<endl;
cin >>choice;	

	}while (choice =='y' || choice =='Y');

	system ("Pause");
	return 0;

}


void quad(double&a, double&b, double&c, double&x1, double&x2)
{
	

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


}
also, sometimes it will output the else statement (meaning it should be a real number) but it will have imaginary numbers (i.e. -1.#j) what is happening here?
My complex root results must be in the form of A+Bi and A-Bi. I don't know where to start with this.
Take a look at the online quadratic solver here:
http://www.mathwarehouse.com/quadratic/quadratic-formula-calculator.php
I entered
1
2
3
a = 1
b = -11
c = 36.5

and got the results:
1
2
x = 5.5+2.5i 
x = 5.5−2.5i 


How to put this into your program?
Well, instead of the just x1 for the root, you need to deal with both the real and imaginary parts separately. You could have two separate variables x1r and x1i (and the same for x2). Pass these new variables to your quad function.

In the normal case, with real roots, the imaginary part x1i is always zero.
In the case where b2 - 4ac is negative, then the roots are imaginary (or complex).
x1r will be -b / (2*a) and
x1i will be sqrt( -(b*b - 4*a*c)) / (2 * a)
Is there a way to do this without changing a lot of my code?
Probably not anyways you'll have to return strings or create a Complex class. ( remember all numbers are complex numbers 4 = 4 + 0i; )
Where should i start? i understand the complex numbers, i am just clueless as to where to start to put it into my code. how would you do it? and i don't know what classes are yet.
You should do the string method then.

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 <sstream>
#include <cmath>

void quadratic( double a, double b , double c , std::string &x1 , std::string &x2 )
{
    // ( -b +/- sqrt( b*b - ( 4 * a * c ) ) ) / ( 2 * a )
    //if discriminant is negative it is imaginary
    double discriminant = b * b - ( 4 * a * c );
    double denominator = 2 * a;
    bool imaginary = false;
    
    if( discriminant < 0 )
    {
    	imaginary = true;
    	discriminant = -discriminant;
    }
    std::stringstream ss;
    if( imaginary )
    {
    	ss << -b / denominator;
    	x1 = ss.str() + '+';
    	x2 = ss.str() + '-';
    	ss.str("");
    	ss.clear();
    	ss << sqrt( discriminant ) / denominator;
    	x1 += ss.str() + 'i';
    	x2 += ss.str() + 'i';
    }
    else
    {
    	ss << ( -b + sqrt( discriminant ) ) / denominator;
    	x1 = ss.str();
    	ss.str("");
    	ss.clear();
    	ss << ( -b - sqrt( discriminant ) ) / denominator ;
    	x2 = ss.str();
    }
}

int main()
{
	double a = 1.0;
	double b = -11.0;
	double c = 36.5;
	
	std::string x1 = "";
	std::string x2 = "";
	
	quadratic( a , b , c , x1 , x2 );
	
	std::cout << x1 << std::endl;
	std::cout << x2 << std::endl;
	
	quadratic( -1 , 10 , 5 , x1 , x2 );
	
	std::cout << x1 << std::endl;
	std::cout << x2 << std::endl;
	return(0);
}


http://ideone.com/YegMWE
This was my version. It needs minor changes in main() to pass the two additional variables, and either print or ignore them as appropriate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void quad(double a, double b, double c, double &x1r, double &x2r, double &x1i, double &x2i)
{
    double d = b*b - 4.0*a*c;
    if (d >= 0)
    {
        x1i = 0;
        x2i = 0;
        x1r = (-b + sqrt(d)) / (2*a);
        x2r = (-b - sqrt(d)) / (2*a);
    }
    else
    {
        x1r = (-b)         / (2*a);
        x1i = (+sqrt(-d))  / (2*a);
        x2r = (-b)         / (2*a);
        x2i = (-sqrt(-d))  / (2*a);
    }
}
Yeah I suppose it'd work also to add two extra variables for i. I didn't even think of that. :P Would probably be better for the op so he doesn't have to use stringstreams.
thanks for the help, and i think the latter one would be easier to use.
How would i do this if i wanted to only pass a, b, and c by reference? Would i pass the others by value? or would i not use a function at all and just put them in my main?
Generally, you would pass a variable by reference when you want to be able to change it. (there are other reasons, but they don't really apply here).

Hence in this program a, b and c are the three variables that it would be better to pass by value, as you don't want to change them. The roots are in effect the returned values from the function, you pass them by reference rather than using a return statement as there is more than one item which needs to be returned.

If you really wanted to do things differently, you could define a struct or class to hold all of the values to be returned.
so passing all the variables by reference like i have is the best way?
No, because there is no need to pass a, b and c by reference. Because you don't want to change them it would be better to pass them by value.
Basically you want to pass by reference when it is a large class/struct, you want to modify a variable directly , you want to return multiple values.

Passing by reference does not create a copy so when you have a very large class it is much faster.

*edit
http://www.cplusplus.com/articles/z6vU7k9E/
Last edited on
Topic archived. No new replies allowed.
Pages: 12