The C++ Program: tpk.cc

  1 /* tpk.cc -- Knuth's TPK program in the programming language C++ */
  2 
  3 #include <iostream.h>
  4 #include <math.h>  // smart header file (C and C++); "-lm" required
  5 
  6 /* f(x) = sqrt(|x|) + 5*x**3 */
  7 double f (double x) {
  8     return (sqrt(fabs(x)) + 5.0*pow(x,3.0));
  9 }
 10 
 11 int main (int argc, char** argv) {
 12     double A [11];
 13 
 14     /* Read in the values of the array "A" */
 15     for (int i=0; i<11; i++) {
 16         cin >> A[i];
 17     }
 18 
 19     /* In reverse order, apply "f" to each element of "A" and print */
 20     for (i=10; i>=0; i--) {    // i already declared in previous loop
 21         double y = f (A[i]);
 22         if (y > 400.0) {
 23             cout << i << " TOO LARGE\n";
 24         } else {
 25             cout << i << ' ' << y << '\n';
 26         }
 27     }
 28 
 29     return 0;
 30 }