Thread: Quadratic Functions

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Quadratic Functions

    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?

    Thanks in advance!
    Last edited by zachsformacs95; 03-08-2010 at 10:23 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    if ( disc > 0 ) {
            printf( "%f, %f", &x1, &x2 );
    You are printing the addresses of x1 and x2 there, not the values.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Thank you! That solves that problem. I still don't know how to solve the other problem though.
    Last edited by zachsformacs95; 03-08-2010 at 10:22 PM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Oh ok, I didn't think that they would be equal, but now I see. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM