Exercise: Compute the roots

Write code to solve the problem.

Question

Write a program that computes the (two) roots of a quadratic equation:

ax2+bx+cax^2 +bx +c

where aa = 1.2, bb = 2.3 and cc = −3.4.

Hint: You can hard-code values of aa, bb and cc and then compute and print the two solutions for xx, to five decimal places.

Note: We included the header file math.h at the top. With this inclusion, you can compute the square root of a number stored in a variable x using the syntax sqrt(x).

Exercise: Compute the roots

Write code to solve the problem.

Question

Write a program that computes the (two) roots of a quadratic equation:

ax2+bx+cax^2 +bx +c

where aa = 1.2, bb = 2.3 and cc = −3.4.

Hint: You can hard-code values of aa, bb and cc and then compute and print the two solutions for xx, to five decimal places.

Note: We included the header file math.h at the top. With this inclusion, you can compute the square root of a number stored in a variable x using the syntax sqrt(x).

C
#include <stdio.h>
#include <math.h>
double * solveEquation(double * myInput)
{
double a, b, c, x1, x2;
/* Don't worry if you don't understand the line 4
and the next three lines for now. We are assigning
the values to a, b, and c of the equation. */
a = myInput[0];
b = myInput[1];
c = myInput[2];
/* Write your code below to solve the equation
and save the results in x1 and x2. */
// Ignore the next few lines for now
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;
}