Hello I am creating a program that solves the quadratic equation ax^2 + bx +c.
I have this program almost complete except the output of the equation in the function called display_quadratic. I need the program to display the variables a,b,c in the equation ax^2 + bx + c but I am having 2 problems. My first problem is that I cannot get the right addition and subtraction signs for the equation.
For instance, if the program had the values for a,b,c as 2,2,3
it will display 2x^2 2x 3
How can I get it to display 2x^2 + 2x + 3? or if it was negative like 2x^2 - 2x - 3?
My next question is how do I get to not display the coefficients that are 1?
I had an if-else statement but no matter what I created it would overlap with another statement and print out twice. Any help is appreciated and welcomed. Thank you.
Here is the code:
Code:#include <stdio.h> #include <stdlib.h> #include <math.h> void display_quadratic (float a, float b, float c); float root1(float a, float b, float c); float root2(float a, float b, float c); float discriminant (float a, float b, float c); float r1,r2; int main (void) { float a,b,c; printf("This program solves the quadratic equation ax^2 + bx + c == 0.\n"); printf("Enter a, b, and c:"); scanf("%g%g%g", &a, &b, &c); printf("OK, the equation you're specifying is:"); display_quadratic(a,b,c); printf(" == 0\n"); r1 = root1(a,b,c); r2 = root2(a,b,c); printf("The roots of your equation are %g and %g, and the factorization is:\n ", r1,r2); display_quadratic(a,b,c); printf(" == "); if (r1 > 0) { printf("(x - %g)", r1); } else printf("(x + %g)", (r1 * -1)); if (r2 > 0) { printf("(x - %g)", r2); } else printf("(x + %g)", (r2 * -1)); } void display_quadratic (float a, float b, float c) { if (a == 1){ printf("x^2 ");} else printf("%gx^2 ", a); if (b == 1){ printf("x ");} else printf("%gx ", b); printf("%g ", c); } float discriminant (float a, float b, float c) { return (pow(b,2) - 4 * a * c); } float root1 (float a, float b, float c) { float root1_calculator; root1_calculator = (-b + sqrt(discriminant(a,b,c)))/2 * a; return (root1_calculator); } float root2 (float a, float b, float c) { float root2_calculator; root2_calculator =(-b - sqrt(discriminant(a,b,c)))/2 * a; return (root2_calculator); }



LinkBack URL
About LinkBacks



