undefined reference to function

i have written a code to find the roots of a quadratic equation using functions passing 2 pointer parameters.

when i compile the code i get this error

/tmp/cclQJtoD.o: In function `quad':
ex11-2.c:(.text+0x14d): undefined reference to `sqrt'
ex11-2.c:(.text+0x17b): undefined reference to `sqrt'
collect2: ld returned 1 exit status


please tell why do i see this error, i have included the header file "math.h" also.
good job posting the error, could you include the code as well?
Make sure to link against the math library (-lm if using gnu)

#include<stdio.h>
#include<math.h>
int main()
{
int i;
float coef[3], roots[2];
float *c, *r;
r=roots;
float * quad (float *);
printf("enter the values of coefficients a,b,c\n");
for (i=0;i<3;i++)
{
scanf("%f",&coef);
}
c=coef;
r=quad(c);
printf("\n the roots are : %f, %f \n",r,r+1);
return 0;
}
float *quad(float *x)
{
float a,b,c,d;
double t;
float r[2],*rp;
a=*x;
printf("value of a : %f ",a);
b=*(x+1);
printf("value of b : %f ",b);
c=*(x+2);
printf("value of c : %f ",c);
d=pow(b,2)-(4*a*c);
printf("value of d : %f ",d);
if(d>0)
{
r[0]=(-b+sqrt(d))/(2*a);
r[1]=(-b-sqrt(d))/(2*a);
rp=r;
return rp;
}
else {
printf("roots imaginery\n");
exit(0);
}
}

thats the code

putting a -lm works

but still i get that warning

and my program also does not give the correct output, it gives garbage values for the roots

i think i have messed up with the return pointers, can u pls help me on that as well
Your quad function is returning a pointer (array) that is allocated on the stack inside quad. You can't do that.

Problem #2 is that you are printing out r and r+1, both of which are pointers to floats, not floats.

Problem #3 is that you are using floats which have less precision than doubles.

Problem #4 is that floating point arithmetic is black magic due to roundoff error.
I refer you to http://docs.sun.com/source/806-3568/ncg_goldberg.html which has
an excellent treatise on the best way to reduce floating point roundoff error in the quadratic formula.
Topic archived. No new replies allowed.