Create a program that implements the functions of a quadratic equation.
I need a C code of this.
Create a program that implements the functions of a quadratic equation. I have also attached an image in what would the quadratic formula be.
STRUCTURE DEFINITION OF A QUADRATIC EQUATION
typedef struct{
float a;
float b;
float c;
}Quadratic;
Implement the following functions:
void discriminant(Quadratic *q, float a, float b, float c);
- Initialize coefficients of Quadratic q.
int isImaginary(Quadratic *q);
- Returns 1 if discriminant is less than 0, else returns 0.
float firstRoot(Quadratic *q);
- Returns the first root of the equation.
float secondRoot(Quadratic *q);
- Returns the second root of the equation.
void display(Quadratic *q);
-Prints the coefficients a, b, c.
int getValue(Quadratic q, int x);
-Given value of x, substitute it in the quadratic
-example: 2x^2 + 3x + 3 ; x = 2; therefore getvalue will return 17.
The task is to implement the functions. And to create a main code. This is the current code.
typedef struct{
float a;
float b;
float c;
} Quadratic;
void discriminant(Quadratic *q, float a, float b, float c);
int isImaginary(Quadratic *q);
float firstRoot(Quadratic *q);
float secondRoot(Quadratic *q);
void display(Quadratic *q);
int getValue(Quadratic q, int x);
Input
1. Value for a
2. Value for b
3. Value for c
4. Value for x
Output should be:
Enter·a: 2
Enter·b: 5
Enter·c: 3
Discriminant·is·not·Imaginary.
First·Root: -1.00
Second·Root: -1.50
Coefficients:
a: 2.00
b: 5.00
c: 3.00
Enter·value·of·x: 4
Equation: (2.00x^2) + (5.00x) + (3.00)
Solution: (2.00(4)^2) + (5.00(4)) + (3.00)
Result:·55
Another test case or another output should be:
Enter a: 1
Enter b: 2
Enter c: 3
Discriminant is Imaginary.
Can't calculate first root. Discriminant is Imaginary.
Can't calculate second root. Discriminant is Imaginary.
Coefficients: a: 1.00
b: 2.00
c: 3.00
Enter value of x: 4 Equation: (1.00x^2) + (2.00x) + (3.00)
Solution: (1.00(4)^2) + (2.00(4)) + (3.00)
Result: 27
Another test case or another output should be:
Enter a: -5
Enter b: 4
Enter c: 2
Discriminant is not Imaginary.
First Root: -0.35
Second Root: 1.15
Coefficients:
a: -5.00
b: 4.00
c: 2.00
Enter value of x: 3
Equation: (-5.00x^2) + (4.00x) + (2.00)
Solution: (-5.00(3)^2) + (4.00(3)) + (2.00)
Result: -31
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images