Roots Function. Can't make it work......

The descriminant value shows up but the roots do not output. Any thoughts?

Heres the problem specifications.

Function poly(given x, z, a, b, c; returns y)

Function roots(given degree, left, right, z, a, b, c, &secondRoot; returns estimateOfRoot)

Note: The roots function has a switch structure to process either a polynomial of degree 2 or 3 with case 2: to handle quadratic formula that returns two roots, one through the normal return statement via estimateOfRoot and a second root through the reference parameter &secondRoot; and with case 3: to handle the cubic polynomial with the incremental search technique.

Your main module should drive the roots function with PIE from the user for degree, coefficients, interval limits, and step size until the user decides to quit the program with a value of -1 for degree, that is, an input sentinel of -1 to terminate a loop in the main module that drives your Polynomial Roots Problem.


Main FUNCTION

#include "head.h"
int main()
{//start main
double a = 0, b = 0, c = 0, z = 0, stepsize = 0, leftbound = 0, rightbound = 0, secondroot = 0, root = 0;
int degree = -1;
cout << "Enter the desired degree(2 or 3) or type (-1) to quit: ";
cin >> degree;

while(degree != -1)
{
while((degree != 2) && (degree != 3))
{
cout << "\nThis is not an option." << endl;
cout << "Enter the desired degree (2 or 3): ";
cin >> degree;
}
cout << "\nEnter a, b, c, and z: ";
cin >> a;
cin >> b;
cin >> c;
cin >> z;

if(degree == 3)
{

cout << "\nPlease enter the step size, right bound, and left bound: ";
cin >> stepsize;
cin >> rightbound;
cin >> leftbound;
}

root = roots(degree, leftbound, rightbound, stepsize, z, a, b, c, secondroot);

if (degree == 2)
{//start if
if (root != -9999)
{//start inner if
cout << "Root 1 is: " << root << endl;
cout << "Root 2 is: " << secondroot << endl;
}//end inner if
else
{//start else
cout << "Root 1 is an imaginary and can not be calculated" << endl;
cout << "Root 2 is an imaginary and can not be calculated" << endl;
}//end else
}// end if

cout << "\nEnter the desired degree (2 or 3) or type (-1) to quit: ";
cin >> degree;

}//end while
}// end main


Poly.cpp FUNCTION
double poly(double x, double z, double a, double b, double c)

{//start poly

double y;

y=z*pow(x,3)+a*pow(x,2)+b*x+c;

return y;

}//end poly




Roots.cpp FUNCTION
double roots(int degree, double leftbound, double rightbound, double stepsize, double z, double a, double b, double c, double &secondRoot)
{//start root
double i = leftbound, test1 = 0, test2 = 0, root = 0, descrim = 0, estimateOfRoot = 0, rootnum = 0;

switch(degree)
{//start switch
case 2:
// aX^2 + bX + c
descrim = pow(-b,2) - 4 * a* c;
cout << "The Descriminant is: " << descrim << endl;
if(descrim < 0)
{//start if

estimateOfRoot = -9999;
secondRoot = -9999;
}// end if
else
{//start else

estimateOfRoot = ((-b + sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
secondRoot = ((-b - sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
}//end else

return estimateOfRoot;

break;

case 3:

descrim = pow(-b,2) - 4 * a* c;
cout << "\nThe Descriminant is: " << descrim << endl;
while(i < rightbound)
{// start while
test1 = poly(i, z, a, b, c);
test2 = poly(i + stepsize, z, a, b, c);

if(abs(test1) < 0.0001)
{//start if

root = i;
cout << "There is a root at: " << root << endl;
rootnum++;
}// end if
else if((test1 * test2) < 0)
{// start else

root = ((i + stepsize) + i) / 2;
cout << "There is a root at: " << root << endl;
rootnum++;
}//end else

i += stepsize;
}// end while
}
}



Please use [code][/code] tags around your code so I can actually read the indentation.
Topic archived. No new replies allowed.