Quadratic equations, need some help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaRok28
    New Member
    • Feb 2007
    • 8

    Quadratic equations, need some help

    // Program Description:
    // This program solves quadratic equations to find their roots. This
    // program takes values of a, b, and c as input and outputs the root(s).
    // The user can repeat the calculation for as many equations as they like.

    Code:
    #include <iostream>
    #include <cmath>
    #include <complex>
    using namespace std;
    
    int main()
    {
       char cAgain;
       int iDataset = 1;
       double dRoot1, dRoot2, dValuea, dValueb, dValuec, dDescriminant;
    
       cout.setf(ios::fixed);
       cout.setf(ios::showpoint);
       cout.precision(4);
    
       do
       {
          cout << endl << "****************" << endl
               << "* Data Set " << iDataset << " *" << endl
               << "****************" << endl << endl;
          cout << endl << "Please enter coefficients for quadratic equation"
               << " ax^2 + bx + c = 0" << endl;
          cout << endl << "Value of 'a' : ";
          cin >> dValuea;
          cout << "Value of 'b' : ";
          cin >> dValueb;
          cout << "Value of 'c' : ";
          cin >> dValuec;
          dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
          dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
          dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
    
             if(dDescriminant < 0)
             {
                cout << endl << "You have entered the equation " << dValuea
                     << "x^2 + " << dValueb << "x +" << dValuec
                     << " = o" << endl;
                cout << "The root to this equation is: " << endl << endl
                     << dRoot1 << " i  and" << endl << dRoot2 << endl << endl;
    
    
             }
             else if(dDescriminant == 0)
             {
                cout << endl << "You have entered the equation " << dValuea
                     << "x^2 + " << dValueb << "x +" << dValuec
                     << " = 0" << endl;
                cout << "The root to this equation is: " << endl << endl
                     << dRoot1 << endl;
             }
             else
             {
                cout << endl << "You have entered the equations " << dValuea
                     << "x^2 + " << dValueb << "x +" << dValuec
                     << " = o" <<endl;
                cout << "The root to this equations is: " << endl << endl
                     << dRoot1 << " and" << endl << endl << dRoot2 << endl;
             }
          cout << endl << "Want to do this again? Y/y/N/n" << endl << endl;
          cin >> cAgain;
          iDataset++;
       }while (cAgain == 'Y' || cAgain == 'y');
    
       return 0;
    
    }
    My else if and else statements both work the way I want them too, but I need my if tatement to do negative numbers. When I enter the values for a, b, c as
    1, 4, a nd 7 respectfully I need the answer to be -2.0000 - 1.7321 i and -2.0000 + 1.7321 i. I am just not sure how to do this, so if anyone has any suggestions.
    Last edited by horace1; Mar 5 '07, 03:52 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you need to test
    Code:
         dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
             if(dDescriminant < 0)
             {
    before you calculate dRoot1 and dRoot2

    if dDescriminant>0 is real rools (note your calculation is not quite correct)
    Code:
          dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
          dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
    if dDescriminant< 0
    the real part is
    Code:
            - dValueb /( 2 * dValuea)
    the imaginary parts are
    Code:
       + and -   sqrt(-dDescriminant) / (2 * dValuea)

    Comment

    • DaRok28
      New Member
      • Feb 2007
      • 8

      #3
      Thanks, I am not sure if what I did is correct, but i went off of what you told me and it worked, so thanks

      Originally posted by horace1
      you need to test
      Code:
           dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
               if(dDescriminant < 0)
               {
      before you calculate dRoot1 and dRoot2

      if dDescriminant>0 is real rools (note your calculation is not quite correct)
      Code:
            dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
            dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
      if dDescriminant< 0
      the real part is
      Code:
              - dValueb /( 2 * dValuea)
      the imaginary parts are
      Code:
         + and -   sqrt(-dDescriminant) / (2 * dValuea)

      Comment

      Working...