![]() |
Data Abstraction and Problem Solving with C++Walls and Mirrorsby Frank M. Carrano |
![]() |
c01p032.cppGo to the documentation of this file.00001 00035 // Computes n! for an integer n >= 0 00036 int f = 1; 00037 00038 // Invariant: f == (j - 1)! 00039 for (int j = 1; j <= n; ++j) 00040 f *= j; 00041 00042 // Computes an approximation to e^x for a real x 00043 double t = 1.0; 00044 double s = 1.0; 00045 int k = 1; 00046 00047 // Invariant: t == x^(k - 1) / (k - 1)! and 00048 // s == 1 + x + x^2 / 2! + ... + x^(k - 1) / (k - 1)! 00049 while (k <= n) 00050 { t *= x / k; 00051 s += t; 00052 ++k; 00053 } // end while |