1
0

running.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // running.c -- A useful program for runners
  2. #include <stdio.h>
  3. const int S_PER_M = 60; // seconds in a minute
  4. const int S_PER_H = 3600; // seconds in an hour
  5. const double M_PER_K = 0.62137; // miles in a kilometer
  6. int main(void)
  7. {
  8. double distk, distm; // distance run in km and in miles
  9. double rate; // average speed in mph
  10. int min, sec; // minutes and seconds of running time
  11. int time; // running time in seconds only
  12. double mtime; // time in seconds for one mile
  13. int mmin, msec; // minutes and seconds for one mile
  14. printf("This program converts your time for a metric race\n");
  15. printf("to a time for running a mile and to your average\n");
  16. printf("speed in miles per hour.\n");
  17. printf("Please enter, in kilometers, the distance run.\n");
  18. scanf("%lf", &distk); // %lf for type double
  19. printf("Next enter the time in minutes and seconds.\n");
  20. printf("Begin by entering the minutes.\n");
  21. scanf("%d", &min);
  22. printf("Now enter the seconds.\n");
  23. scanf("%d", &sec);
  24. // converts time to pure seconds
  25. time = S_PER_M * min + sec;
  26. // converts kilometers to miles
  27. distm = M_PER_K * distk;
  28. // miles per sec x sec per hour = mph
  29. rate = distm / time * S_PER_H;
  30. // time/distance = time per mile
  31. mtime = (double) time / distm;
  32. mmin = (int) mtime / S_PER_M; // find whole minutes
  33. msec = (int) mtime % S_PER_M; // find remaining seconds
  34. printf("You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n",
  35. distk, distm, min, sec);
  36. printf("That pace corresponds to running a mile in %d min, ",
  37. mmin);
  38. printf("%d sec.\nYour average speed was %1.2f mph.\n",msec,
  39. rate);
  40. return 0;
  41. }