1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
Write a subfucntion called "diff_poly" to differentiate a polynomial.
//Assume for simplicity, he polynomial (with integers conefficients)
//is of degree no more than 5, you could use
//"bool diff_poly(intt& a, int& b, int& c, int& d, int& e, int& f)" as the prototype.
//Ask the user for the coefficients (e.g. 0 5 -2 0 0 6 for
//5x^4 -2x^3 + 6) in the main function. Then pass them to "diff_poly"
//via pass-by-reference on the coefficents.
//The subfunction differentiate the polynomial and return true if the result is
//a none-zero polynomial and pass the coefficents of the result out to the main.
//Then main takes the coefficients and print the result on screen. It will also
//indicate if the derivative is zero function or not based on the return value from
//diff_poly
#include <iostream>
using namespace std;
bool diff_poly (int& a, int& b, int& c, int& d, int& e, int& f){
a=a*5;
b=b*4;
c=c*3;
d=d*2;
e=e*1;
f=f*0;
if(a+b+c+d+e+f>0)
{
return true;
}
return false;
}
int main()
{
bool diff;
int a,b,c,d,e,f;
char again;
do
{ // run program untill user wants to stop
cout<<"\nEnter six integers positive or negative:";
cin>>a,b,c,d,e,f;
diff=diff_poly(a,b,c,d,e,f);
if(diff=true){
cout<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<endl;
}
cout<<"\nDo you want to run progaram again? (y/n):"<<endl;
cin>>again;
}
while((again=='y') || (again=='Y'));
cout<<"\nBye\n";
return 0;
}
|