
Patrick B. answered 03/26/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define OUTBUFF_SIZE (255)
char * RootDistance( double A, double B, double C)
{
double discriminant = B*B - 4*A*C;
char outbuff[OUTBUFF_SIZE];
if (discriminant<0)
{
strcpy(outbuff,"NO ROOTS");
}
else if (A==0)
{
strcpy(outbuff,"0");
}
else
{
double root1 = (-B + sqrt(discriminant))/(2*A);
double root2 = (-B - sqrt(discriminant))/(2*A);
//algebraically the same as sqrt(discriminant)/A
sprintf(outbuff,"%6.2f", fabs(root1-root2));
}
int N = strlen(outbuff);
char * buffReturn = (char*)malloc(N);
strcpy(buffReturn,outbuff);
return(buffReturn);
}
int main()
{
char * buff;
printf("%s \n",(buff = RootDistance(1,-6,9))); free(buff);
printf("%s \n",(buff=RootDistance(10,-1,-2))); free(buff);
printf("%s \n",(buff=RootDistance(1,0,1))); free(buff);
}