Implement a function void rootdouble a double b double c dou

Implement a function void root(double a, double b, double c, double& x1, double& x2) which assigns two solutions of quadratic equation ax2? + bx + c = ) to variables x1 and x2. where a, b, and c are real numbers. The two solutions are

Solution


#include <stdio.h>
#include <stdlib.h>
#include<math.h>

//FUNCTION PROTOTYPE
void root(double,double,double,double*,double*);

int main()
{
double a=2,b=9,c=3;
double x1,x2;

root(a,b,c,&x1,&x2);

printf(\"\ x1=%.2f\\tx2=%.2f\ \",x1,x2);

return 0;
}
void root(double a,double b,double c,double *x1,double *x2)
{
double determinant = b*b-4*a*c;
// condition for real and different roots
if (determinant > 0)
{
// sqrt() function returns square root
*x1 = (-b+sqrt(determinant))/(2*a);
*x2 = (-b-sqrt(determinant))/(2*a);

}

//condition for real and equal roots
else if (determinant == 0)
{
*x1 = *x2 = -b/(2*a);


}

}


OUTPUT:

x1=-0.36 x2=-4.14

Implement a function void root(double a, double b, double c, double& x1, double& x2) which assigns two solutions of quadratic equation ax2? + bx + c = )

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site