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;
}
Am I heading in the right direction? Comments???