By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,483 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
5 Replies


gpraghuram
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 <iostream>
#include <cmath>
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<b): \n”;
cin>>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)<<endl;
}
}
}
Return;
}

New Function
Double poly(double x, double a0, double a1, double a2, double a3)
{
Return a0*x*x*x+a1*x*x+a2*x+a3;
}
Nov 4 '08 #3

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
  1. #include "head.h"
  2. int main()
  3. {//start main
  4.     double a = 0, b = 0, c = 0, z = 0, stepsize = 0, leftbound = 0, rightbound = 0, secondroot = 0, root = 0;
  5.     int degree = -1;
  6.     cout << "Enter the desired degree(2 or 3) or type (-1) to quit: ";
  7.     cin >> degree;
  8.  
  9.     while(degree != -1)
  10.     {
  11.         while((degree != 2) && (degree != 3))
  12.         {
  13.             cout << "\nThis is not an option." << endl;
  14.             cout << "Enter the desired degree (2 or 3): ";
  15.             cin >> degree;
  16.         }
  17.             cout << "\nEnter a, b, c, and z: ";
  18.             cin >> a;
  19.             cin >> b;
  20.             cin >> c;
  21.             cin >> z;
  22.  
  23.             if(degree == 3)
  24.             {
  25.  
  26.                 cout << "\nPlease enter the step size, right bound, and left bound: ";
  27.                 cin >> stepsize;
  28.                 cin >> rightbound;
  29.                 cin >> leftbound;
  30.             }
  31.  
  32.             root = roots(degree, leftbound, rightbound, stepsize, z, a, b, c, secondroot);
  33.  
  34.             if (degree == 2)
  35.             {//start if
  36.                 if (root != -9999)
  37.                 {//start inner if
  38.                     cout << "Root 1 is: " << root << endl;
  39.                     cout << "Root 2 is: " << secondroot << endl;
  40.                 }//end inner if
  41.                 else
  42.                 {//start else
  43.                     cout << "Root 1 is an imaginary and can not be calculated" << endl;
  44.                     cout << "Root 2 is an imaginary and can not be calculated" << endl;
  45.                 }//end else
  46.             }// end if
  47.  
  48.             cout << "\nEnter the desired degree (2 or 3) or type (-1) to quit: ";
  49.             cin >> degree;
  50.  
  51.     }//end while
  52. }// end main
  53.  
  54.  
  55. Poly.cpp FUNCTION
  56. double poly(double x, double z, double a, double b, double c) 
  57.  
  58. {//start poly 
  59.  
  60. double y; 
  61.  
  62. y=z*pow(x,3)+a*pow(x,2)+b*x+c; 
  63.  
  64. return y; 
  65.  
  66. }//end poly
  67.  
  68.  
  69.  
  70.  
  71. Roots.cpp FUNCTION
  72. double roots(int degree, double leftbound, double rightbound, double stepsize, double z, double a, double b, double c, double &secondRoot)
  73. {//start root
  74.     double i = leftbound, test1 = 0, test2 = 0, root = 0, descrim = 0, estimateOfRoot = 0, rootnum = 0;
  75.  
  76.     switch(degree) 
  77.     {//start switch
  78.         case 2:
  79.             // aX^2 + bX + c
  80.             descrim = pow(-b,2) - 4 * a* c;
  81.             cout << "The Descriminant is: " << descrim << endl;
  82.             if(descrim < 0)
  83.             {//start if
  84.  
  85.                 estimateOfRoot = -9999;
  86.                 secondRoot = -9999;
  87.             }// end if
  88.             else
  89.             {//start else
  90.  
  91.                 estimateOfRoot = ((-b + sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
  92.                 secondRoot = ((-b - sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
  93.             }//end else
  94.  
  95.             return estimateOfRoot;    
  96.  
  97.             break;
  98.  
  99.         case 3:
  100.  
  101.             descrim = pow(-b,2) - 4 * a* c;
  102.             cout << "\nThe Descriminant is: " << descrim << endl;
  103.             while(i < rightbound)
  104.             {// start while
  105.                 test1 = poly(i, z, a, b, c);
  106.                 test2 = poly(i + stepsize, z, a, b, c);
  107.  
  108.                 if(abs(test1) < 0.0001)
  109.                 {//start if
  110.  
  111.                     root = i;
  112.                     cout << "There is a root at: " << root << endl;
  113.                     rootnum++;
  114.                 }// end if
  115.                 else if((test1 * test2) < 0)
  116.                 {// start else
  117.  
  118.                     root = ((i + stepsize) + i) / 2;
  119.                     cout << "There is a root at: " << root << endl;
  120.                     rootnum++;
  121.                 }//end else
  122.  
  123.                 i += stepsize;
  124.             }// end while
  125.     }
  126. }
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

Post your reply

Sign in to post your reply or Sign up for a free account.