FUNCTION POLY(N,A,X) ! implicitly real (float) function poly(int n,int a,real x)
DIMENSION A(N) ! shape A as 1d array of n values in this scope
C ! say nothing (blank comment)
POLY = 0. ! initialise return variable to float value 0
L = N ! set L (implicitly integer) to n
DO 1 K = 1,N ! for(int k=1; k<=n; ++k)
POLY = POLY*X + A(L) ! update return variable
1 L = L-1 ! decrement L
RETURN ! return current value for poly
END
therefore, in c-like syntax:
float poly(int n, int a, float x) {
float result = 0;
int l = n;
for(int k=1; k <= n; ++k) {
result = result*x + a(l);
--l;
}
return result;
}
A bit that does not translate is to redistribute A as an array. In C, you must pass a pointer and use it as an array, and in C ++ / C # you will probably pass a vector structure with a property of its own length.
In C #, using a list:
float poly(List<float> coeffs, float x) {
float result = 0;
for(int i=coeffs.Count-1; i >= 0; --i) {
result = result*x + coeff[i];
}
return result;
}