Adblock breaks this site

need help with c++ program

Discussion in 'Programming General' started by froxy, Sep 19, 2009.

  1. froxy

    froxy Newcomer

    Joined:
    Sep 19, 2009
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0
    need help with c++ program

    I need some help with a c++ program for my class, i can't figure out whats going on.


    Code:
    Sample Input 	Sample Output
    1, -3, -10 	-2, 5
    3, 6, 3 	-1
    2, -2, -12 	-2, 3
    3, -2, 20 	NO REAL ROOTS
    Here is so far what ive got

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        double a, b, c, d;
        double root1, root2, temp;
        
        //output
        
        cout << "enter a, b, c";
        cin >> a >> b >> c;
        
        //process
        
        d = pow(b,2) - 4 * a * c;
        if(d >= 0)
        {
              root1 = (-b + sqrt(d))/(2 * a);
              }
              if(d > 0)
              {
                     root2 = (-b + sqrt(d))/(2*a);
                     }
                                      if(root1 > root2)
                     {
                                    temp = root1;
                                    root1 = root2;
                                    root2 = temp;
                                    }
    
                                    
                                    // output
                                    
                                    if(d >= 0)
                                    {
                                            cout << root1 << " ";
                                            }
                                            if(d > 0)
                                            {
                                                   cout << root2 << " " <<endl;
                                            }
                                            if(d < 0)
                                            {
                                                   cout <<"No Real Roots" << " ";
                                                   }
                                                   cin.get();
                                                   cin.get();
                                                   return 0;
                                                   }
    
    Can someone help me out with the sample output. i get the 2nd integer, but i dont get the first.
     
  2. OnceUponATimeInMexico

    OnceUponATimeInMexico Active Member

    Joined:
    Sep 11, 2007
    Posts:
    158
    Referrals:
    0
    Sythe Gold:
    0
    need help with c++ program

    Erm... i'm not that advanced in programming, but have studied quadratic formulas abit...

    Tell me if i'm wrong, but you have worked out root one and root two as the same.

    root1 = (-b + sqrt(d))/(2 * a);
    root2 = (-b + sqrt(d))/(2*a);

    doesn't one of them need to be a minus?

    root2 = (-b - sqrt(d))/(2*a);

    If not it's probably me just reading it wrong.

    As for your output i'm not entirley sure... sure.

    Ill have a try of making this my self, looks fun =]
     
  3. OnceUponATimeInMexico

    OnceUponATimeInMexico Active Member

    Joined:
    Sep 11, 2007
    Posts:
    158
    Referrals:
    0
    Sythe Gold:
    0
    need help with c++ program

    I know you want to do this your self, as most people do. But i found this if you want to try see where you have gone wrong. Or look for a few hints.

    // Determine real and complex roots of a quadratic equation and check results
    // ax^2 + bx + c = 0

    #include <iostream>
    #include <math.h>
    #include <complex>
    using namespace std;

    int main(void)
    {
    float a, b, c, x1, x2, root;
    complex<float> z;

    cout << "Roots of quadratic equation, Enter a, b and c ? ";
    cin >> a >> b >> c;

    root = b * b - 4.0f * a * c; // b*b - 4ac
    if (root >= 0.0f) // if +
    {
    root = sqrt(root); // real roots
    x1 = (-b + root) / (2.0f * a);
    x2 = (-b - root) / (2.0f * a);
    cout << "Real roots " << x1 << " and " << x2 << endl;
    cout << "Test " << a * x1 * x1 + b * x1 + c // check result
    << " and " << a * x2 * x2 + b * x2 + c << endl;
    }
    else
    { // complex roots
    root = sqrt(-root) / (2.0f * a);
    x1 = -b / (2.0f * a);
    cout << "Complex roots " << x1 << " +- i " << root << endl;
    z = complex<float>(x1, root);
    cout << "Test " << a * z * z + b * z + c; // check result
    z = complex<float>(x1, -root);
    cout << " and " << a * z * z + b * z + c << endl;
    }
    system("pause");
    return 0;
    }
     
  4. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    need help with c++ program

    yes because someone just learning simple algebra definitely understands how complex and imaginary numbers work.
     
< Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick. | [RuneScape] Howto Make an AutoTalker [RuneScape] >


 
 
Adblock breaks this site