Problem with using/calling function

I am writing a program that will output the roots of a 2nd degree polynomial. I have to use the function find_roots, I know i am using it wrong but i am lost as to how i would call it twice for both roots. I think i could use 'static' to return two values, but not sure how. Any help would be appreciated.

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
  #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;


double find_root (double, double);
int main()
{
	double a;
	double b;
	double c;
	double root1;
	double root2;
	double x1;
	double x2;
	double disc = ((b*b) - (4*a*c));

	cout<<"This function will find the roots of a 2nd degree polynomial"
		"Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
	cin>>a>>b>>c;

	root1 = find_root(x1, x2);
	cout<<x1<<endl;

	system("Pause");
	return 0;
}

double find_root(double x1, double x2)
{
	double a;
	double b;
	double c;
	double x1;
	double x2;
	double 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);

	return x1;
	return x2;
}
Also, i wrote a similar program before and this is it, would this still work if i just put it into my find_root function? If i tweaked it a bit to return the roots to main0 and cout them there?

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
double A;
	double B;
	double C;
	double x1;
	double x2;
	double disc;

	

	cout<<"This program will calculate the roots of a 2nd order polynomial, Ax^2 + Bx + C\n\n";
	cout<<"Please input the values for the coefficients of the polynomial (A, B, C) ";
	cin>>A>>B>>C;

	x1 = (-B + sqrt(B*B - 4*A*C))/(2*A);
	x2 = (-B - sqrt(B*B - 4*A*C))/(2*A);
	disc = (B*B - 4*A*C);		//discriminant


	if ( disc < 0 )
		

	
	cout<<"\nThe roots are "<<setw(7)<<fixed<<setprecision(2)<<(-B/2*A)<< " +/-" <<setw(7)<<(sqrt(abs(disc))/2*A)<<"i "; //imaginary
	
	else
	{
	
	if(A==0)
	{ 
		cout<<"That equation describes a line since A = 0"<<endl;	// line


	}
	else

	{cout<<fixed<<setprecision(2)<<"The roots of the polynomial are x1 = "<<setw(6)<<fixed<<setprecision(2)<<x1<<" and x2 = "<<setw(6)<<x2<<endl;
	}
	
	}
	

	




	system("Pause");
	return 0;
Topic archived. No new replies allowed.