programming-examples/c++/Others/Fig10_45.cpp - Better algorithm to replace fig10_43.c (see text).cpp
2019-11-15 12:59:38 +01:00

29 lines
677 B
C++

Fig10_45.cpp - Better algorithm to replace fig10_43.c (see text)
#include <iostream.h>
#include "vector.h"
/* START: Fig10_45.txt */
double eval( int n )
{
vector<double> c( n + 1 );
c[ 0 ] = 1.0;
for( int i = 1; i <= n; i++ )
{
double sum = 0.0;
for( int j = 0; j < i; j++ )
sum += c[ j ];
c[ i ] = 2.0 * sum / i + i;
}
return c[ n ];
}
/* END */
int main( )
{
cout << "eval( 10 ) = " << eval( 10 ) << endl;
return 0;
}