![]() |
| | #1 |
| Registered User Join Date: Mar 2006
Posts: 17
| Unknown Math Issues. 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;
}
}
|
| Sir Andus is offline | |
| | #2 |
| semi-colon generator 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; Code: int main()
{
double a, b, c, xplus, xminus, x, deter;
Code: double deter = determinant ( a, b, c ); Code:
double determinant ( double a, double b, double c )
{
// Identify the value of the determinant:
return ( pow(b,2.0) - ( 4.0 * a * c ) );
}
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 ;
}
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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |