/*************************************************** * Test program for Horner's rule * * ------------------------------------------------ * * Reference: BASIC Scientific Subroutines, Vol. II * * By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1].* * * * C++ Version By J.-P. Moreau, Paris. * * (www.jpmoreau.fr) * * ------------------------------------------------ * * SAMPLE RUN: * * * * Input the five coefficients: * * * * A( 0) = 1 * * A( 1) = 4 * * A( 2) = 6 * * A( 3) = 4 * * A( 4) = 1 * * * * What is the expansion point: 1 * * * * The shifted coefficients are: * * * * B( 0) = 16 * * B( 1) = 32 * * B( 2) = 24 * * B( 3) = 8 * * B( 4) = 1 * * * ***************************************************/ #include #include double C[5][6]; double A[6], B[6]; double x0; int i; /************************************************** * Horner's shifting rule subroutine * * ----------------------------------------------- * * This routine takes a given quartic polynomial * * and converts it to a Taylor expansion. * * The input series coefficients are A(i), the * * expansion point is x0. The shifted coefficients * * are returned in B(i). * **************************************************/ void Horner_Shift() { int i, j; for (j=0; j < 5; j++) C[j][0] = A[4-j]; i=0; while (i<5) { C[0][i+1] = C[0][i]; j = 1; while (j <= 4-i) { C[j][i+1] = x0*C[j-1][i+1]+C[j][i]; j++; } i++; } for (i=0; i < 5; i++) B[4-i] = C[i][4-i+1]; } void main() { // Init(); printf("\n Input the five coefficients:\n\n"); for (i=0; i < 5; i++) { printf(" A(%d) = ",i); scanf("%lf",&A[i]); } printf("\n What is the expansion point: "); scanf("%lf",&x0); Horner_Shift(); printf("\n\n The shifted coefficients are:\n\n"); for (i=0; i < 5; i++) printf(" B(%d) = %6.0f\n",i,B[i]); printf("\n"); } //End of file horner.cpp