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
|
#include <iostream>
#include <cmath>
void quadsolve(double a, double b, double c, bool& real, double& x1, double& x2){
double delta = b*b - 4*a*c;
if(delta>0){
double sqdelta = std::sqrt(delta);
x1 = ((-b+sqdelta)/2*a);
x2 = ((-b-sqdelta)/2*a);
real=true;
}
else{
real=false;
}
}
void quadsolve(double a, double b, double c, bool& real, double& x1, double& x2);
int main(){
double a,b,c;
std::cout << " please enter a b c" << std::endl;
std::cin >> a >> b >> c;
double s1, s2;
bool fake;
if(quadsolve(a, b, c, fake, s1, s2)){
std::cout << " the roots of the equation are " << s1 << " and " << s2 << std::endl;
}
else{
std::cout << " the equation has no real roots" << std::endl;
}
return 0;
}
|