Unknown Math Issues.

This is a discussion on Unknown Math Issues. within the C++ Programming forums, part of the General Programming Boards category; As of lately, I've been just messing around with C++ just basically trying to grasp how to do various tasks ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    Unknown Math Issues.

    As of lately, I've been just messing around with C++ just basically trying to grasp how to do various tasks with it. Earlier this week, I decided to write a program that had to do with the quadratic formula. Basically, using if statements to determine whether the function had two, one, or no roots. I've gotten all the syntax errors out of it, but the math seems to be off a little. Whenever I run the program, I always end up getting "1.000" for each output. Any help would be superb.

    Code:
    // This program takes three coefficients of the function f(x) = ax^2 + bx + c and figures out the
    // value of the determinant, which in turn displays the roots of the function, if any exist.
    
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    double a, b, c, xplus, xminus, x, deter;
    void read ( double &a, double &b, double &c );
    double determinant ( double a, double b, double c );
    bool quadplus ( double a, double b, double deter );
    bool quadminus ( double a, double b, double deter );
    bool quadone ( double a, double b );
    bool report ( double a, double b, double c, double xplus, double xminus, double x );
    
    int main()
    {
      double a, b, c, xplus, xminus, x, deter;
      read ( a, b, c );
      deter = determinant ( a, b, c );
      xplus = quadplus ( a, b, deter );
      xminus = quadminus ( a, b, deter );
      x = quadone ( a, b );
      report ( a, b, c, xplus, xminus, x );
      
      system("Pause");
    }
    
    void read ( double &a, double &b, double &c )
    {
      // Read in the three coefficients:
      cout << "Enter the coefficients of a quadratic function f(x) = ax^2 + bx + c:" << endl;
      cout << "  a = ";
      cin >> a;
      cout << "  b = ";
      cin >> b;
      cout << "  c = ";
      cin >> c;
    }
      
    double determinant ( double a, double b, double c )
    {
      // Identify the value of the determinant:
      deter = ( pow(b,2.0) - ( 4.0 * a * c ) );
      return deter;
    }
    
    bool quadplus ( double a, double b, double deter )
    {
      // Find the addition value of the function if it has two roots:
      double xplus;
      if ( deter > 0 ) {
        xplus = (( -1.0 * b ) + sqrt( deter )) / ( 4.0 * a );
        return xplus;
      } else;
    }
    
    bool quadminus ( double a, double b, double deter )
    {
      // Find the subtraction value of the functon if it has two roots:
      double xminus;
      if ( deter > 0 ) {
        xminus = (( -1.0 * b ) - sqrt( deter )) / ( 4.0 * a );
        return xminus;
      } else;
    }
    
    bool quadone ( double a, double b )
    {
      // Find the value of the function if it has one root:
      if ( deter == 0 ) {
        x = ( -1.0 * b ) / ( 4 * a );
        return x;
      } else;
    }
      
    
    bool report ( double a, double b, double c, double xplus, double xminus, double x)
    {
      // Report:
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout << setprecision(3);
      if ( deter > 0 ) {
        cout << "The function f(x) = " << a << "*x^2 + " << b << "*x + " << c <<
        " has two real roots:" << endl << " x = " << xplus << endl << " x = " << xminus << endl;
      } else if ( deter == 0 ) {
        cout << "The function f(x) = " << a << "*x^2 + " << b << "*x + " << c <<
        " has one real root:"<< endl << " x = " << x << endl;
      } if ( deter < 0 ) {
        cout << "The function f(x) = " << a << "*x^2 + " << b << "*x + " << c <<
        " has no real roots." << endl;
      }
    }
    Besides the mathematical errors, any methods as to how make the program simpler would also appreciated. Thanks in advance.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    code reduced for clarity

    Code:
    using namespace std;
    
    double a, b, c, xplus, xminus, x, deter;
    why are you declaring these as global and then again as local to main?
    Code:
    int main()
    {
      double a, b, c, xplus, xminus, x, deter;
    declare variables just before you need them
    Code:
      double deter = determinant ( a, b, c );
    don't bother with an unneccessary temporary
    Code:
      
    double determinant ( double a, double b, double c )
    {
      // Identify the value of the determinant:
      return ( pow(b,2.0) - ( 4.0 * a * c ) );
    }
    this should cause an error (or at least a warning)
    you returning xplus (a double) as a bool. this will change any value of xplus (other then 0.0) to 1!!
    Code:
    bool quadplus ( double a, double b, double deter )
    {
      // Find the addition value of the function if it has two roots:
      double xplus;
      if ( deter > 0 ) {
        xplus = (( -1.0 * b ) + sqrt( deter )) / ( 4.0 * a );
        return xplus;
      } else; // ARRRGGHHH!!! not all control paths return a value!
    }
    
    // try it like this
    bool quadplus ( double a, double b, double deter, double &xplus)
    {
      bool bSuccess = false;
      // Find the addition value of the function if it has two roots:
      double xplus;
      if ( deter > 0 ) {
        xplus = (( -1.0 * b ) + sqrt( deter )) / ( 4.0 * a );
        bSuccess = true;
      } 
      return bSuccess ;
    }
    see last comment
    Code:
    bool quadminus ( double a, double b, double deter )
    {
      // Find the subtraction value of the functon if it has two roots:
      double xminus;
      if ( deter > 0 ) {
        xminus = (( -1.0 * b ) - sqrt( deter )) / ( 4.0 * a );
        return xminus;
      } else;
    }
    
    bool quadone ( double a, double b )
    {
      // Find the value of the function if it has one root:
      if ( deter == 0 ) {
        x = ( -1.0 * b ) / ( 4 * a );
        return x;
      } else;
    }
    Code:
    bool report ( double a, double b, double c, double xplus, double xminus, double x)
    
    // doesn't return anything!!!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 08:22 PM
  3. Unknown Math Function
    By verd in forum C Programming
    Replies: 2
    Last Post: 02-06-2005, 03:31 PM
  4. Math Issues
    By Berserker in forum C Programming
    Replies: 9
    Last Post: 01-11-2005, 01:54 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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