!@#$%
Your C program will do the following tasks:
i. Prompt and obtain the coefficients a, b, and c
ii. In order to determine whether the roots are complex, it will calculate the quantity b2 −4ac
iii. If this quantity is negative, then the program will print out the message “The roots are
complex”
iv. If not, the program will calculate the two real roots of the quadratic equation as
(−b + (sqrt(b2 − 4ac))/2a and
(−b −(sqrt(b2 − 4ac))/2a.
sample output:
Enter value for a:
Enter value for b:
Enter value for c:
Roots are: (((depends on values entered)))
Issue:
I have tried to do this project and regardless of what numbers I enter for a,b, and c, I get 0s for the roots in any of the situations in the project statement. also, the type is supposed to be double for the values but whenever i try that i get errors so i have left them as int for now. if someone could change them to type double and correct the issue that is giving me 0s everytime, that would be much appreciated.
Thanks!
My code:
#include <stdio.h>
#include <math.h>
void getData (int*, int*, int*);
int checkSolution (int, int, int);
int main()
{
int a,b,c;
getData (&a, &b, &c);
checkSolution (a, b, c);
return 0;
}
void getData (int* a, int* b, int* c)
{
printf(“nntQuadratic Equation: a*x^2+b*x+c = 0”);
printf(“nntEnter value for a: “);
scanf(“%d”, a);
printf(“nntEnter value for b: “);
scanf(“%d”, b);
printf(“nntEnter value for c: “);
scanf(“%d”, c);
}
int checkSolution (int a, int b, int c)
{
int d;
float root1, root2;
if(a == 0 && b == 0)
{
printf(“nnttNo Solution!”);
return -1;
}
else {
printf(“the roots of a and b are: %0.2ft %0.2f”, root1, root2);
return 1;
}
if (a == 0)
{
float root = -(c/b);
printf(“nnttOnly one solution: %0.2f”, root);
return -2;
}
else {
printf(“the roots of a and b are: %0.2ft %0.2f”, root1, root2);
return 2;
}
d = b*b-4*a*c;
if(d<0)
{
printf(“No real roots exist!”);
return -3;
}
else {
printf(“the roots of a and b are: %0.2ft %0.2f”, root1, root2);
return 3;
}
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b – sqrt(d))/(2*a);
printf(“nnttThe two roots are: %0.2f, %0.2f”, root1, root2);
return 0;
}
Expert Answer
#include <stdio.h>
#include <math.h>
double ans;
double root1 = 0, root2 = 0;
int IsComplex(double a, double b, double c)
{
ans = (b*b) – 4 * (a*c);
if (ans == 0)
return 2;
else if (ans > 0)
return 1;
else
return 0;
}
void calculate(double a, double b, double c)
{
root1 = (-b + sqrt(ans)) / (2 * a);
root2 = (-b – sqrt(ans)) / (2 * a);
printf(“nn—————————————–n”);
printf(“nThe quantity (b^2-4ac) is %.2lf”, ans);
printf(“nnRoot1 = %.2lfnRoot2 = %.2lfnn”, root1, root2);
}
void getData()
{
double a = 0, b = 0, c = 0;
printf(“Enter a: “);
scanf_s(“%lf”, &a);
printf(“nEnter b: “);
scanf_s(“%lf”, &b);
printf(“nEnter c: “);
scanf_s(“%lf”, &c);
if (IsComplex(a, b, c) == 1)
calculate(a, b, c);
else if (IsComplex(a, b, c) == 0)
{
printf(“nn—————————————–n”);
printf(“The quantity (b^2-4ac) is negative (imaginary)”);
printf(“nThe roots are complexn”);
}
else if (IsComplex(a, b, c) == 2)
calculate(a, b, c);
}
int main()
{
getData();
system(“pause”);
return 0;
}