Like Tree3Likes
  • 1 Post By Salem
  • 2 Post By grumpy

C code for quadratic formula:

This is a discussion on C code for quadratic formula: within the C Programming forums, part of the General Programming Boards category; Hi everybody. I made this code for quadratic formula but it's not working as it should. I am learning "C ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    C code for quadratic formula:

    Hi everybody. I made this code for quadratic formula but it's not working as it should. I am learning "C language" so it's very simple code.
    Important: It is working efficiently for "real and equal" and "real and equal" roots. But for "complex roots" it's not true for every value of a, b, c.
    Example: for a=1,b=2,c=3; it prints "The first root = -1 + 1.414214 i"
    and "The second root = -1 - 1.414214 i", which is accurate result.
    But for a=2,b=3,c=4; it prints correct answer for imaginary part but "0.0000" for both the real parts and wrong results for other values. Please help
    Note: Please don't post brand new code in reply, because I don't have full command on C language. Just make change where needed in my code and reply this post.
    Regards: arslan20
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    main()
    {
        int a,b,c,d;
        float x1,x2;
        printf("Enter value of a : ");
        scanf("%d",&a);
        printf("Enter value of b : ");
        scanf("%d",&b);
        printf("Enter value of c : ");
        scanf("%d",&c);
        d = pow(b,2) - 4 * a *c;
        if(d==0)
        {
            printf("\nThe roots are real and equal");
            x1 = -b/(2*a);
            printf("\nThe first root = %f",x1);
            printf("\nThe second root = %f",x1);
        }
        else
        if(d>0)
        {
            printf("\nThe roots are real and unequal");
            x1 = (-b+sqrt(d))/(2*a);
            x2 = (-b-sqrt(d))/(2*a);
            printf("\nThe first root = %f",x1);
            printf("\nThe second root = %f",x2);
        }
        else
        {
        printf("\nThe roots are complex");
        d = -d;
        x1 = -b/(2*a);
        x2 = sqrt(d)/(2*a);
        printf("\nThe first root = %f + %f i",x1,x2);
        printf("\nThe second root = %f - %f i",x1,x2);
    }
        getch();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Change a line to this
    x1 = (float)-b/(2*a);

    The problem you have is your existing expression is all done using integer arithmetic, so any fraction is lost before it gets assigned to x1
    arslan20 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Great thanks

    Thank you Salem: It worked great for me. 110% helpful and true answer. But you know I am beginner in C language so i don't understand the reason. Although you mentioned it but my level is low. It will be great if you come up to my level and give me reason that why (float) is necessary for complex roots but not for real one. Thanks once again....

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,210
    It is to do with how you did the calculations for the real roots.

    In the statements
    Code:
     x1 = (-b+sqrt(d))/(2*a);
     x2 = (-b-sqrt(d))/(2*a);
    sqrt() is a function that accepts an argument of type double, and returns a double. The compiler therefore converts d to type double, passes the result to sqrt(), which returns double. Since sqrt(d) is of type double, the value of -b is also converted to type double before adding it to sqrt(d). 2*a is computed as an int. Then since (-b + sqrt(d)) is of type double, the value of 2*a is converted to type double before doing the division. The result is that (-b+sqrt(d))/(2*a) is computed with a value of type double. x1 is of type float, so that result is converted to float and the result is stored in x1.

    A similar sequence of events occurs in the calculation of x2.

    In contrast, your calculation for complex roots
    Code:
        d = -d;
        x1 = -b/(2*a);
        x2 = sqrt(d)/(2*a);
    d = -d; is performed purely using integer arithmetic. The calculation of x1, since b and a are both of type int, computes (2*a) as an int, computes -b as an int, and then divides the two using integer arithmetic. No conversion to double occurs in that calculation. The calculation of x2, however, converts d to a value of type double, computes sqrt(d) as a value with type double, converts the integer (2*a) to double before doing the division, converts the result of the division to float in order to store it in x2.

    Salem's change
    Code:
    x1 = (float)-b/(2*a);
    forces -b to be converted to float. The compiler therefore converts the int value (2*a) to float before performing the division, which means the result of division has type float - which is then stored without conversion in x1.

    All of those conversions before performing operations result from basic rules of the C language.



    The basic concept is that all calculations involving values of type int will be done integer integer arithmetic. However, if an expression with an operation (addition, multiplication, etc) involves a value of type int and a value of type double, the int will be converted to double before performing the operation.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Right way

    Thanks grumpy: you explained it right way. You should be a teacher because of your method of explaining. well done!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program formula code problem
    By parachutes89 in forum C Programming
    Replies: 5
    Last Post: 09-14-2009, 04:13 AM
  2. quadratic formula in c
    By cakestler in forum C Programming
    Replies: 2
    Last Post: 02-08-2009, 08:41 PM
  3. Need help with quadratic formula functions
    By orikon in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2005, 04:49 PM
  4. Help on quadratic formula DOS program
    By blankstare77 in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2005, 08:57 PM
  5. Replies: 6
    Last Post: 04-04-2003, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21