A Sample Program

I l @ ve RuBoard

A Sample Program

Listing 5.16 is a useful program (if you're a runner or know one) that illustrates several of the ideas in this chapter. It looks long, but all the calculations are done in six lines near the end. The bulk of the program relays information between the computer and the user . We've tried using enough comments to make it nearly self-explanatory. Read through it, and when you are done, we'll clear up a few points.

Listing 5.16 The running.c program.
 /* running.c -- A useful program for runners */ #include <stdio.h> #define S_PER_M      60  /* seconds in a minute  */ #define S_PER_H    3600  /* seconds in an hour   */ #define M_PER_K 0.62137  /* miles in a kilometer */ int main(void) {   double distk, distm;  /* distance run in km and in miles     */   double rate;          /* average speed in mph                */   int min, sec;         /* minutes and seconds of running time */   int time;             /* running time in seconds only        */   double mtime;         /* time in seconds for one mile        */   int mmin, msec;       /* minutes and seconds for one mile    */   printf("This program converts your time for a metric race\n");   printf("to a time for running a mile and to your average\n");   printf("speed in miles per hour.\n");   printf("Please enter, in kilometers, the distance run.\n");   scanf("%lf", &distk);  /* %lf for type double            */   printf("Next enter the time in minutes and seconds.\n");   printf("Begin by entering the minutes.\n");   scanf("%d", &min);   printf("Now enter the seconds.\n");   scanf("%d", &sec);   time = S_PER_M * min + sec;                         /* converts time to pure seconds       */   distm = M_PER_K * distk;                         /* converts kilometers to miles        */   rate = distm / time * S_PER_H;                         /* miles per sec x sec per hour = mph  */   mtime = (double) time / distm;                         /* time/distance = time per mile       */   mmin = (int) mtime / S_PER_M; /* find whole minutes       */   msec = (int) mtime % S_PER_M; /* find remaining seconds   */   printf("You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n",        distk, distm, min, sec);   printf("That pace corresponds to running a mile in %d min, ",        mmin);   printf("%d sec.\nYour average speed was %1.2f mph.\n",msec,        rate);   return 0; } 

Listing 5.16 uses the same approach used earlier in min_sec to convert the final time to minutes and seconds, but it also makes type conversions. Why? Because you need integer arguments for the seconds-to-minutes part of the program, but the metric-to-mile conversion involves floating-point numbers . We have used the cast operator to make these conversions explicit.

To tell the truth, it should be possible to write the program using just automatic conversions. In fact, we did so, using mtime of type int to force the time calculation to be converted to integer form. However, that version failed to run on one of the seven systems we tried. That compiler (a somewhat obsolete version) failed to follow the C rules. Using type casts makes your intent clearer not only to the reader, but perhaps to the compiler as well.

Here's a sample output:

 This program converts your time for a metric race to a time for running a mile and to your average speed in miles per hour. Please enter, in kilometers, the distance run.  10.0  Next enter the time in minutes and seconds. Begin by entering the minutes.  36  Now enter the seconds.  23  You ran 10.00 km (6.21 miles) in 36 min, 23 sec. That pace corresponds to running a mile in 5 min, 51 sec. Your average speed was 10.25 mph. 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net