454,497 Members | 2,483 Online
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

# I need help: Time is limited. Due tomorrow!

 P: 5 This is probably the worst prof I have ever had so I have no idea what to do to make this program. Help would be greatly appreciated. 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. Nov 4 '08 #1
5 Replies

 Expert 100+ P: 1,275 We cant write the code for you. Tell us what problem u are facing with this. Raghu Nov 4 '08 #2

 P: 5 Here, I wrote psuedocode for it. Does this seem correct? Main: read coefficients, interval endpoints a and b, and step size compute the number of subintervals, n set k to 0 while k<= n-1 compute left subinterval endpoint compute right subinterval endpoint check_roots (left, right, coefficients) increment k by 1 check_roots (b, b, coefficients) check_roots (left, right, coefficients); set f_left to poly(left, coefficients) set f_right to poly(right, coefficients) if f_left is near zero print root at left endpoint else if f_left * f_right < 0 print root at midpoint of subinterval return poly(x,a0,a1,a2,a3); return a0x^3 + a1x^2 + a2x + a3 New Function #include #include Using namespace std; int main() { int n; double a0, a1, a2, a3, a, b, step, left, right; cout<<”Enter coefficients a0, a1, a2, a3: \n”; cin>>a0>>a1>>a2>>a3; cout<<”Enter interval limits a, b (a>a >>b; cout<<”Enter step size: \n”; cin>>step; n= ceil((b-a)/step); for (int k=0; k<=n-1; k++) { Left = a + k*step; If (k == n-1) { Right=b; } } Else { right=left+step; } Check_roots(left,right,a0,a1,a2,a3); } Check_roots(b,b,a0,a1,a2,a3); Return 0; } New Function Void check_roots(double left, double right, doubl a0, double a1, double a2, double a3) { Double f_left, f_right; F_left = poly(left,a0,a1,a2,a3); F_right=poly(right,a0,a1,a2,a3); If (fabs(f_left) < 0.1e-04) { Cout<< “root detected at “ << left << endl; } If (fabs(f_right)<0.1e-04) ; Else { If (f_left*f_right <0) { Cout<<”root detected at “<< (left+right)/2)<

 10K+ P: 13,264 Where is the switch structure that was mentioned in your specs? Better write the code (according to the specs) and post only if you get specific problems with it. Just dumping some pseudo code on us and asking us if it's correct is a no no. Nov 4 '08 #4

 P: 5 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. Expand|Select|Wrap|Line Numbers #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     } } Nov 4 '08 #5

 100+ P: 424 The descriminant value shows up but the roots do not output. Any thoughts? Yes. Debug your code. Why don't you cout << root straight after root = roots(degree, leftbound, rightbound, stepsize, z, a, b, c, secondroot); to see what comes out? If the roots dont show then what happens instead? Does your program crash? Did you print out intermediate variables in your function like a,b,c to see they are what you expected? By the way, if degree==3 then the way you've written it you will never see your roots. Please Use CODE tags around your code. Nov 4 '08 #6