I have this program written to calculate the real roots of a quadratic equation. How can I and what steps do I need to take to switch it to a stand alone function?
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double a, b, c, result_1, result_2, root_1;
cout << "To find the real roots of the quadratic enter a variable for a \n";
cin>> a;
cout<< "To find the real roots of the quadratic enter a variable for b \n";
cin>> b;
cout<< "To find the real roots of the quadratic enter a variable for c \n";
cin>> c;
root_1 = (b*b)- 4*a*c;
if (root_1>=0)
{
result_1 = (-b + sqrt(root_1))/(2*a);
result_2 = (-b - sqrt(root_1))/(2*a);
cout << "The real roots of the quadratic equation are\n";
cin >> result_1;
cin >> result_2;
} else
cout << "The results of the equation are imaginary.\n ";
cout << "No real roots exist.\n";
return 0;
}
I changed it to:
cout << "the real roots of the quadratic equation are\n" << result_1 << result_2;
Thanks for the helpful eye.
However, I am still only getting the response "The results of the equation are imaginary.
No real roots exist."
And I don't think I have created a stand alone function either. Any suggestions on either one of those?
I ran that stand alone like this:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
int findRoots (double a, double b, double c, double& result_1, double& result_2 )
{
double root_1;
root_1 = (b*b)- 4*a*c;
if (root_1>=0)
{
result_1 = (-b + sqrt(root_1))/(2*a);
result_2 = (-b - sqrt(root_1))/(2*a);
return 1;
}
else
{
return 0;
}
}
and got back this:error C2144: syntax error : 'int' should be preceded by ';'.
Did I screw up the copy and paste?