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

  1. /*                    miles.c
  2.  *
  3.  *    Synopsis-                Accepts a number of gallons as input and
  4. *                    calculates and displays the number of miles
  5.  *                    that can be traveled.
  6.  *
  7.  *    Objective-                To illustrate the use of a preprocessor-defined
  8.  *                    constant to make a program easily modifiable,
  9.  *                    and the use of parameters and return values to
  10.  *                    make a function independent.
  11.  */
  12.  
  13. /* Include Files */
  14. #include <stdio.h>
  15.  
  16. /* Preprocessor Constants */
  17. #define MILEAGE 28                                                            /* Note 1 */
  18.  
  19. /* Function Prototypes */
  20. int miles (int num_gallons, int mileage );
  21. /* PRECONDITION:                    num_gallons is an integer that represents the
  22.  *                    amount of gasoline available. mileage is the
  23.  *                    miles per gallon of a vehicle.
  24. *
  25.  * POSTCONDITION:                    The return value is the number of miles that
  26.  *                    can be traveled with the vehicle and that
  27.  *                    amount of gas.
  28.  */
  29.  
  30. int main(void)
  31. {
  32.     int gallons;
  33.     printf("        Travel Calculator\n");
  34.     printf("---------------------------------\n");
  35.                                                                                         /* Note 2 */
  36.     printf("Current mileage is %d miles per gallon.\n", MILEAGE);
  37.     printf("\n\nEnter the gallons of gas (whole numbers please): ");
  38.     scanf("%d", &gallons);
  39.                                                                                         /* Note 3 */
  40.     printf("Thank you. You will be able to travel %d miles.\n",
  41.                                         miles( gallons, MILEAGE));
  42.     return 0;
  43. }
  44. /*******************************miles()**************************/
  45. int miles( int num_gallons, int mileage )                                                            /* Note 4 */
  46. {
  47.     return ( num_gallons * mileage );
  48. }
  49.