This is a discussion on Quadratic Functions within the C Programming forums, part of the General Programming Boards category; Hello. I am extremely new to C. I am trying to write a simple program that will determine the zeros ...

Hello. I am extremely new to C. I am trying to write a simple program that will determine the zeros of a quadratic function. I have a couple of problems.

Code:
```#include <stdio.h>
#include <math.h>

int main ( int argc, const char * argv[] ) {

float a;  // a;
float b;  // b;
float c;  // c;
float x1;  // x1;
float x2;  // x2;
float disc;  // discriminant;

printf( "a = " );
scanf( "%f", &a );
fpurge( stdin );

printf( "b = " );
scanf( "%f", &b );
fpurge( stdin );

printf( "c = " );
scanf( "%f", &c );
fpurge( stdin );

disc = ( b * b ) - ( 4 * a * c );
x1 = ( ( -1 * b ) + sqrt(disc) ) / ( 2 * a );
x2 = ( ( -1 * b ) - sqrt(disc) ) / ( 2 * a );

if ( disc > 0 ) {
printf( "%f, %f", &x1, &x2 );
}
else if ( disc == 0 ) {

}
else if ( disc < 0 ) {
printf( "No real solutions." );
}

return 0;
}```
Once I get a, b, and c, I can determine the discriminant. If the discriminant is positive, there are 2 solutions (x1 and x2), if it is 0, there is 1 solution (x1 or x2), and if it is negative, there are no solutions. I have 2 problems.

First of all, when I test the program with an equation that should give me two numbers, it gives me 0 and 0 (and it's not supposed to be).

My other problem is when the discriminant is 0. I don't know if x1 or x2 will give me the correct answer. How can I determine which one is right?

2. Code:
```if ( disc > 0 ) {
printf( "%f, %f", &x1, &x2 );```
You are printing the addresses of x1 and x2 there, not the values.

3. Thank you! That solves that problem. I still don't know how to solve the other problem though.

4. Well your code might act weird if the discriminant is less than 0. You need to test for this in an if statement prior to calling sqrt on it.

Change the last portion of your code to this:

Code:
```    disc = ( b * b ) - ( 4 * a * c );

/* test if discriminant is negative, no need to compute x1,x2 if it is */
if(disc < 0){
printf("No real solutions.");
}
else{
/* compute solutions */
x1 = ( ( -1 * b ) + sqrt(disc) ) / ( 2 * a );
x2 = ( ( -1 * b ) - sqrt(disc) ) / ( 2 * a );

/* test if they are equal */
if(x1 == x2){
printf("x1=x2=%f",x1);
}
else{
printf("x1=%f and x2=%f",x1,x2);
}
}```

5. Oh ok, I didn't think that they would be equal, but now I see. Thank you!