C++ Programming Your time machine is capable of going forward in time up to 24 h
ID: 3797124 • Letter: C
Question
C++ Programming
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 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. If input is 0 and 2359 Enter start time: Enter end time: The time difference between 0 and 2359 is 1439 minutes. If input is 2359 and 1 Enter start time: Enter end time: The time difference between 2359 and 1 is 2 minutes. If input is 2010 and 1000 Enter start time 2010 Enter end time 1000 The time difference between 2010 and 1000 is 830 minutes.Explanation / Answer
//The program is given below:
#include <iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int timeDifference(int start,int end)
{
int minutes; //minutes is for results in minutes
int diffHours; //for difference between hours
int diffMinutes; //for difference between minutes
//calculate difference between hours and minutes
diffHours = end/100-start/100;
diffMinutes = end%100-start%100;
//if condition is used when end<=start 1440 are 24 hours in minutes.
if(end<=start)
minutes=1440-abs((diffHours*60)+diffMinutes);
else
minutes=abs((diffHours*60)+diffMinutes);
return(minutes);
}
int main()
{
int startTime=0;
int endTime=0;
//prompt and accept input from user
cout<<"Enter start time: "<<endl;
cin>>startTime;
cout<<"Enter end time: "<<endl;
cin>>endTime;
//display result
cout<<"The time difference between "<<startTime<<" and "<<endTime<<" is "<<timeDifference(startTime, endTime)<<" minutes."<<endl;
return 0;
}
/* Output
sh-4.2$ main Enter start time: 0 Enter end time: 2359 The time difference between 0 and 2359 is 1439 minutes.
sh-4.2$ main Enter start time: 2359 Enter end time: 1 The time difference between 2359 and 1 is 2 minutes.
sh-4.2$ main Enter start time: 2010 Enter end time: 1000 The time difference between 2010 and 1000 is 830 minutes. sh-4.2$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.