The subject requires the implementation of a function to calculate the order ofn
, the coefficient isa[0]
...a[n]
The polynomialF (x) =∑ni=0 (A[I]*XI) f (x) x
The value at the point.
function Interface Definition:
double f( int n, double a[], double x );
Which n
is the order of the polynomial, the a[]
storage factor, x
is the given point. The function must return f(x)
the value of the polynomial.
Example of a referee test procedure:
#include <stdio.h>#define MAXN 10double f( int n, double a[], double x );int main(){ int n, i; double a[MAXN], x; scanf("%d %lf", &n, &x); for ( i=0; i<=n; i++ ) scanf(“%lf”, &a[i]); printf("%.1f\n", f(n, a, x)); return 0;}/* 你的代码将被嵌在这里 */
Input Sample:
2 1.11 2.5 -38.7
Sample output:
-43.1
程序代码:*
Double f (int n, double a[], double x)
{
Double sum;
if (n>=0)
Sum=a[n]*pow (x,n) +f (n-1,a,x);
return sum;
}
4-2 Polynomial evaluation