Write the definition of a function named quadratic that receives three double parameters a, b, c. If the value of a is 0 then the function prints the message "no solution for a=0" and returns. If the value of "b squared" – 4ac is negative, then the code prints out the message "no real solutions" and returns. Otherwise the function prints out the largest solution to the quadratic equation. The formula for the solutions to this equation can be found here: Quadratic Equation on Wikipedia.
.
.
Click on the title for the solution
.
.
double quadratic(double a, double b, double c){
if(a==0){
cout<<"no solution for a=0";
return a;
}else if ((b*b-4*a*c)<0){
cout<<"no real solutions";
}else{
double x= (-b+sqrt(b*b-4*a*c))/(2*a);
double y= (-b-sqrt(b*b-4*a*c))/(2*a);
if (x>y){
cout<<x;
return x;
}else{
cout<<y;
return y;
}
}
}
.
Click on the title for the solution
.
.
This is the answer:
:
double quadratic(double a, double b, double c){
if(a==0){
cout<<"no solution for a=0";
return a;
}else if ((b*b-4*a*c)<0){
cout<<"no real solutions";
}else{
double x= (-b+sqrt(b*b-4*a*c))/(2*a);
double y= (-b-sqrt(b*b-4*a*c))/(2*a);
if (x>y){
cout<<x;
return x;
}else{
cout<<y;
return y;
}
}
}
Comments
Post a Comment