A C program is required to calculate the quadratic equation
A C program is required to calculate the quadratic equation, but which two solutions are given by the formula: x1,2= b±b 24 ac/ 2a (1)
it must use a function called CorrectSolutions() that is used to compute the real solutions of the variables represented by the quadratic equations
it also must use : int computeSolutions(double a, double b, double c, double* px1, double* px2);
if b 24ac<0 then the solutions are complex numbers (i.e. not real) and the function returns 0. *px2 and *px2 are set to 0. •
if b 24ac=0 then x1=x2 and the function returns 1, after writing in *px1 the value of solution x1 and in *px2 the value of x2 (these are equal). •
if b 24ac>0 then the solutions are distinct and the function returns 2, after writing in *px1 the value of x1 and in *px2 the value of x2.
a function named WriteCoefficients (double* pa, double* pb, double* pc) which reads from the terminal a,b and c
lastly, the main() function that reads in a loop sequences of 3 coefficients in local variables a, b, c using the readCoefficients() function as long as the user enters three numbers
output supposed to look like :
1 2 1 -- 1 real solution: x1=x2=-1.00000 3 0 1 -- no real solutions
3 0 -1 -- 2 real solutions: x1=-0.57735 x2=0.57735
Solution
#include<stdio.h>
#include<math.h>
int CorrectSolutions(double a, double b, double c, double* px1, double* px2,int determinant);
int computeSolutions(double a, double b, double c, double* px1, double* px2);
int WriteCoefficients(double* pa, double* pb, double* pc);
void readCoefficients();
int main()
{
readCoefficients();
}
void readCoefficients()
{
char ch;
double a, b, c, root1,root2, realPart, determinant,imaginaryPart,x1,x2;
int ret;
do
{
WriteCoefficients(&a,&b,&c);
ret = computeSolutions(a,b,c,&x1,&x2);
CorrectSolutions(a,b,c,&x1,&x2,ret);
switch(ret)
{
case 0:
printf(\"Solutions are complex numbers \ \");
/*realPart = -b/(2*a);
imaginaryPart = sqrt(-determinant)/(2*a);
printf(\"root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi\", realPart, imaginaryPart, realPart, imaginaryPart);*/
break;
case 1:
printf(\"Value of px1 = px2 = %lf \ \",x1);
break;
case 2:
printf(\"the solutions are distinct \ \");
printf(\"px1= %lf px2= %lf\ \",x1, x2);
break;
default:
break;
}
printf(\"Want to continue or enter q to quit \ \");
fflush(stdin);
scanf(\"%c\",&ch);
}while(ch!=\'q\');
printf(\"Quitting.....\ \");
return;
}
int WriteCoefficients(double* pa, double* pb, double* pc)
{
printf(\"Enter the coefficients a, b , c : \");
scanf(\"%lf%lf%lf\",pa,pb,pc);
return 0;
}
int computeSolutions(double a, double b, double c, double* px1, double* px2)
{
double determinant,root1, root2;
determinant = b*b-4*a*c;
// condition for real and different roots
if (determinant > 0)
{
return 2;
}
else if (determinant == 0)
{
return 1;
}
// if roots are not real
else
{
return 0;
}
}
int CorrectSolutions(double a, double b, double c, double* px1, double* px2,int ret)
{
double realPart, imaginaryPart, determinant;
determinant = b*b-4*a*c;
if( ret > 0 )
{
if (ret == 1)
{
*px1 = *px2 = -b/(2*a);
}
else
{
*px1 = (-b+sqrt(determinant))/(2*a);
*px2 = (-b-sqrt(determinant))/(2*a);
}
}
else
{
*px1 = 0;
*px2 = 0;
//realPart = -b/(2*a);
//imaginaryPart = sqrt(-determinant)/(2*a);
printf(\"no real solutions\ \");
//printf(\"x1 = %0.2lf+%0.5lfi and x2 = %.2f-%0.5fi\", realPart, imaginaryPart, realPart, imaginaryPart);
//printf(\"x1 = %0.5lf and x2 = %0.5f\", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}


