#include <iostream> #include <cmath> using namespace std; bool quadraticFormula(double a, double b, double c, double *totalResults) { double D = sqrt( (b*b) - (4*a*c) );//shorter name if (!a)// a==0 => 2*a==0 { cout<<"Denominator is zero\n"; return false; // function failed -you can also use exceptions- } totalResults[0] = ( -b + D)/(2*a); totalResults[1] = ( -b - D)/(2*a); return true;//function succeded } int main(void) { double a, b, c; double x[2]; cout<<"Please Enter a, b, and c to get Results of x:\n"; cout<<"a: "; cin>>a; cout<<"b: "; cin>>b; cout<<"c: "; cin>>c; if ( quadraticFormula(a, b ,c, x) ) { cout<<"\nYour Results are:"<<endl; cout << x[0] << endl << x[1] << endl; } system("pause"); return 0; } |
(b*b) - (4*a*c)
being -ve?sqrt()
can only handle +ve numbers.