Help with derivative code

I have to find the derivative of a polynomial with degree 5 with any given coefficient but it's been giving me nonsense answers
This is my rationale
The polynomial format is ax^5 + bx^4 + cx^3 + dx^2 +ex + f
where a,b,c,d,e,f are coefficients
So deriv should be 5ax^4 + 4bx^3 + 3cx^2 + 2dx + 1ex^0 + 0f
What am I doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
double Deriv (double array[], int arraySize, double xVal)
{
    double answer = 0; //clears out garbage values
    for (int i = 0; i < arraySize; i++) //i is the degree in descending order
    {
        if (i==0) //for the case of i=0 (a constant number)
        {
            answer = 0;
        }
        answer = answer + i*array[i]*pow(xVal,i-1); //i-1 to show that taking deriv decreases the degree exponent by 1
    }
    return answer;
}
Last edited on
> it's been giving me nonsense answers
Give an example input, with its expected output and what is giving you instead.
Also, show how you are using your function.
Topic archived. No new replies allowed.