Solution: Compute the Roots

Solution: Compute the Roots

C
#include <stdio.h>
#include <math.h>
double * solveEquation(double * myInput)
{
double a, b, c, x1, x2;
a = myInput[0];
b = myInput[1];
c = myInput[2];
x1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
x2 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
myInput[3] = x1;
myInput[4] = x2;
return myInput;
}
int main()
{
double data[5];
/* coefficients of ax^2 + bx + c = 0 */
data[0] = 1.0;
data[1] = -5.0;
data[2] = 6.0;
solveEquation(data);
printf("Root 1: %f\n", data[3]);
printf("Root 2: %f\n", data[4]);
return 0;
}