1. ## polynomials

I'm new to C programming and I have an assignment that's really been bugging me. I have to write a function
double eval(double p[], double x, int n)
{
}
that returns the value of polynomial p evaluated at x using Horner's Rule which is p(x) = a0 + x(a1 + (a2 + x(a3 + x(a4 + x(a5 )))))

Here's what I've written so far:

#include <stdio.h>
#include <math.h>
#define N 5
double eval(double p[], double x, int n);

int main() {

double polynomial; /* Used for return value from eval function */

double p[N + 1];

/* Print results of polynomial evaluation */
printf("Polynomial evaluated at x: %f\n\n", polynomial);

return 0;
}

/* Polynomial evaluation function */
double eval(double p[], double x, int n) {
double result;
int i;
if (n <= 0)
{
return 0.0;
}
result = p[0];

for (i=1; i <= n; i++)
{
result = result + p[i] * pow(x, i);
}

return result;
}

2. >>result = result + p[i] * pow(x, i);

Below i have added a picture taken from google for that guys rule. I would say, yes you are in the right direction, are there any problems?

--edit--
Question: What is x? What do you pass? and where is the function call in main?

3. ... but notice that you are not using Homer's Rule (D'oh!). You have correctly described it at the top of your post. Think about a for-loop that evaluates each of the brackets in turn. The resulting code will be more efficient than repeatedly using pow(a,b).

actually you almost got the algorithm right (probably just a typo) it should be:

a0 + a1 * x + a2 * x^2 + a3 * x^3 + a4 * x^4 + a5 * x^5 =
a0 + x * (a1 + x * (a2 + x * (a3 + x * (a4 + x * (a5)))))

Good luck,