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

  1. /*                    arraypar.c
  2.  *
  3.  *   Synopsis  - Accepts input of int values (an inventory) from 
  4.  *               standard input, stores them in an array and 
  5.  *               displays them on standard output.
  6.  *
  7.  *   Objective - To illustrate passing an array as a parameter to a 
  8.  *               function.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13.  
  14. /* Constant Definitions */
  15. #define MAX 20
  16.  
  17. /* Function Prototypes */
  18. void print_inventory( int inventory[], int numitems);         /* Note 1 */
  19. /*   PRECONDITION:  inventory is an array that has at least numitems 
  20.  *                  cells.
  21.  *
  22.  *   POSTCONDITION: The first numitems elements in the array inventory
  23.  *                  have been displayed on the terminal screen.
  24.  */
  25.  
  26. int input_inventory( int inventory[], int maxnum );           /* Note 1 */
  27. /*   PRECONDITION:  inventory is an array that has at least maxnum 
  28.  *                  cells.
  29.  *
  30.  *   POSTCONDITION: Some cells of the array inventory have been 
  31.  *                  populated with elements entered from the keyboard. 
  32.  *                  The return value is the number of cells that were 
  33.  *                  populated.
  34.  */
  35.  
  36. int main( void )
  37. {
  38.      int inventory[MAX];                                     /* Note 2 */    
  39.      int num_items;
  40.  
  41.      printf( "Please enter the number of items in stock." );
  42.      printf( "  Enter -1 when you are done.\n" );
  43.  
  44.                                                              /* Note 3 */
  45.      num_items = input_inventory( inventory, MAX );
  46.                                                              /* Note 4 */
  47.      print_inventory( inventory, num_items );
  48.      return 0;
  49. }
  50.  
  51. /*******************************input_inventory()****************/
  52.  
  53. int input_inventory( int inventory[], int maxnum )           /* Note 5 */
  54. {
  55.      int index = 0;
  56.  
  57.      scanf( "%d", &inventory[ index ] );
  58.      while ( index < maxnum-1 && inventory[ index ] != -1 ) {
  59.           index++;
  60.           scanf( "%d", &inventory[index] );                  /* Note 6 */
  61.      }
  62.      if ( index == maxnum - 1 ) {
  63.           printf( "No room for more items.\n" );
  64.           return ( index+1 );
  65.      }
  66.      else return ( index );
  67. }
  68.  
  69. /*******************************print_inventory()****************/
  70.  
  71. void print_inventory( int inventory[], int numitems )        /* Note 5 */
  72. {
  73.      int index;
  74.  
  75.      for ( index = 0; index < numitems; index++ ) {
  76.           printf( "Item number %d:\t\t", index+1 );
  77.           printf( "Number on hand  %5d\n", inventory[index] );
  78.      }
  79. }
  80.