• 12-26-2008
nynicue
I do not understand why this program gives me wrong result, if anyone can help I would be very appriciated
Code:

```#include<stdio.h> float a,b,c; float D; // descriminant float root_1, root_2,sqroot; void get_quation(void) {         printf("Enter numbers: a,b,c : \n");         scanf("%f%f%f",&a,&b,&c); } float find_descriminant(void) {                 D=b*b-4*a*c;         return D; }  float absolute_value(float x) {         if(x<0)                 x=-x;         return x; } float find_squareroot(float x) {         const float eplison=.00001;         float guess=1.0;         while(absolute_value(guess*guess-x)>=eplison)                 guess=(x/guess+guess)/2.0;         return guess; } int main(void) { get_quation(); find_descriminant(); if(D>0) {         sqroot=find_squareroot(D);                 root_1=(-b+sqroot)/2*a;                 root_2=(-b-sqroot)/2*a;         printf("Square roots is: %f and %f\n", root_1,root_2);        } else if (D==0) {         root_1=root_2=-b/2*a;         printf("The root is: \n", root_1); } else {         printf("Roots are imaginary\n"); } return 0; }```

• 12-26-2008
matsp
Code:

``` float absolute_value(float x) {         if(x<0)                 x=-x;         return x; }```
There is a standard function, fabs() thad does the same thing as this.

Code:

```float find_squareroot(float x) {         const float eplison=.00001;         float guess=1.0;         while(absolute_value(guess*guess-x)>=eplison)                 guess=(x/guess+guess)/2.0;         return guess; }```
And this one called sqrt() [except it most often does it as a single instruction].

As to why it gives the wrong value, you probably have to tell us what input values you are using, and what the results are that you are seeing.

--
Mats
• 12-26-2008
Dino
Precedence is biting you.

Code:

```if(D>0) {         sqroot=find_squareroot(D);                 root_1=(-b+sqroot)/(2*a) ;                 root_2=(-b-sqroot)/(2*a) ;         printf("Square roots is: %f and %f\n", root_1,root_2);        }```
• 12-26-2008
nynicue
Code:

```Precedence is biting you. Code: if(D>0) {         sqroot=find_squareroot(D);                 root_1=(-b+sqroot)/(2*a) ;                 root_2=(-b-sqroot)/(2*a) ;         printf("Square roots is: %f and %f\n", root_1,root_2);        }```
Thank you for your help Dino, it worked.
• 12-26-2008
Dino
Don't forget the other one.
• 12-26-2008
nynicue
Sorry matsp I forgot to thank you, I very appriciate for all willingness to help us beginners.
I am glad there is forums like this one.