Problem1: Your time machine is capable of going forward in time up to 24 hours.
ID: 3671621 • Letter: P
Question
Problem1:
Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time and an end time and calculate the difference in minutes between the start and end times. The end time will always be within 24 hours of the start time. Use military notation for both the start and end times (e.g. 0000 for midnight and 2359 to represent one minute before midnight).
Write C++ a function that takes as input a start time and an end time, represented as an int using military notation. The function should return the difference in minutes as an integer. Write a main program to test your function.
Hint: Be careful of time intervals that start before midnight and end the following day.
Sample output (bolded text denote input from user)
The time difference between 0000 and 2359 is 1439 minutes.
The time difference between 2359 and 0001 is 2 minutes.
The time difference between 2010 and 1000 is 830 minutes.
The time difference between 0300 and 1500 is 720 minutes.
Note: Your program must compile, Make sure that you are submitting your source code not only the excecutible file
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
//function that calculates the total minutes
int calmin(int stime, int etime){
if(stime<etime) //start time and end time on same day
return ((etime/100-stime/100)*60 + etime%100 - stime%100);
else //start time and end time on cosecutive days
return ((2400/100-stime/100)*60 - stime%100 + etime/100 *60 + etime%100);
}
int main(){
int stime,etime;
cout<<"Enter start and end time";
cin>>stime>>etime;
cout<<"The time difference between "<<setfill('0')<<setw(4)<<stime<<" and "<<setfill('0')<<setw(4)<<etime<<" is "<<calmin(stime,etime)<<" minutes";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.