Quadraatic Formula Program Problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Quadraatic Formula Program Problem

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    2

    Quadraatic Formula Program Problem

    Okay, I'm developing a program that will solve quadratic equations for users. I have them plug in values for a, b, and c in the following formula: ax^2 + bx +c. This formula probably looks familiar to you guys from highschool. Anyways, for some reason my code outputs completely inaccurate results. Here is my code:

    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    //variable declarations

    int aval;
    int bval;
    int cval;
    int root;
    int preroot;
    double sol1;
    double sol2;

    //a user inputs an integer for each of the specified values

    cout<< "a = ";
    cin >> aval;
    cout<<"b = ";
    cin>>bval;
    cout<<"c = ";
    cin>>cval;

    //number crunching


    sol1 = (-bval + sqrt(bval*bval - 4*aval*cval))/(2*aval);
    sol2 = (-bval - sqrt(bval*bval - 4*aval*cval))/(2*aval);

    //output

    cout<<"Your solutions are: x = ";
    cout<<sol1;
    cout<<" or x = ";
    cout<<sol2<< endl;
    system("PAUSE");
    return 0;
    }

    For example, the equation 4x^2 + 5x + 1 comes up with x = -4 and x = -16 when the answers are clearly -1 and -1/4. I'm probably making some really stupid mistake or maybe i messed up the formula but i can't seem to figure it out! Anyways, some help would be nice. And please be gentle i'm new to this. Thanks!

    PS. I'm plugging the user input into the quadratic formula just in case you were confused. The quadratic formula can be found here: http://mste.illinois.edu/exner/ncsa/quad/

  2. #2
    Join Date
    Jul 2011
    Posts
    3

    Re: Quadraatic Formula Program Problem

    A possible problem is that ints don't divide very well. E.g. 1 divided by 2 will give you zero (0.5 with the .5 chopped off).

    Try it with floats as the input and output.

    Also a good principle when doing any maths in C++ is for the answer and every input to all be the same datatype, otherwise you will often get unexpected results.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Quadraatic Formula Program Problem

    As has been suggested the problem is integer division. Switch to doubles throughout.

    And note that 2 is an integer whereas 2.0 is a double.

    Another problem is that you must make sure that sqrt isn't called with a negative parameter. This happens when the quadratic formula has imaginary solutions.
    Last edited by nuzzle; July 20th, 2011 at 07:10 AM.

  4. #4
    Join Date
    Jul 2011
    Posts
    2

    Re: Quadraatic Formula Program Problem

    Thank you very much. I changed everything to doubles including the non variable values as well and i got the correct answers. This should make math homework go by a lot faster Thanks to both of you! Cheers!

    -MiniatureBeast

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Quadraatic Formula Program Problem

    Quote Originally Posted by MiniatureBeast View Post
    This should make math homework go by a lot faster
    Don't forget that some teachers will take off points if you don't write down every step.

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Quadraatic Formula Program Problem

    Quote Originally Posted by Lindley View Post
    Don't forget that some teachers will take off points if you don't write down every step.
    And, if you miss something in one step, but carry the issue forward, some teachers will give you partial credit (i.e. not penalize you full points because you wrote a '8' down instead of a '7').

    Viggy

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Quadraatic Formula Program Problem

    Quote Originally Posted by MiniatureBeast View Post
    the non variable values
    They're called literals.

    Good luck.

  8. #8
    Join Date
    Jul 2011
    Posts
    1

    Re: Quadraatic Formula Program Problem

    Honestly I didn't read much of what people said, I just read part of your first post, BUT
    there is something I'd like to say

    since sqrt(bval*bval - 4*aval*cval))/(2*aval) is needed to find x AND y
    you should do something like

    d = sqrt(bval*bval - 4*aval*cval))/(2*aval)

    x = -bval + d
    y = -bval - d

    just copy pasted parts of your code, didn't check them for syntax / semantical errors

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center