Hi, I need help writing A C program using pointers. Here is the description: Wri
ID: 3641988 • Letter: H
Question
Hi,
I need help writing A C program using pointers. Here is the description:
Write a function that solves the quadratic equation ax2 + bx + c = 0. The prototype of the function should be:
int quadratic(double a, double b, double c, double* r1, double* r2);
When called, the function should do the following:
o If a and b are both 0 it should return 1, indicating that this is not an equation
o If a is 0 and b is not 0, it should return 2, indicating that this is a linear equation and there is one real root. It should put the root in *r1.
o If the equation has two real roots, it should return 3 and put the two roots in *r1 and *r2.
o If the equation has two complex roots, it should return 4 and put the real part in *r1 and the imaginary part in *r2. The roots are (*r1) + j(*r2) and (*r1) – j(*r2)
Explanation / Answer
int quadratic(double a, double b, double c, double* r1, double* r2)
{
if ( a == b && b==0)
{
return 1;
}
else if(a == 0 && b!=0)
{
*r1 = (-c)/b ;
return 2;
}
else if(b*b-4*a*c >=0)
{
*r1 =( -b + sqrt(b*b-4*a*c) )/ (2*a);
*r2 =( -b - sqrt(b*b-4*a*c) )/ (2*a);
return 3;
}
else
{
*r1 = (-b)/ (2*a);
*r2 = sqrt(4*a*c-b*b) / (2*a) ;
return 4;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.