Quadratic Solutions in C++

Discussion in 'Computer Support' started by Sunny, Feb 25, 2006.

  1. Sunny

    Sunny Guest

    Hello

    does anyone know how to write a code in Borland C++ to find the
    solutions of a quadratic ax^2 + bx + c = 0

    for any parameter a, b, and c.

    the solutions of which can either be Real or Complex.

    (It is really the part of how to incorporate the Complex Number system
    into the code that i can not figure out.)

    any help would be much appreciated.

    thanks.
    Sunny, Feb 25, 2006
    #1
    1. Advertising

  2. Sunny

    Mike Easter Guest

    Sunny wrote:

    > does anyone know how to write a code in Borland C++ to find the
    > solutions of a quadratic ax^2 + bx + c = 0


    Is this a homework problem, or what?

    --
    Mike Easter
    Mike Easter, Feb 25, 2006
    #2
    1. Advertising

  3. Sunny

    7 Guest

    Sunny wrote:

    > Hello
    >
    > does anyone know how to write a code in Borland C++ to find the
    > solutions of a quadratic ax^2 + bx + c = 0
    >
    > for any parameter a, b, and c.
    >
    > the solutions of which can either be Real or Complex.
    >
    > (It is really the part of how to incorporate the Complex Number system
    > into the code that i can not figure out.)
    >
    > any help would be much appreciated.
    >
    > thanks.



    For students to lecturers and professors;
    boot up quantian or knoppix live dvd edition,
    run xmaxima, and it will solve your equations
    algebraically.
    Hundreds of livecds and livedvds to choose from here..
    http://www.livecdlist.com
    All open source, free and comes with source code.
    7, Feb 25, 2006
    #3
  4. Sunny

    old jon Guest

    "Sunny" <> wrote in message
    news:...
    > Hello
    >
    > does anyone know how to write a code in Borland C++ to find the
    > solutions of a quadratic ax^2 + bx + c = 0
    >
    > for any parameter a, b, and c.
    >
    > the solutions of which can either be Real or Complex.
    >
    > (It is really the part of how to incorporate the Complex Number system
    > into the code that i can not figure out.)
    >
    > any help would be much appreciated.
    >
    > thanks.
    >

    Try this one Sunny:
    http://www.overclock.net/application-programming/60094-quadratic-equations-c-style.html

    --
    bw..OJ
    old jon, Feb 25, 2006
    #4
  5. Sunny

    Rich Wilson Guest

    "Sunny" <> wrote in message
    news:...
    > Hello
    >
    > does anyone know how to write a code in Borland C++ to find the
    > solutions of a quadratic ax^2 + bx + c = 0
    >
    > for any parameter a, b, and c.
    >
    > the solutions of which can either be Real or Complex.



    What's wrong with the old
    (-b +/- sqrt (b^2 - 4*a*c)) / 2*a
    formula?

    I'd calculate the bit inside the sqrt() and see if it's negative or not,
    then write different code to handle the real and complex solutions. IIRC the
    complex solutions are still going to be of the form
    A +/- Bi
    where A and B are pretty easy to calculate using regular (real) arithmetic
    so it's mostly a question of how your code is going to display the
    solutions.
    Rich Wilson, Feb 25, 2006
    #5
  6. Sunny

    why? Guest

    On 25 Feb 2006 07:56:23 -0800, Sunny wrote:

    >Hello
    >
    >does anyone know how to write a code in Borland C++ to find the
    >solutions of a quadratic ax^2 + bx + c = 0


    Yes, it doesn't matter if it's Borland.

    <snip>

    Me
    why?, Feb 25, 2006
    #6
  7. Sunny

    Sunny Guest

    i think i have found a solution, but i keep on getting the wrong answer
    can somebody see where the error is??????????????????/

    int main()
    {
    double a, b, c;
    cout << "Please enter the three coefficients a, b and c :";
    cin >> a >> b >> c;

    double temp = b*b - 4*a*c; // temporary variable - determines
    whether roots are real or complex.

    if (temp > 0.0) //temp is positive - real roots
    {
    double Root1 = (-b + sqrt(temp)) / (2 * a);
    double Root2 = (-b - sqrt(temp)) / (2 * a);
    cout << "There are two real roots." << endl <<
    "The roots are " << Root1 << " and " << Root2 << endl;
    }
    else if (temp < 0.0) //temp is negative - complex roots
    {
    double Real = -b / (2 * a);
    double Imag = sqrt(c / a - b * b / (4 * a * a));
    cout << "There are two complex roots." << endl <<
    "The roots are " << Real << " +/- " << Imag << " j" << endl;
    }
    else //roots are both the same
    {
    double Root = -b / (2 * a);
    cout << "There are two identical roots." << endl <<
    "Both roots are " << Root << endl;
    }
    return 0;
    }
    Sunny, Feb 25, 2006
    #7
  8. Sunny

    Rich Wilson Guest

    "Sunny" <> wrote in message
    news:...
    >i think i have found a solution, but i keep on getting the wrong answer
    > can somebody see where the error is??????????????????/
    >
    > int main()
    > {
    > double a, b, c;
    > cout << "Please enter the three coefficients a, b and c :";
    > cin >> a >> b >> c;
    >
    > double temp = b*b - 4*a*c; // temporary variable - determines
    > whether roots are real or complex.
    >
    > if (temp > 0.0) //temp is positive - real roots
    > {
    > double Root1 = (-b + sqrt(temp)) / (2 * a);
    > double Root2 = (-b - sqrt(temp)) / (2 * a);
    > cout << "There are two real roots." << endl <<
    > "The roots are " << Root1 << " and " << Root2 << endl;
    > }
    > else if (temp < 0.0) //temp is negative - complex roots
    > {
    > double Real = -b / (2 * a);
    > double Imag = sqrt(c / a - b * b / (4 * a * a));


    Shouldnt't that be sqrt(-temp)/(2*a) ?
    Rich Wilson, Feb 25, 2006
    #8
  9. Sunny

    why? Guest

    On 25 Feb 2006 10:09:38 -0800, Sunny wrote:

    >i think i have found a solution, but i keep on getting the wrong answer
    >can somebody see where the error is??????????????????/


    So you make this easy by pointing out
    a) it's the real , complex or same root answer that is wrong
    b) it's a programming fault.
    c) you don't cout variables in intermediate steps to check the root1,
    root2 , real , imag , temp or anything is correct before you go on to
    use it later.
    d) no sample input with expected / actual output. This most likely will
    fix the error you have somewhere.

    >int main()
    >{
    > double a, b, c;
    > cout << "Please enter the three coefficients a, b and c :";
    > cin >> a >> b >> c;
    >
    > double temp = b*b - 4*a*c; // temporary variable - determines
    >whether roots are real or complex.
    >
    > if (temp > 0.0) //temp is positive - real roots
    > {
    > double Root1 = (-b + sqrt(temp)) / (2 * a);
    > double Root2 = (-b - sqrt(temp)) / (2 * a);
    > cout << "There are two real roots." << endl <<
    > "The roots are " << Root1 << " and " << Root2 << endl;
    > }
    > else if (temp < 0.0) //temp is negative - complex roots
    > {
    > double Real = -b / (2 * a);
    > double Imag = sqrt(c / a - b * b / (4 * a * a));


    It would help to have comments to make sure what you want / are aiming
    for as an answer is the same as the formula you are using.

    > cout << "There are two complex roots." << endl <<
    > "The roots are " << Real << " +/- " << Imag << " j" << endl;
    > }
    > else //roots are both the same
    > {
    > double Root = -b / (2 * a);
    > cout << "There are two identical roots." << endl <<
    > "Both roots are " << Root << endl;
    > }
    > return 0;
    >}



    Me
    why?, Feb 25, 2006
    #9
  10. Sunny

    Clogwog Guest

    7 wrote:
    > Sunny wrote:
    >
    >> Hello
    >>
    >> does anyone know how to write a code in Borland C++ to find the
    >> solutions of a quadratic ax^2 + bx + c = 0
    >>
    >> for any parameter a, b, and c.
    >>
    >> the solutions of which can either be Real or Complex.
    >>
    >> (It is really the part of how to incorporate the Complex Number
    >> system into the code that i can not figure out.)
    >>
    >> any help would be much appreciated.
    >>
    >> thanks.

    >
    >
    > For students to lecturers and professors;
    > boot up quantian or knoppix live dvd edition,
    > run xmaxima, and it will solve your equations
    > algebraically.
    > Hundreds of livecds and livedvds to choose from here..
    > http://www.livecdlist.com
    > All open source, free and comes with source code.


    You fucking spamtard!
    Clogwog, Feb 25, 2006
    #10
  11. Sunny

    Rick Merrill Guest

    why? wrote:
    > On 25 Feb 2006 10:09:38 -0800, Sunny wrote:
    >
    >
    >>i think i have found a solution, but i keep on getting the wrong answer
    >>can somebody see where the error is??????????????????/

    >
    >
    > So you make this easy by pointing out
    > a) it's the real , complex or same root answer that is wrong
    > b) it's a programming fault.
    > c) you don't cout variables in intermediate steps to check the root1,
    > root2 , real , imag , temp or anything is correct before you go on to
    > use it later.
    > d) no sample input with expected / actual output. This most likely will
    > fix the error you have somewhere.
    >
    >
    >>int main()
    >>{
    >> double a, b, c;
    >> cout << "Please enter the three coefficients a, b and c :";
    >> cin >> a >> b >> c;
    >>
    >> double temp = b*b - 4*a*c; // temporary variable - determines
    >>whether roots are real or complex.
    >>
    >> if (temp > 0.0) //temp is positive - real roots
    >> {
    >> double Root1 = (-b + sqrt(temp)) / (2 * a);
    >> double Root2 = (-b - sqrt(temp)) / (2 * a);
    >> cout << "There are two real roots." << endl <<
    >> "The roots are " << Root1 << " and " << Root2 << endl;
    >> }
    >> else if (temp < 0.0) //temp is negative - complex roots
    >> {
    >> double Real = -b / (2 * a);
    >> double Imag = sqrt(c / a - b * b / (4 * a * a));

    >
    >
    > It would help to have comments to make sure what you want / are aiming
    > for as an answer is the same as the formula you are using.


    Yes,and as someone else pointed out the above line should be
    double Imag = sqrt(-temp)/2*a
    for speed, accuracy and clarity.
    To fix the error add parenthesis!

    >
    >
    >> cout << "There are two complex roots." << endl <<
    >> "The roots are " << Real << " +/- " << Imag << " j" << endl;
    >> }
    >> else //roots are both the same
    >> {
    >> double Root = -b / (2 * a);
    >> cout << "There are two identical roots." << endl <<
    >> "Both roots are " << Root << endl;
    >> }
    >> return 0;
    >>}

    >
    >
    >
    > Me
    Rick Merrill, Feb 25, 2006
    #11
  12. Sunny

    why? Guest

    On Sat, 25 Feb 2006 15:47:40 -0500, Rick Merrill wrote:

    >why? wrote:
    >> On 25 Feb 2006 10:09:38 -0800, Sunny wrote:
    >>
    >>
    >>>i think i have found a solution, but i keep on getting the wrong answer
    >>>can somebody see where the error is??????????????????/

    >>
    >>
    >> So you make this easy by pointing out
    >> a) it's the real , complex or same root answer that is wrong
    >> b) it's a programming fault.
    >> c) you don't cout variables in intermediate steps to check the root1,
    >> root2 , real , imag , temp or anything is correct before you go on to
    >> use it later.
    >> d) no sample input with expected / actual output. This most likely will
    >> fix the error you have somewhere.
    >>
    >>
    >>>int main()
    >>>{
    >>> double a, b, c;
    >>> cout << "Please enter the three coefficients a, b and c :";
    >>> cin >> a >> b >> c;
    >>>
    >>> double temp = b*b - 4*a*c; // temporary variable - determines
    >>>whether roots are real or complex.
    >>>
    >>> if (temp > 0.0) //temp is positive - real roots
    >>> {
    >>> double Root1 = (-b + sqrt(temp)) / (2 * a);
    >>> double Root2 = (-b - sqrt(temp)) / (2 * a);
    >>> cout << "There are two real roots." << endl <<
    >>> "The roots are " << Root1 << " and " << Root2 << endl;
    >>> }
    >>> else if (temp < 0.0) //temp is negative - complex roots
    >>> {
    >>> double Real = -b / (2 * a);
    >>> double Imag = sqrt(c / a - b * b / (4 * a * a));

    >>
    >>
    >> It would help to have comments to make sure what you want / are aiming
    >> for as an answer is the same as the formula you are using.

    >
    >Yes,and as someone else pointed out the above line should be
    > double Imag = sqrt(-temp)/2*a
    >for speed, accuracy and clarity.
    >To fix the error add parenthesis!


    Hence my remark placed at a *strategic* point, correcting the errors in
    the maths doesn't help the OP really solve the problem, it's a fix sure
    but a little double checking / understanding of the equation works much
    better.
    More so since the programming aspect isn't an issue, it's only a wrapper
    around the maths and is so simple it's less likely to be the cause of
    the wrong answers.

    <snip>

    Me
    why?, Feb 25, 2006
    #12
  13. Sunny

    7 Guest

    Clogwog wrote:

    > 7 wrote:
    >> Sunny wrote:
    >>
    >>> Hello
    >>>
    >>> does anyone know how to write a code in Borland C++ to find the
    >>> solutions of a quadratic ax^2 + bx + c = 0
    >>>
    >>> for any parameter a, b, and c.
    >>>
    >>> the solutions of which can either be Real or Complex.
    >>>
    >>> (It is really the part of how to incorporate the Complex Number
    >>> system into the code that i can not figure out.)
    >>>
    >>> any help would be much appreciated.
    >>>
    >>> thanks.

    >>
    >>
    >> For students to lecturers and professors;
    >> boot up quantian or knoppix live dvd edition,
    >> run xmaxima, and it will solve your equations
    >> algebraically.
    >> Hundreds of livecds and livedvds to choose from here..
    >> http://www.livecdlist.com
    >> All open source, free and comes with source code.

    >
    > You fucking spamtard!


    But you are not of course?
    7, Feb 26, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Silverstrand
    Replies:
    10
    Views:
    1,112
    XhArD
    Oct 5, 2005
  2. Harrison Midkiff

    Configuring an Enterprise wireless solutions with encryption

    Harrison Midkiff, Jul 2, 2004, in forum: Wireless Networking
    Replies:
    0
    Views:
    1,282
    Harrison Midkiff
    Jul 2, 2004
  3. The Group

    ---> $5 Prestigious Virtual Office Solutions (Singapore)

    The Group, Dec 29, 2003, in forum: Microsoft Certification
    Replies:
    0
    Views:
    1,525
    The Group
    Dec 29, 2003
  4. Simon

    Microsoft Solutions Framework

    Simon, Feb 4, 2004, in forum: Microsoft Certification
    Replies:
    2
    Views:
    700
    Simon
    Feb 12, 2004
  5. 3133t3

    Laptop Training Solutions San Diego

    3133t3, Sep 15, 2004, in forum: Microsoft Certification
    Replies:
    1
    Views:
    514
    Guest
    Sep 15, 2004
Loading...

Share This Page