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.
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.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; }
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?
Thanks in advance!



LinkBack URL
About LinkBacks


