This coder needs HELP!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharptool
    New Member
    • Mar 2008
    • 2

    #1

    This coder needs HELP!!

    Please, please, please make suggestions on how to resolve these errors.
    I am trying to build a program that computes a, b, c for the quadratic equation but I'm not having any luck. It's been 5 hours and I've only manage to clear 4 of my errors.
    Please note the primary focus is to use function calls. The main function needs to pass the coefficients to a function root1 that computes and returns the value of x1 to the main. I am trying to do the same with x2. The return type should be a floating point.
    Any help is greatly appreciated! thx.

    ****
    [code=cpp]
    #include <iostream>
    #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    using namespace std;

    int main ()

    {
    int a, b, c, x1, x2;

    cout << "Please enter coefficients a, b, and c of the Quadratic Equation, ax^2 + bx + c = 0: " << "\n";
    cout << "Please enter a value for coefficient a, where coefficients a and b are not equal to 0: " << "\n";
    cin >> a >> b >> c;
    }

    int Calculate_First _Root (int a, int b, int c, float x1)

    {
    x1 = (- b + sqrt(pow(b, 2) + 4 * a * c) ) / (2 * a);
    cout << "The real root value =" << x1 << "\n";
    }

    int Calculate_Secon d_Root (int a, int b, int c, float x2)

    {
    x2 = (- b - sqrt(pow(b, 2) + 4 * a * c) ) / (2 * a);
    cout << "The real root value =" << x2 << "\n";
    getch();
    }[/code]

    ****Errors:
    1>------ Build started: Project: Quad, Configuration: Debug Win32 ------
    1>Compiling...
    1>Quad.cpp
    1>c:\ : warning C4101: 'x2' : unreferenced local variable
    1>c:\ : warning C4101: 'x1' : unreferenced local variable
    1>c:\ : error C2668: 'pow' : ambiguous call to overloaded function
    1> c:\program files\microsoft visual studio 9.0\vc\include\ math.h(575): could be 'long double pow(long double,int)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ math.h(527): or 'float pow(float,int)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ math.h(489): or 'double pow(double,int) '
    1> while trying to match the argument list '(int, int)'
    1>c:\ : error C2668: 'pow' : ambiguous call to overloaded function
    1> c:\program files\microsoft visual studio 9.0\vc\include\ math.h(575): could be 'long double pow(long double,int)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ math.h(527): or 'float pow(float,int)'
    1> c:\program files\microsoft visual studio 9.0\vc\include\ math.h(489): or 'double pow(double,int) '
    1> while trying to match the argument list '(int, int)'
    1>Quad - 2 error(s), 2 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by sicarie; Mar 3 '08, 02:59 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You are not calling any function from your main function and thus you're not
    using variables x1 and x2; that's what your compiler is complaining about. It's
    a bit of overkill to call a pow() function for calculating the square of b, better
    calculate it as b*b which gets rid of most of the errors.

    kind regards,

    Jos

    Comment

    • sharptool
      New Member
      • Mar 2008
      • 2

      #3
      Originally posted by JosAH
      You are not calling any function from your main function and thus you're not
      using variables x1 and x2; that's what your compiler is complaining about. It's
      a bit of overkill to call a pow() function for calculating the square of b, better
      calculate it as b*b which gets rid of most of the errors.

      kind regards,

      Jos

      Thanks for the quick reply.

      I am not sure how to call a function from a main function. Any help?
      Also, I have re-worked some of the coding.
      However, I can't get the program to calculate values for the inputs. After the values are entered, I press the 'ENTER' key and the program remains still with the input values in place. 'ENTER' again will cause the program to close???

      Any suggestions?


      Thanks.
      [code=cpp]
      #include <iostream>
      #include <conio.h>
      #include <math.h>
      using namespace std;

      int main (float a, float b, float c, float x1, float x2, float dis)

      {
      cout << "Enter coefficients a, b, and c of the Quadratic Equation, ax^2 + bx + c = 0 placing a whitespace between each value:" << "\n";
      cout << "\n";
      cin >> a >> b >> c;
      getch();
      }

      int Calculate_First _Root (float a, float b, float c, float& x1)

      {
      float dis = (b * b) - (4 * a * c);
      x1 = (- b + sqrt((dis)) / (2 * a));
      return 1;
      cout << "Coefficien ts = " << a << ", " << b << ", " << c;
      cout << "; Real root values = " << x1 << "\n";
      }

      int Calculate_Secon d_Root (float a, float b, float c, float& x2)

      {
      float dis = (b * b) - (4 * a * c);
      x2 = (- b - sqrt((dis)) / (2 * a));
      return 1;
      cout << "Coefficien ts = " << a << ", " << b << ", " << c;
      cout << "; Real root values =" << x2 << "\n";
      }[/code]
      Last edited by sicarie; Mar 3 '08, 03:00 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

      Comment

      • fual
        New Member
        • Feb 2008
        • 28

        #4
        You don't need two separate functions for the two roots, but you do need to check to see if b^2 - 4ac < 0 since then your function has complex roots and you need to deal with this case separately, something like:

        Code:
        #include <complex>
        #include <cmath>
        
        typedef std::complex<double> complex_type;
        
        complex_type discriminant( const double& a, const double& b, const double& c ) {
          double d(b*b - 4*a*c,0);
          if( d < 0 ) return complex_type(0,sqrt(-d));
          else return complex_type(sqrt(d),0);
        }
        Have a play with that and find out about the C++ complex type. http://msdn2.microsoft.com/en-us/lib...hd(VS.80).aspx

        Comment

        • fual
          New Member
          • Feb 2008
          • 28

          #5
          Supposed to read
          Code:
          #include <complex>
          #include <cmath>
          
          typedef std::complex<double> complex_type;
          
          complex_type discriminant( const double& a, const double& b, const double& c ) {
            double d(b*b - 4*a*c);
            if( d < 0 ) return complex_type(0,sqrt(-d));
            else return complex_type(sqrt(d),0);
          }

          Comment

          Working...