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 / count.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  50 lines

  1. /*                    count.c
  2.  *
  3.  *   Synopsis  - Takes values of initial, final, and step from 
  4.  *               the command line arguments. Counts from the 
  5.  *               low value of initial to the high value of
  6.  *               final with increments of step.
  7.  *
  8.  *   Objective - To illustrate a use of command line arguments.
  9.  *               Also uses the library function atoi().
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. /* Function Prototypes */
  17. void count( char **args );                                 /* Note 3 */
  18. /* PRECONDITION:   args contains the address of an array of pointers 
  19.  *                 to characters, namely argv[].
  20.  * POSTCONDITION:  Displays integer values from argv[1] to argv[2] 
  21.  *                 in increments of argv[3].
  22.  */
  23.  
  24. int main( int argc, char *argv[] )
  25. {
  26.      if ( argc != 4 ) {                                   /* Note 1 */
  27.           printf("Usage: count start stop step\n");
  28.           exit(1);
  29.      }
  30.                                                           /* Note 2 */
  31.      printf("Counting from %s to %s by %s's.\n",
  32.                                     *(argv+1), *(argv+2), *(argv+3) );
  33.      count( argv );
  34.      return 0;
  35. }
  36.  
  37. /*******************************count()*************************/
  38. void count( char **args )                                /* Note 3 */
  39. {
  40.      int result, initial, final, step;
  41.  
  42.      initial = atoi( args[1] );                          /* Note 4 */
  43.      final   = atoi( args[2] );
  44.      step    = atoi( args[3] );
  45.  
  46.      for ( result = initial; result <= final; result += step )
  47.           printf( "%d\t", result );
  48.      printf("\n");
  49. }
  50.