Need help with quadratic equation program

Pages: 12
I know i'm not that close to done, but i am stuck. Where do i go from here? I need to write a program that when the user inputs the coefficients, a b and c, that it will output all the roots. I must account for real roots, repeated roots, and complex roots. Any help is appreciated, 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
 #include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

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

int main()
{
	double a;
	double b;
	double c;
	double y;
	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;

if((b * b - 4 * a * c) > 0)
quad(a,b,c);
cout <<"The positive roots are "<<x1<<endl<<endl;

if else ((b * b - 4 * a * c) = 0);
quad(a,b,c);

if else ((b * b - 4 * a * c) < 0);
quad (a,b,c);
cout <<"The negative roots are "<<x2<<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;

}


double quad(double&a, double&b, double&c)
{
	x1 = (-b+sqrt(b*b-4*a*c))/(2*a);
	x2 = (-b-sqrt(b*b-4*a*c))/(2*a);



	return;

}
Last edited on
You need to pass the parameters x1 and y1 by reference to the function quad. Since you won't be permanently changing the values of a, b and c, these can be passed by value rather than by reference. The return type of the function should be void. (Or possibly bool, in order to indicate whether the roots are real or imaginary).

When dividing by 2a, take care.
x / 2 * a means divide x by 2, then multiply the result by a.
In other words it is equivalent to (x / 2) * a

I am supposed to use pass by reference, and can you help me with what you are explaining? what do i need to add/change? and ok thanks for that tip!
You need to pass the parameters x1 and y1 by reference to the function quad. That is in addition to the existing parameters a, b, and c.

However, when you say "I am supposed to use pass by reference", I would interpret that as applying to x1 and y1 only, since those variables will be modified by the function and are used to pass back the result to the calling function.
im still stuck as to what to do
I'm willing to help, but I'm lost as to where you are stuck. Could you please explain what the problem is - presumably something which I wrote previously wasn't clear, but I think you need to point out specifically what it is that needs to be clarified.
How do i pass the parameters x1 and x2 by reference to my function quad? and also, my if else statements say "expecting a '(' " but i thought i already have a set of parenthesis there?
How do i pass the parameters x1 and x2 by reference to my function quad?
in exactly the same way as you are already passing the parameters a, b and c by reference.

Instead of three parameters, there will be five, and their names will be a, b, c, x1 and x2.

As for the parentheses, I've not tried to compile this, but currently I see a couple of problems on lines 35 and 38.
if else should read else if.
Also those lines should not end with a semicolon.
Also at line 35, the test for "equal to" should be == rather than =

Try that and see how you get on. Overall this looks fairly close to being correct, it just needs a few things ironing out.

The calculation seems reasonably close, but the logic is slightly flawed.

There are three possibilities: both roots are real and different, both roots are real and the same, and both roots are imaginary. The third case we cannot calculate here, it involves the square root of a negative number.
Last edited on
ok, so here is what i have now. It is saying my else if statements are incorrect, it specifically says that it expects a statement. Also, how would i output the imaginary roots?


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

using namespace std;

double 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;

if((b * b - 4 * a * c) > 0);
quad(a,b,c,x1,x2);
cout <<"The positive roots are "<<x1<<endl<<endl;

else if ((b * b - 4 * a * c) == 0)
quad(a,b,c,x1,x2);

else if ((b * b - 4 * a * c) < 0)
quad (a,b,c,x1,x2);
cout <<"The negative roots are "<<x2<<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;

}


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

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



	return x1,x2;

}
I think the problem lies on line 30 where you accidentally put a semi-colon.
I just tried and it still says error. any ideas? also, i changed a bit of my code. is this better?

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

using namespace std;

double 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;

if(x1 || x2 >= 0)
quad(a,b,c,x1,x2);
cout <<"The positive roots are "<<x1<<x2<<endl<<endl;

else if (x1 || x2 < 0)
quad(a,b,c,x1,x2);

cout <<"The negative roots are "<<x1<<x2<<endl;

else if ((b * b - 4 * a * c) < 0)
quad (a,b,c,x1,x2);
cout <<"The imaginary roots are "<<x2<<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;

}


