Like Tree3Likes
  • 3 Post By Salem

Quick Question

This is a discussion on Quick Question within the C Programming forums, part of the General Programming Boards category; This is for a class. I am NOT asking for someone to do this for me. I just can't find ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Quick Question

    This is for a class. I am NOT asking for someone to do this for me. I just can't find out an issue and would appreciate it if anyone could point out its cause.

    This is the program.
    Code:
    int main(void)
    {
    //Variable declaration
    
    
    double a, b, c, discriminant, r1, r2;
    
    
    //Display title prompt
    printf("This program finds the roots of the quadratic equation.\n");
    
    
    //Prompt user for coefficients of the quadratic equation
    printf("Input the a, b, and c coefficients of the quadratic equation\n in this form: (a)x^2 + (b)x + (c) \n");
    
    
    //Read user values and assign to appropriate variables
    scanf("%dx^2+%dx+%d", &a, &b, &c);
    
    
    //Find root type
    discriminant = (b*b-4*a*c);
    
    
    //Find roots using the variables and display
    
    if (discriminant>0)
    {
    r1 = -b+sqrt(discriminant)/(2*a);
    r2 = +b+sqrt(discriminant)/(2*a);
    printf("The two roots are real: ");
    printf("%d %d\n");
    }
    else if (discriminant==0)
    {
    r1 = -b+sqrt(discriminant)/(2*a);
    r2 = +b+sqrt(discriminant)/(2*a);
    printf("The two roots are equal: ");
    printf("%d %d\n");
    }
    else
    {
    r1 = -b+sqrt(discriminant)/(2*a);
    r2 = +b+sqrt(discriminant)/(2*a);
    printf("The two roots are complex and may not be correct: ");
    printf("%d %d\n");
    }
    
    
    //Termination statements
    system ("PAUSE");
    return 0;
    }
    My problem is that, no matter the variables, I always get the last else statement as the answer "The roots are complex...." even when I KNOW they're real or equal. Can't see why. Any help?
    Last edited by Salem; 10-04-2012 at 12:08 AM. Reason: old code restored

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Any guesses? I've only just started learning, so if it's a simple error, don't hold it against me.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're using the format for integers in scanf. The format for double is lf (that's a lowercase L).
    Code:
    scanf("%lfx^2+%lfx+%lf", &a, &b, &c);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    I fixed that. It just made the wrong answer longer. Changed everything to float and put %f everywhere.

    Any other ideas?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post your current code.
    EDIT:
    You are not only using the wrong format in your printf's, you also don't have any variables listed!

    So this
    Code:
    		printf("%d %d\n");
    should be
    Code:
    		printf("%f %f\n", r1, r2);
    Last edited by oogabooga; 10-03-2012 at 10:36 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Also changed discriminant to d. and some other syntax things that doesn't matter.
    Current code:
    Code:
    int main(void)
    {
    //Variable declaration
    
    
    float a, b, c, d, r1, r2;
    
    
    //Display title prompt
    printf("This program finds the roots of the quadratic equation.\n");
    
    
    //Prompt user for coefficients of the quadratic equation
    printf("Input the a, b, and c coefficients of the quadratic equation\n in this form: (a)x^2 + (b)x + (c) \n");
    
    
    //Read user values and assign to appropriate variables
    scanf("%fx^2+%fx+%f", &a, &b, &c);
    
    
    //Find root type
    d = (b*b)-(4*a*c);
    
    
    //Find roots using the variables and display
    
    if (d>0)
    {
    r1 = (-b+(sqrt(d)))/(2*a);
    r2 = (b+(sqrt(d)))/(2*a);
    printf("The two roots are real: ");
    printf("%f %f\n");
    }
    else if (d==0)
    {
    r1 = (-b+(sqrt(d)))/(2*a);
    r2 = (b+(sqrt(d)))/(2*a);
    printf("The two roots are equal: ");
    printf("%f %f\n");
    }
    else
    {
    r1 = (-b+(sqrt(d)))/(2*a);
    r2 = (b+(sqrt(d)))/(2*a);
    printf("The two roots are complex and may not be correct: ");
    printf("%f %f\n");
    }
    
    
    //Termination statements
    system ("PAUSE");
    return 0;
    }
    Last edited by Salem; 10-04-2012 at 12:09 AM. Reason: restore old code

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Quote Originally Posted by oogabooga View Post
    Post your current code.
    EDIT:
    You are not only using the wrong format in your printf's, you also don't have any variables listed!

    So this
    Code:
            printf("%d %d\n");
    should be
    Code:
            printf("%f %f\n", r1, r2);
    Oh..... lemme try fixing that.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Still wrong.

    Code:
    //code deleted
    Last edited by Lavendershoe; 10-03-2012 at 11:19 PM.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    I figured out what I needed.

    Thank you, oog. Appreciated.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Please stop deleting your code after you've got your answer.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question
    By gunghomiller in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2007, 05:38 PM
  2. quick question pls help.
    By Ment in forum C++ Programming
    Replies: 4
    Last Post: 12-24-2002, 10:44 AM
  3. quick msg box question
    By heat511 in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2002, 09:33 AM
  4. Quick Question
    By DISGUISED in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2002, 09:54 PM

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