// Example with functions
// These #include statements also declare information at "file scope"
#include <iostream>
#include <cstdlib>
#include <cmath>
// function definition - include the code for the function
// calculate the root of the polyinomial equation ax^2+bx+c = 0
//
// root1 = (-b + sqrt(b^2 - 4ac))/2a
// root2 = (-b - sqrt(b^2 - 4ac))/2a
//
// returns a value of false if the equation has imaginary roots
// otherwise return a value of true.
bool getRoots(int a, int b, int c, float &rt1, float &rt2)
{
float t1, t2;
t1 = pow(b, 2) - 4*a*c;
// check for imaginary root
if (t1 < 0)
{
cout << "Fatal error: Imaginary roots" << endl;
exit (1);
}
else
{
rt1 = (-b + sqrt(t1)) / 2*a;
rt2 = (-b - sqrt(t1)) / 2*a;
return true;
}
}
int main()
{
int c1, c2, c3;
float root1, root2;
int number1 = 7;
bool result;
cout << "Number1: " << number1 << endl;
cout << "Starting Main" << endl;
cout << "Enter in the 3 coeffiecients: ";
cin >> c1 >> c2 >> c3;
// function call
result = getRoots (c1, c2, c3, root1, root2);
//if (result == true)
//if (getRoots (c1, c2, c3, root1, root2) == true)
if (result)
{
cout << "The first root is: " << root1 << endl;
cout << "The second root is: " << root2 << endl;
}
else
{
cout << "The equation has imaginary roots." << endl;
}
cout << "Ending Main" << endl;
}