home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch9 / multi.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  50 lines

  1. /*                   multi.c
  2.  *
  3.  *   Synopsis  - Displays a multiplication table for positive
  4.  *               integer products between 0*0 and 3*4.
  5.  *
  6.  *   Objective - Demonstrates multidimensional arrays and
  7.  *               the ?: construct.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Constant Definitions */
  14. #define ROWS        4
  15. #define COLUMNS     5
  16.  
  17. /* Function Prototypes */
  18. void printab( int rows, int columns, int array[][5] );     /* Note 1 */
  19. /* PRECONDITION:  rows and columns contain the numbers of rows and 
  20.  *                columns of array.
  21.  * POSTCONDITION: Displays the contents of an array of type int.
  22.  */
  23.  
  24. int main( void )
  25. {
  26.      int multiarray[ROWS][COLUMNS],                       /* Note 2 */
  27.          row, column;
  28.  
  29.      for ( row = 0; row < ROWS; row++ )                   /* Note 3 */
  30.           for ( column = 0; column < COLUMNS; column++ )
  31.                multiarray[row][column] = row * column;
  32.  
  33.      printab( ROWS, COLUMNS, multiarray );
  34.      return 0;
  35. }
  36.  
  37. /******************************* printab() **************************/
  38.                                                           /* Note 4 */
  39. void printab( int rows, int columns, int array[][COLUMNS] )
  40. {
  41.      int i = 0, j = 0;
  42.  
  43.      while ( i < rows ) {
  44.           printf( "%d%c", array[i][j],                    /* Note 5 */
  45.                               ( j == columns-1 ) ? '\n' : '\t' );
  46.                                                           /* Note 5 */
  47.           ( j == columns - 1 ) ? i++,j=0 : j++;
  48.      }
  49. }
  50.