Thread: Quadratic Equation

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Exclamation Quadratic Equation

    This is my code:
    Code:
    #include <math.h>
    #include <stdio.h>
    
    
    int main(void)
    {
    
    
    float a,b,c,root_1,root_2;
    
    
    printf("Please enter value a from the quadratic equation \n");
    scanf("%f",&a);
    
    
    printf("Please enter value b from the quadratic equation \n");
    scanf("%f",&b);
    
    
    printf("Please enter value c from the quadratic equation \n");
    scanf("%f",&c);
    
    
    double disc = pow(b,2) - 4*a*c;
    
    
    if ((disc)>0)
    {
    
    
    root_1 = (-b + sqrt(disc))/(2*a);
    root_2 = (-b - sqrt(disc))/(2*a);
    
    
    printf("\n The first root is %.2f",root_1);
    printf("\n The second root is %.2f",root_2);
    
    
    }
    
    
    else
    {
    
    
    printf("\n The roots are imaginary because the discriminant is negative!");
    
    
    }
    
    
    return 0;
    }
    And I keep getting this error:

    Code:
    /tmp/ccgtUIun.o: In function `main':
    assign345.c:(.text+0xc7): undefined reference to `sqrt'
    assign345.c:(.text+0xef): undefined reference to `sqrt'
    collect2: ld returned 1 exit status



    PLEASE HELP!!!

  2. #2

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    You need to link with the math library. Adding the compiler switch -lm works for most compilers

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I dont understand, can you please show me?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    How do I link with the math library??

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    AS it said in the other thread, add "-lm" to your linker flags.

    You're probably using some command line to compile/link your program. Add the string "-lm" (without the quotes) to that command line.

    If you're using an IDE (Integrated Development Environment) search through build options for linker options, and add "-lm" there.

    You could have worked this out quite easily be simply reading documentation for whatever program you're using to compile your source code, and build your program.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation solving
    By narendrav in forum C Programming
    Replies: 6
    Last Post: 05-18-2011, 08:49 AM
  2. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  3. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM
  4. Quadratic Equation problems
    By Rachel228 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 02:42 AM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM