quadratic root and imaginary result

This is a discussion on quadratic root and imaginary result within the C++ Programming forums, part of the General Programming Boards category; Hello, I have the following program, but the issue I'm having that whenever one of the roots is imaginary both ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    53

    quadratic root and imaginary result

    Hello,

    I have the following program, but the issue I'm having that whenever one of the roots is imaginary both solutions become zero. Any help with this will be appreciated.

    Code:
    #include <iostream>
    #include <iomanip>
    
    // this header file identifies all of the special mathematical functions.
    #include <cmath>
    using namespace std;
    
    // Version 1.2 Adds functions for Quadratic roots of complex value
    //    This is an incomplete version for students to work with
    // Version 1.1 Removed standard method calls and implemented operator overloading
    // Version 1.0 Initial complex value declaration
    
    struct ComplexStruc {
       double real, imaginary;
    } ;
    
    class myComplex {
       double real, imaginary;
    public:
       myComplex( double dReal = 0.0, double dImaginary = 0.0 );
    
       double getReal( void ) { return real; };
       double getImaginary( void ) { return imaginary; };
       void setReal( double dR ) { real = dR; };
       void setImaginary( double dI ) { imaginary = dI; };
    
       friend ostream& operator<<(ostream& osLeft, const myComplex& right);
       const myComplex operator +( const myComplex& right ) const;
       const myComplex operator -( const myComplex& right ) const;
       const myComplex operator *( const myComplex& right ) const;
    
    } ;
    
    double QuadraticRoot1( double dA, double dB, double dC )
    {
       double dRoot = 0;
       double dIntermediate1 = 0.0, dIntermediate2 = 0.0;
    
       if( (dIntermediate1 = 4.0 * dA * dC) < (dIntermediate2 = dB * dB) ) {
          dRoot = (-dB - pow(dIntermediate2 - dIntermediate1, 0.5)) / (2.0 * dA);
       }
       else {
          // value is complex
       }
       return dRoot;
    }
    
    double QuadraticRoot2( double dA, double dB, double dC )
    {
       double dRoot= 0;
       double dIntermediate1 = 0.0, dIntermediate2 = 0.0;
    
       if( (dIntermediate1 = 4.0 * dA * dC) < (dIntermediate2 = dB * dB) ) {
          dRoot = (-dB + pow(dIntermediate2 - dIntermediate1, 0.5)) / (2.0 * dA);
       }
       else {
          // value is complex
       }
       return dRoot;
    }
    
    int main()
    {
       double dA, dB, dC ;
    
       myComplex mcA(1.2, 1.3), mcB(2.1, 2.3), mcC(0.0, 0.0);
    
       cout << endl << endl << endl << "Using overloaded operators" << endl << endl;
    
       cout << "The original value of parameter C is zero." << endl;
       cout << "Only the real component value of zero shows, " << mcC << endl << endl;
    
       mcC = (mcA + mcB);
    
       cout << "Addition, " << mcC << endl << endl ;
       cout << "Multiplication and subtraction (A*B)-B, " << (mcA*mcB)-mcB << endl << endl;
    
       do {
          cout << "Quadratic solution, input coefficients A, B then C "
             << endl << "enter all zeros to stop " << endl ;
          cin >> dA >> dB >> dC ;
          if( dA == 0 && dB == 0 && dC == 0 )
             break;
          cout << "Root 1 = " << QuadraticRoot1( dA, dB, dC ) << endl << endl ;
          cout << "Root 2 = " << QuadraticRoot2( dA, dB, dC ) << endl << endl ;
       } while( true );
    
       system( "pause" );
       return 0;
    }
    
    myComplex::myComplex(double dReal, double dImaginary)
    {
       real = dReal;
       imaginary = dImaginary;
       return;
    }
    
    ostream& operator<<(ostream& osLeft, const myComplex& rhv)
    {
       osLeft << fixed << setprecision(3) << rhv.real ;
       if( rhv.imaginary < 0 )
          osLeft << " - " ;
       else if( rhv.imaginary > 0 )
          osLeft << " + " ;
       else
          return osLeft;
    
       osLeft << abs(rhv.imaginary) << " j" ;
    
       return osLeft; 
    }
    
    const myComplex myComplex::operator +( const myComplex& rhv )const
    {
       myComplex temp(0,0);
       temp.real = real + rhv.real;
       temp.imaginary = imaginary + rhv.imaginary;
       return temp;
    }
    
    const myComplex myComplex::operator -( const myComplex& rhv )const
    {
       myComplex temp(0,0);
       temp.real = real - rhv.real;
       temp.imaginary = imaginary - rhv.imaginary;
       return temp;
    }
    
    const myComplex myComplex::operator *( const myComplex& rhv )const
    {
       myComplex temp(0,0);
       temp.real = (real * rhv.real)  + (imaginary * rhv.imaginary);
       temp.imaginary = (real * rhv.imaginary)  + (imaginary * rhv.real);
       return temp;
    }

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    1,053
    This would probably be better suited for the C++ forum

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,801
    Agreed, and moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Code:
    double QuadraticRoot1( double dA, double dB, double dC )
    {
        double dRoot = 0;
    
    .
    .
       else {
          // value is complex
       }
       return dRoot;
    
    }
    
    
    double QuadraticRoot2( double dA, double dB, double dC )
    {
       double dRoot= 0;
       .
       . 
       else {
          // value is complex
       }
       return dRoot;
    }
    So,you have nothing in the else block therefore initial value of dRoot is returned.
    ie. 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extreme beginner quadratic roots solver
    By browser in forum C Programming
    Replies: 3
    Last Post: 11-08-2009, 10:33 AM

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