Exercise 4-2 - CBD page 163

Solution index                   Textual C program


/**
 * TABLEOFPOWERS.C 
 *
 * Exercise solution 2.4.  (C By Dissection Chap. 4, ex. 2)
 *
 * Computes a table of powers.
 * 25-Feb-03 by Dimon
 **/


/**
 * INCLUDE statements 
 **/
#include <stdio.h>
#include <stdlib.h>


/**
 * DEFINE statements
 **/
#define NUMROWS 25



/**
 * Function declarations
 **/
int square(int x);
int cube(int x);
int quartic(int x);
int quintic(int x);

/**
 * The main function
 **/
int main(void) {  
  
  /* Declaration of variables */

  int i = 1; 
  int status = 0;


  /* Display table header */
  
  printf("A Table of Powers\n");
  printf("-----------------\n");
  printf("\n");
  printf("Integer   Square     Cube  Quartic  Quintic\n");
  printf("-------   ------     ----  -------  -------\n");


  /* A FOR loop calculates and prints out the results in the table */ 

  for(i = 1; i < NUMROWS; i++) {
    printf("%7d  ",i);
    printf("%7d  ",square(i));
    printf("%7d  ",cube(i));
    printf("%7d  ",quartic(i));
    printf("%7d  ",quintic(i));
    printf("\n");
  } /* End of FOR loop */

  /* Terminate program */
  return status;    

} /* END OF MAIN */


/**
 * Function definitions
 **/

/**
 * Returns the square (x^2) of the argument.
 **/
int square(int x) {
  return x*x;
}

/**
 * Returns the cube (x^3) of the argument.
 **/
int cube(int x) {
  return square(x)*x;
}

/**
 * Returns the quartic (x^4) of the argument.
 **/
int quartic(int x) {
  return square(square(x));
}

/**
 * Returns the quintic (x^5) of the argument.
 **/
int quintic(int x) {
  return square(x)*cube(x);
}


#define NUMROWS 25
The symbolic constant NUMROWS is defined, so that it becomes clear how many times the loop writing out table rows will be executed. It is always a good idea to introduce symbolic constants, like NUMROWS, whenever there is a need to have constants in your program. In particular, it makes it easy to change the size of the table, in case the table size appears more than once in your program.
 

int square(int x);
int cube(int x);
int quartic(int x);
int quintic(int x);
These are function declarations made before int main(void), such that the compiler will know the names, input arguments and return types, and write a warning if we are messing up some data types or mis-spelling the name. In each case, the return type is int, and the argument type is int. When each function is called, x becomes the name of a local variable inside the function body with the value given in the parentheses of the function call.
 

  printf("A Table of Powers\n");
  printf("-----------------\n");
  printf("\n");
  printf("Integer   Square     Cube  Quartic  Quintic\n");
  printf("-------   ------     ----  -------  -------\n");
We are now inside the main function. A nice table header is printed on the screen. Notice the number of blank spaces; they have been adjusted to the number of digits in the greatest number in the table.
 

  for(i = 1; i < NUMROWS; i++) {
A FOR loop calculates and prints out the results in the table. It starts with i = 1, and iterates for as long as i is less than NUMROWS. As i is incremented by 1 each time the loop is executed, it is executed 25 times in total. The FOR loop could also have been written for( ; i < NUMROWS; i++) {...} or for( ; i < NUMROWS; ++i) {...}, since i has already been initialized to 1, and is incremented by one for each iteration in either construction.
 

    printf("%7d  ",i);
    printf("%7d  ",square(i));
    printf("%7d  ",cube(i));
    printf("%7d  ",quartic(i));
    printf("%7d  ",quintic(i));
    printf("\n");
This is the body of the FOR loop. Each table row is printed out, followed by a newline. Each number in the table is allotted 7 digits, in which the return value of the subsequent function calls are written.
 

int square(int x) {
  return x*x;
}
This function returns the square of the argument x. At the call to the function, x is given the value of the argument in the parentheses. The function simply computes the square of x and returns it.
 

int cube(int x) {
  return square(x)*x;
}
This function returns the cube of the argument x. There is no name conflict with the x in the square function, since both x's are local variables. The function calls square(x), which is well-known at this point in time, and which returns the value x*x. It then multiplies this value by x, obtaining x*x*x, and returns the result.
 

int quartic(int x) {
  return square(square(x));
}
This function returns the quartic of the argument x. The function calls square(square(x)), i.e., square(x*x), i.e., (x*x)*(x*x).
 

int quintic(int x) {
  return square(x)*cube(x);
}
quintic(x) uses the two aforementioned functions to calculate (x^2)*(x^3). Note that e.g., square(cube(x)) would return a wrong result (why?).
 

The output of the program is the following:
Integer   Square     Cube  Quartic  Quintic
-------   ------     ----  -------  -------
      1        1        1        1        1  
      2        4        8       16       32  
      3        9       27       81      243  
      4       16       64      256     1024  
      5       25      125      625     3125  
      6       36      216     1296     7776  
      7       49      343     2401    16807  
      8       64      512     4096    32768  
      9       81      729     6561    59049  
     10      100     1000    10000   100000  
     11      121     1331    14641   161051  
     12      144     1728    20736   248832  
     13      169     2197    28561   371293  
     14      196     2744    38416   537824  
     15      225     3375    50625   759375  
     16      256     4096    65536  1048576  
     17      289     4913    83521  1419857  
     18      324     5832   104976  1889568  
     19      361     6859   130321  2476099  
     20      400     8000   160000  3200000  
     21      441     9261   194481  4084101  
     22      484    10648   234256  5153632  
     23      529    12167   279841  6436343  
     24      576    13824   331776  7962624 
 


Generated: Wednesday, March 29, 2006, 12:33:25
This program dissection page is generated from an XML-in-LAML source file