Here is the code, translated into C++, and written as a subprogram
with a simple main program for reading the polynomial and testing it:

Here are the new features of the code:

a
is an array of double precision values of length
MAX+1
. It reserves space in memory for the array and
defines a
to be a pointer to the first element a[0]
. Notice that we have chosen MAX
to be the
highest allowed degree of the polynomial. Since a polynomial of
degree takes coefficients, we need MAX+1
spaces.

a[0]
up to a[n]
.

a
is a
pointer to a double. It isn't necessary to specify the number of
elements, because space for the array is not allocated by the
subprogram, but by the calling program (main program in this case).
This syntax is equivalent to
void poly(int n, double *a, const double x0, double &p){ ... }but the square bracket notation helps remind us that the pointer is being used to pass an array of numbers.
Doubly subscripted arrays (matrices) can be declared and passed this way:
void func(double b[10][5]){ ... } int main(){ double b[10][5]; ... func(b); }
A coding weakness of the main program in the polynomial example above
is that it can handle only a fixed maximum degree for the polynomial,
since we allocate a fixed amount of storage for a
. To
evaluate a polynomial of degree 51 requires recompiling with a larger
MAX
. A better way to do this is to make the declaration
depend on the input value n
. That declaration can be
made only after n
is known. Then the only limitation is
the maximum available computer memory.
Here is how:

If the operating system has enough space in memory, the memory allocation succeeds.