The C Program: tpk.c
1 /* tpk.c: Knuth's TPK program in the C programming language */
2
3 #include <stdio.h> /* include header file for standard I/O */
4 #include <math.h> /* include header file for math functions */
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], y;
13 int i;
14
15 /* Read in the values of the array A */
16 for (i=0; i<11; i++) {
17 scanf ("%lf", &A[i]);
18 }
19
20 /* In reverse order, apply "f" to each element of A and print */
21 for (i=10; i>=0; i--) {
22 y = f (A[i]);
23 if (y > 400.0) {
24 printf ("%d TOO LARGE\n", i);
25 } else {
26 printf ("%d %f\n", i, y);
27 }
28 }
29 return (0);
30 }