C++ program Your time machine is capable of going forward in time up to 24 hours
ID: 3753616 • Letter: C
Question
C++ program
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 them. 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 for one minute before midnight). Write 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 driver program that calls your subroutine with times entered by the user. Hint: Be careful of time intervals that start before midnight and end the following day.
Explanation / Answer
#include<iostream>
using namespace std;
//method that takes start time and end time
//returns the time difference in minutes in integer
int Difference(int start ,int end)
{
//seperating hrs and minutes of start time
int hs = start/100;
int ms = start - hs*100 ;
//seperating hrs and minutes of end time
int he = end/100;
int me = end - he*100 ;
//finding hours difference
int h = he-hs,m;
if(me<ms)//here minutes in end time is less than minutes in start time
{
h--;//so decreasing one hour
m = (60-ms)+me;//calculating minutes
}
else//here minutes in start time is less than minutes in end time
{
m = me - ms;//
}
//converting hours to minutes
m = m + (h*60);
//returning total minutes
return m;
}
//testing
int main()
{
//pls give a thumbs up if you find this helpful
//feel free to ask doubts in comments, if have any
int s,e;//variable declaration
//reading input
cout<<"Enter start time:(24 hrs format):";
cin>>s;
cout<<"Enter end time:(24 hrs format):";
cin>>e;
cout<<"Minutes :"<<Difference(s,e)<<endl;
return 0;
}
output:
Enter start time:(24 hrs format):0110
Enter end time:(24 hrs format):0220
Minutes :70
Process exited normally.
Press any key to continue . . .
output2:
Enter start time:(24 hrs format):0220
Enter end time:(24 hrs format):0310
Minutes :50
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.