double 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);



	return x1,x2;

}
Also remove lines 56 and 63
// double x1,x2; // Not needed

// return x1,x2; // Not needed
Can you be a little more clear "says error." What exactly is the error? If it is a compiler error the neat thing about them is they tell you exactly what line the error is on and what the error is.

*edit as chervil mentioned remove line 65 retun x1 , x2;
and make that function have a void return type. Remember functions can only return one value and you are using references so you shouldn't need to return anything.
Last edited on
Error:expected a statement. on lines 34 and 39 on the most recent one.
Ah that is because you have that output line in between the if statements either put curly braces around the first if statement before the else if or remove the output on line 32. Also you are doing your if/else if wrong.

One line 30 if( x1 || x2 >= 0) means this.. if x1 ( always true unless x1 == 0 ) or x2 >= 0 since it is an or if one of them is true it is a true so this if will always be true. What you want is if( x1 >=0 || x2 >= 0 ) they don't chain like math does.
How should i be doing my if/else if statements? and ok, let me change that.
1
2
3
4
if( condition )
    //do something
else if( condtion )
    //do something 


Is okay if you are doing one thing in each statement
But even if it is one thing I will still suggest you use braces like this:
1
2
3
4
5
6
7
8
if( condtion )
{
    //do something
}
else if( condition )
{
    //do something
}


You cant put stuff inbetween if and else if they must be back to back

*fixed code tag
Last edited on
ok, fixed all that. now it is saying 'uninitialized local variable used, x1' in line 30 and 'quad' must return a value in line 65.

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

using namespace std;

double 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;

if(x1 >=0 || x2 >= 0)
{quad(a,b,c,x1,x2);
cout <<"The positve roots are "<<x1<<x2<<endl;

}
else if (x1 <0 || x2 < 0)
{quad(a,b,c,x1,x2);
cout <<"The negative roots are "<<x1<<x2<<endl;

}
else if ((b * b - 4 * a * c) < 0)
{quad (a,b,c,x1,x2);
cout <<"The imaginary roots are "<<x2<<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;

}


double 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);


}
like I said earlier change the return type from double to void on line 7 and line 57. Also your error is pretty easy :P Like it says it's not initialized. I would remove line 31 , 36 , and line 41. Then put that line quad( a , b , c , x1 , x2 ); to line 29. That will fix your error. Also you may want to reconsider your logic on lines 32 , 37 , 42.

Think about it. You are saying if x1 <0 OR x2 < 0 the negative values are x1 AND x2

Also line 40-42 is the same as line 35-37 isn't it?

I would do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if( x1 < 0 || x2 < 0 )
{
    std::cout << "The imaginary roots are: ";
    if( x1 < 0 )
        std::cout << x1 << ' ';
    if( x2 < 0 )
        std::cout << x2  << ' ';
    std::cout << std::endl;
}

if( x1 >= 0 || x2 >= 0 )
{
    std::cout << "The real roots are: ";
    if( x1 >= 0 )
        std::cout << x1 << ' ';
    if( x2 >= 0 )
        std::cout << x2 << ' ';
    std::cout << std::endl;
}


The better method would probably be something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if( x1 < 0 )
{
    std::cout << x1 << " is an imaginary root." << std::endl;
}
else
{
    std::cout << x1 << " is a real root." << std::endl;
}

if( x2 < 0 )
{
    std::cout << x2 << " is an imaginary root." << std::endl;
}
else
{
    std::cout << x2 << " is a real root." << std::endl;
}



You should also create a function for your output maybe something like this

1
2
3
4
5
6
7
8

void type_of_root( int value )
{
    if( root < 0 )
        std::cout << value << " is an imaginary root." << std::endl;
    else
        std::cout << value << " is a real root." << std::endl;
}
In the earlier version if the code, it was testing b2 > 4ac which made sense. (because it indicates whether the roots are real or imaginary). Now the code is testing whether x1 and x2 are positive, which doesn't seem very useful.

Is there an original question that you are working from that says what the program needs to be doing?
Pages: 12