1. ## 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. 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!!!``` Popular pages Recent additions 