Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help with C++ please. I\'m supposed to write a program that calculat

ID: 3570284 • Letter: I

Question

I need some help with C++ please. I'm supposed to write a program that calculates the derivative of a function at a given point. This is to be done with the most basic of differentiation rules, which is the power rule. If you're unfamiliar, the power rule says that you take a function, for example f(x)=6x^3, and multiply the exponent by the leading coefficient, then lower the exponent by 1. So for the example, the derivative f'(x)=18x^2. This then needs to be evaluated at any user entered point for x.

Explanation / Answer

/*
    x: value of x in the polynomial
    c: array of coefficients
    n: number of coefficients
    */
        double derivePolynomial(double x, double c[], int n) {
                double result = 0;
                double p = 1;
                int counter = 1;

                for(int i=n-1; i>=0; i--) //backward loop
                    {
                    result = result + c[i]*p*counter;
                    counter++; // number of power
                    p = p*x;
                }

                return result;
            }



   //Output in main() looks like this

   double x=1.5;
   double coeffs[4]={3,2.2,-1,0.5};
   int numCoeffs=4;

   cout << " = " << derivePolynomial(x,coeffs,numCoeffs) << endl;