home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap08 / change2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  1.0 KB  |  46 lines

  1. /* change2.c -- modified to demonstrate passing       */
  2. /*              an address to a function              */
  3.  
  4. #define NCOINS (4)
  5. #define CENT (0x9b)  /* IBM PC cent character */
  6.  
  7. main()
  8. {
  9.     static int coins[NCOINS] = {25, 10, 5, 1};
  10.     int pennies;
  11.     float amount;
  12.  
  13.     printf("Enter an amount and I will ");
  14.     printf(" give you change.\nAmount: ");
  15.     if (scanf("%f", &amount) != 1)
  16.         {
  17.         printf("I don't know how to change that!\n");
  18.         exit(1);
  19.         }
  20.     pennies = (int)(amount * 100.0);
  21.  
  22.     Show_change( coins, &coins[NCOINS], pennies);
  23.  
  24. }
  25.  
  26. Show_change( int amts[], int *end, int due)
  27. {
  28.     int count;
  29.  
  30.     while ( amts < end )    /* compare pointers */ 
  31.         {
  32.         count = 0;
  33.     while ((due -= *amts) >= -1)
  34.             {
  35.             ++count;
  36.             }
  37.         if (count > 0)
  38.             printf("%4d %2d%c\n", count, *amts, CENT);
  39.         if (due == 0)
  40.             break;
  41.         due += *amts;
  42.  
  43.         ++amts;             /* increment a pointer */
  44.         }
  45. }
  46.