Advertisement
Guest User

PA2 Elapsed Time

a guest
Jan 25th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /*Thomas Birt
  2. COSC 1336
  3. PA - Elapsed Time
  4. January 22, 2020
  5. Program prompts the user to input the elapsed time for an event in seconds stored as an integer. The program then outputs the elapsed time in days, hours, minutes and seconds.
  6. */
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. // Write the function PROTOTYPES here for
  11. // getSeconds, getMinutes, getRemainder, getHours, getDays and displayElapsedTime
  12. // See the Function Demo Program for examples
  13.  
  14. int getSeconds();
  15. int getMinutes(int);
  16. int getRemainder(int);
  17. int getHours(int);
  18. int getDays(int);
  19. void displayElapsedTime(int, int, int, int);
  20.  
  21. const int SECONDS_PER_MINUTE = 60;
  22. const int MINUTES_PER_HOUR = 60;
  23. const int HOURS_PER_DAY = 24;
  24.  
  25. int main()
  26. {
  27. int seconds; // number of seconds user has entered
  28. int remainder; // seconds remaining
  29. int minutes;
  30. int hours;
  31. int days;
  32.  
  33. // Get the number of seconds to prime loop (or 0 to quit)
  34. seconds = getSeconds();
  35.  
  36. while (seconds != 0) // continue processes while seconds not 0
  37. {
  38. minutes = getMinutes(seconds);
  39. remainder = getRemainder(seconds);
  40. if (seconds > 60)
  41. {
  42. seconds %= SECONDS_PER_MINUTE;
  43. }
  44. hours = getHours(minutes);
  45. if (minutes > 60) {
  46. minutes %= MINUTES_PER_HOUR;
  47. }
  48. days = getDays(hours);
  49. if (hours > 24) {
  50. hours %= HOURS_PER_DAY;
  51. }
  52. displayElapsedTime(days, hours, minutes, remainder);
  53. seconds = getSeconds(); // modify loop control variable
  54. }
  55.  
  56. system("pause");
  57. return 0;
  58. }
  59.  
  60. // Write the function DEFINITIONS here for
  61. int getSeconds()
  62. {
  63. int seconds; // radius value input by user
  64.  
  65. cout << "\nEnter the elapsed time in seconds: ";
  66. cin >> seconds;
  67.  
  68. return seconds;
  69. }
  70.  
  71.  
  72. int getRemainder(int seconds)
  73. {
  74. return seconds % SECONDS_PER_MINUTE; // seconds left over from dividing
  75. }
  76.  
  77. int getMinutes(int seconds)
  78. {
  79. return seconds / SECONDS_PER_MINUTE; // calculating number of minutes
  80. }
  81.  
  82. int getHours(int minutes)
  83. {
  84. // calculating number of hours
  85. return minutes / MINUTES_PER_HOUR;
  86. }
  87.  
  88. int getDays(int hours)
  89. {
  90. return hours / HOURS_PER_DAY; // calculating number of days
  91. }
  92.  
  93. void displayElapsedTime(int days, int hours, int minutes, int remainder)
  94. {
  95. cout << "\nThe equivalent time in days hours:minutes:seconds = " << days << " and " << hours << ":" << minutes << ":" << remainder << endl;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement