# Help With Functions

• 02-03-2013
M_A_T_T
Help With Functions
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); }```
• 02-03-2013
c99tutorial
Quote:

Originally Posted by M_A_T_T
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?

The way you are doing it looks fine, except in your format you have 5 pieces, but your function is only handling 3:
2x^2
+
2x
+
3

You need an if/else to handle the + or minus sign. In other words, if b < 0, you need to print a minus. The same logic for coefficients of 1. If abs(b) == 1, you don't print the coefficient
• 02-03-2013
M_A_T_T
Quote:

Originally Posted by c99tutorial
The way you are doing it looks fine, except in your format you have 5 pieces, but your function is only handling 3:
2x^2
+
2x
+
3

You need an if/else to handle the + or minus sign. In other words, if b < 0, you need to print a minus. The same logic for coefficients of 1. If abs(b) == 1, you don't print the coefficient

So would it go like:
Code:

```if (b < 0) { printf("-%gx +", b); } if (b > 0) { printf("%gx +", b); } if (abs (b) == 1) { printf("x"); }```
What happens if lets say b is -3, then wouldn't it print --3?

Also when b = 1 or -1, how will it know when to print the + or - sign with the abs(b) == 1?

This was the situation I was getting stuck on.