Thread: Quadratic Equation Program

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Quadratic Equation Program

    Guys Please Help! I am having problems with this program. I can't clear an error that I'm getting, but I'm not even sure that this program takes care of what was assigned. Let me know if you can provide an assistance!
    Below is the assignment and what I have written. Bare with me, I'm a beginner!

    The real roots of a quadratic equation can be calculated using the quadratic formula as:
    _______
    root 1 = (-b + b2 - 4ac ) / 2a
    _______
    root 2 = (-b - b2 - 4ac ) / 2a

    However, if a user entered a value of zero for a, the division by 2a would result in an error when the program is run. Another error occurs when the value of the term b2 - 4ac, which is called the discriminant, is negative, because the square root of a negative number cannot be taken.

    Using the algorithm below, write a C program that solves for the two roots of a quadratic equation.

    display a program purpose message
    display a prompt for the coefficients
    accept user input values for a, b, and c
    if a = 0 and b = 0 then
    display a message saying that the equation is degenerate (has no solution)
    else if a = 0 then
    calculate the single root equal to -c / b
    display the single root
    else
    calculate the discriminant
    if the discriminant > 0 then
    solve for both roots using the quadratic formula
    display the two roots
    else if the discriminant < 0 then
    display a message that there are no real roots
    else
    calculate the repeated root equal to -b / (2a)
    display the repeated root
    endif
    endif*/




    #include <stdio.h>
    #include <math.h>

    void main ()

    {
    int A, B, C, D, single_root, repeated_root;
    double root1, root2;
    printf("Please enter 3 integers separated by a space: ");
    printf("These numbers will be calculated as A, B and C in a quadratic equation and the output will be the two calculated roots.");

    scanf("%d,%d,%d,",&A,&B,&C);

    if (A==0&&B==0)
    printf ("Since 0 was your input for A and B, the equation has no solution.");


    if(A==0)
    { (single_root= -C/B);
    printf("Given the value of %d and %d, the single root is equal to %d", C,B,single_root);
    }
    else (D=B^2-4*A*C);

    {
    if (D>0)
    { (root1=(-B+sqrt(B^2-4*A*C))/(2*A))
    (root2=(-B-sqrt(B^2-4*A*C))/(2*A));
    printf("The two roots are %d and %d.",root1, root2);
    }
    else if (D<0)
    printf ("There are no real roots.");
    else (repeated_root = -B/(2*A));
    printf ("The repeated root is %d.", repeated_root);
    }

    }

    Let me know of anything that might help me, I don't have anyone I can ask for help on this, so I'm hoping any of you guys can help me!
    Thanks =)

  2. #2
    Unregistered
    Guest
    There are some errors.

    First in C square of B is not B^2 use B*B instead.

    Then you calculate D and do no use it.

    root1=(-B+sqrt(D))/(2*A);

    At the end of
    (root1=(-B+sqrt(B^2-4*A*C))/(2*A))
    you need a ";".

    In order to display floats use %f

    printf("The two roots are %f and %f.",root1, root2);

    There are some other mistakes that you'll find yourself

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Thank you! Those definitely helped. I now have 0 errors, but when i execute the file, it skips right to the last "if" statement no matter what i set A, B, and C equal to. Any suggestions?

    Thanks again!!

  4. #4
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    ....For starters your might try editing:

    if (A==0&&B==0)
    printf ("Since 0 was your input for A and B, the equation has no solution.");
    and changing it to:

    Code:
    if ((A==0) && (B==0))
    { 
    printf ("Since 0 was your input for A and B, the equation has no solution.");
    }
    Use the extra paranths to check for double cases like that....to make sure the order gets down...You want to check A, then Check B...your code might have read that all as one large statement and gotten things mixed with .. Also pay close attention to your {curly braces}. Your program has them running all over the place...I think it makes things easier to read and understand if you begin each use them to open and close all of your if and else statements. good luck.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Thank you "saves the day"! That did help, and yeah, my curly braces were goin nuts...
    I turned it in yesterday... cross your fingers!
    Thanks to you guys who helped =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM