Thread: quadratic equation

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    quadratic equation

    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;
    }

    Thank's in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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);	
    }
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Don't forget the other one.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation cpp program problems
    By faluiretoread in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2008, 03:22 PM
  2. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM
  3. Replies: 8
    Last Post: 03-10-2008, 07:15 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