C++ polynomial Show your complete work and write comments! To get full points. W
ID: 3769054 • Letter: C
Question
C++ polynomial
Show your complete work and write comments! To get full points. Write a program to print all the derivative(s) of a polynomial expression (shown below) at point x = A An example output is shown above(INPUT FROM USER IN RED) The value of x and coefficients can be floating point values. You are free to write the code by choosing any of the programming techniques discussed in the class. No restrictions. The example output shown above, is just that, an example! The program should w ork for any polynomial degree(you can assume a maximum polynomial degree of 50) Notice that the coefficient for expression x^1 in the example is 0 and hence is not printed in the f(x) expression. Also, the negative coefficient for x^4 in the example is printed as "-3.0" in the f(x) expression. You have to ensure that your code does this exactly. Similar printing notation for the derivatives MUST be taken care of by your code (see the example output). As this is an extra credit project, the TAs or 1 will not help you with the logic of this program. You need to come up with your own. The only help that you will get is with respect to debugging the code and during the office hours only. If you submit an incomplete/non-working program you will only get 25% of the total points. Full credit ONLY for correctly working programs that work for a general case, have comments and use legible variable names.Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
float i,d;
int coeff[100],degree[100];
clrscr();
cout<<" Enter the degree of the polynomial: ";
cin>>d;
for(i=d;i>=0;i--)
{ if(i>1)
{
degree[i]=i;
cout<<" Enter the coefficient of x^"<<degree[i]<<": ";
cin>>coeff[i];
}
else if(i==1)
{
degree[i]=i;
cout<<" Enter the coefficient of x:";
cin>>coeff[i];
}
else
{
degree[0]=0;
cout<<" Enter the constant term:";
cin>>coeff[0];
}
}
clrscr();
cout<<" Entered polynomial function, f (x)=";
for(i=d;i>=0;i--)
{
if(coeff[i]>0&°ree[i]!=1)
{
if(degree[i])
cout<<"+"<<coeff[i]<<"x^"<<degree[i];
else
cout<<"+"<<coeff[0];
}
else if(coeff[i]<0&°ree[i]!=1)
{
if(degree[i])
cout<<coeff[i]<<"x^"<<degree[i];
else
cout<<coeff[0];
}
else if(coeff[i]!=0&°ree[i]==1)
{
if(coeff[i]>0)
cout<<"+"<<coeff[i]<<"x";
else
cout<<coeff[i]<<"x";
}
}
getch();
for(i=d;i>=0;i--)
{
coeff[i]*=degree[i];
degree[i]-=1;
}
cout<<" First derivative of function,f'(x)=";
for(i=d;i>=0;i--)
{
if(coeff[i]>0&°ree[i]!=1)
{
if(degree[i])
cout<<"+"<<coeff[i]<<"x^"<<degree[i];
else
cout<<"+"<<coeff[i];
}
else if(coeff[i]<0&°ree[i]!=1)
{
if(degree[i])
cout<<coeff[i]<<"x^"<<degree[i];
else
cout<<coeff[i];
}
else if(coeff[i]!=0&°ree[i]==1)
{
if(coeff[i]>0)
cout<<"+"<<coeff[i]<<"x";
else
cout<<coeff[i]<<"x";
}
}
getch();
for(i=d;i>=0;i--)
{
coeff[i]*=degree[i];
degree[i]-=1;
}
cout<<" Second derivative of function , f"(x)=";
for(i=d;i>=0;i--)
{
if(coeff[i]>0&°ree[i]!=1)
{
if(degree[i])
cout<<"+"<<coeff[i]<<"x^"<<degree[i];
else
cout<<"+"<<coeff[i];
}
else if(coeff[i]<0&°ree[i]!=1)
{
if(degree[i])
cout<<coeff[i]<<"x^"<<degree[i];
else
cout<<coeff[i];
}
else if(coeff[i]!=0&°ree[i]==1)
{
if(coeff[i]>0)
cout<<"+"<<coeff[i]<<"x";
else
cout<<coeff[i]<<"x";
}
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.