A quadratic equation is in the form “ax 2 + bx + c = 0”, where a, b, and c repre
ID: 3818877 • Letter: A
Question
A quadratic equation is in the form “ax2 + bx + c = 0”, where a, b, and c represent floating-point values. Write a program that asks the user for the three values and output the result.
## FOR C ##
The program should contain two functions as follows:
Function “Square” which takes three float type parameters and return a float corresponding to b2 -4*a*c.
Function “numSol” which take three float parameters and two float pointer parameters, this function has no return.
The program should output a statement indicating the number of possible solutions and depending the number of solutions, output the proper number of statements for them.
do not repeat any calculations, meaning that function numSol need to call function Square instead of repeating the calculations done in numRoot.
Explanation / Answer
#include <stdio.h>
#include <math.h>
float Square(float b, float a, float c)
{
return (b*b - (4*a*c));
}
// Prints roots of quadratic equation ax*2 + bx + x
void numSol(float a, float b, float c, float *sol1, float *sol2)
{
// If a is 0, then equation is not quadratic, but
// linear
if (a == 0)
{
printf("Invalid");
return;
}
int d = Square(b, a, c);
double sqrt_val = sqrt(abs(d));
if (d > 0)
{
printf("Roots are real and different ");
*sol1 = (double)(-b + sqrt_val)/(2*a);
*sol2 = (double)(-b - sqrt_val)/(2*a);
printf("Sol1 = %f, Sol2 = %f ", *sol1, *sol2);
}
else if (d == 0)
{
printf("Roots are real and same ");
*sol1 = -(double)b / (2*a);
*sol2 = -(double)b / (2*a);
printf("Sol1 = %f, Sol2 = %f ", *sol1, *sol2);
}
else // d < 0
{
printf("Roots are complex ");
printf("sol1 = %f + i%f, sol2 = %f + i%f ", -(double)b / (2*a), sqrt_val, -(double)b / (2*a), sqrt_val);
}
}
// Driver code
int main()
{
float a = 1, b = -7, c = 12;
float sol1, sol2;
numSol(a, b, c, &sol1, &sol2);
return 0;
}