Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define and test a function called isQuadratic that finds the solutions of a quad

ID: 3782572 • Letter: D

Question

Define and test a function called isQuadratic that finds the solutions of a quadratic equation:

ax^2 + bx + c = 0

Make it a Boolean function that returns true if real solutions exist and false otherwise. Pass the coefficients a, b, and c by value. Place the calculated solutions into two variables passed to the function by reference.

Recall that the formulas for the solutions of a quadratic equation are:

x1 = -b + sqrt(b^2 - 4ac) / 2a

x2 = -b - sqrt(b^2 - 4ac) / 2a

a. You will need to use the sqrt(double z) standard library function for calculating the square root. It is declared in math.h.

b. Your code must get a, b, and c as input.

c. If x1 and x2 are real numbers print them to the output screen

d. If x1 and x2 are not real numbers print "Calculated numbers x1 and x2 are not real numbers. They are imaginary!"

Explanation / Answer

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool isQuadratic(double a, double b, double c, double *x1, double *x2)
{
bool is_real = 1;
double determinant = (b*b) - 4*a*c;

if (determinant >= 0)
{
*x1 = (-1*b + sqrt(determinant))/(2*a);
*x2 = (-1*b - sqrt(determinant))/(2*a);
}
else
{
is_real = 0;
}
return is_real;
}

int main()
{
double a, b, c;

printf("Quadratic equation is of form: ax^2 + bx + c = 0 ");
printf("Enter coefficient a: ");
scanf("%lf", &a);

printf("Enter coefficient b: ");
scanf("%lf", &b);

printf("Enter coefficient c: ");
scanf("%lf", &c);

double x1;
double x2;
bool is_real = isQuadratic(a, b, c, &x1, &x2);

if (is_real)
{
printf("x1 : %f, x2 : %f ", x1, x2);
}
else
{
printf("Calculated numbers x1 and x2 are not real numbers. They are imaginary! ");
}
return 0;
}

/*

output

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote