C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-06-2006, 06:03 PM   #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.
Sir Andus is offline   Reply With Quote
Old 03-06-2006, 06:54 PM   #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?
ChaosEngine is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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