Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your time machine is capable of going forward in time up to 24 hours. The machin

ID: 3803639 • Letter: Y

Question

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 (in hours, minutes, and a Boolean indicating AM or PM) and a future time (in hours, minutes, and a Boolean indicating AM or PM) and calculate the difference in minutes between the start and future time.

A time specified in your program with three variables:

int hours, minutes;

bool isAM;

For example, to represent 11:50 PM, you would store:

hours = 11

minutes=50

isAm = false

This means that you need six variables to store a start and future time.

Write a program that allows the user to enter a start time and a future time. Include a function named computeDifference that takes the six variables as parameters that represent the start time and future time. Your function should return, as an int, the time difference in minutes. For example, given a start time of 11:59 AM and a future time of 12:01 PM, your program should compute 2 minutes as the time difference. Given a start time of 11:59 AM and a future time of 11:58 AM, your program should compute 1439 minutes as the time difference and should display 23 hours and 59 minutes.

You may need “AM” or “PM” from the user’s input by reading in two character values or a string. i.e. ‘a’ for am and ‘p’ for pm or "am" or "pm".

ALLOW USER TO CONTINUE TO USE TIME MACHINE UNTIL HE/SHE WISHES TO STOP.

**See function below which can be used as a helper function, to avoid a little redundancy in computeDifference

SAMPLE OUTPUT

Enter start time, in the format 'HH:MM xm', where 'xm' is
either 'am' or 'pm' for AM or PM: 12:05 am

Enter future time in the format 'HH:MM xm' where 'xm' is
either 'am' or 'pm': 12:15 am

There are 10 minutes (0 hours and 10 minutes) between 12:05 AM and 12:15AM.

Another go for the time machine? Type 1 for a go and any other number of a no go=>1

Enter start time, in the format 'HH:MM xm', where 'xm' is
either 'am' or 'pm' for AM or PM: 12:58 am

Enter future time in the format 'HH:MM xm' where 'xm' is
either 'am' or 'pm': 12:59 pm

There are 721 minutes (12 hours and 1 minute) between 12:58 AM and 12:59PM.

Another go for the time machine? Type 1 for a go and any other number of a no go=>1

Enter start time, in the format 'HH:MM xm', where 'xm' is
either 'am' or 'pm' for AM or PM: 4:50 pm

Enter future time in the format 'HH:MM xm' where 'xm' is
either 'am' or 'pm': 6:30 am

There are 820 minutes (13 hours and 40 minutes) between 4:50 PM and 6:30AM.

Another go for the time machine? Type 1 for a go and any other number of a no go=>2

** FUNCTION

int minutes_since_midnight(int hours, int minutes, bool isAM)
{
using namespace std;

// Convert 12AM (midnight) to hour '0'
if ((hours == 12) && (isAM))
{
hours = 0;
}

// Add 12 to hours after noon
if ((hours < 12) && (! isAM))
{
hours += 12;
}

return minutes + (hours * MINUTES_IN_HOUR);
}

Explanation / Answer

Solution:

#include <iostream>

#include <cstring>

#include <string>

#include <iomanip>

#include<cstdlib>

using namespace std;

const int MINUTES_IN_HOUR=60;

// method to compute minutesfrom mid night

int minutes_since_midnight(int hours, int minutes, bool isAM)

{

     using namespace std;

     // Convert 12AM (midnight) to hour '0'

     if ((hours == 12) && (isAM))

     {

          hours = 0;

     }

     // Add 12 to hours after noon

     if ((hours < 12) && (! isAM))

     {

          hours += 12;

     }

     return minutes + (hours * MINUTES_IN_HOUR);

}

// method to compute the time difference

int computeDifference(int stHr, int stMt, bool stMdn, int fiHr, int fnMt, bool fnMdn)

{

     int minutes = 0;

     if((stMdn)&&(fnMdn))

     {

          minutes=minutes_since_midnight(fiHr-stHr, fnMt-stMt, 1);

     }

     else

     {

          minutes=minutes_since_midnight(fiHr - stHr,59 - stMt,0);          

     }

     return minutes;

}

// main method

int main()

{

     int strtHrs, strtMns;

     int frHrs, ftrMns;

     int conGo, diffMin;

     string strMer, frMerd;

     bool smdn=0,fmdn=0;

     while(1)

     {

          strtHrs=0, strtMns=0, smdn=0, frHrs=0, ftrMns=0, fmdn=0;

          cout<<"Enter start time, in the format 'HH:MM xm', where 'xm' is either 'am' or 'pm' for AM or PM: ";

          cin>>strtHrs>>strtMns>>strMer;

          if((strMer.compare("am") == 0)||(strMer.compare("AM") == 0))

          smdn=1;

          cout<<"Enter future time, in the format 'HH:MM xm', where 'xm' is either 'am' or 'pm' for AM or PM: ";

          cin>>frHrs>>ftrMns>>frMerd;

          if((frMerd.compare("am") == 0)||(frMerd.compare("AM") == 0))

          fmdn=1;

          diffMin = computeDifference(strtHrs, strtMns, smdn, frHrs, ftrMns, fmdn);

          cout<<"There are "<<setfill('0')<<setw(2)<<diffMin<<"minutes ("<<setfill('0')<<setw(2)<<diffMin/60<<" hours and "<<setfill('0')<<setw(2)<<diffMin%60;

          cout<<" minutes) between "<<setfill('0')<<setw(2)<<strtHrs<<":"<<setfill('0')<<setw(2)<<strtMns<<" "<<(strMer);

          cout<<" and "<<setfill('0')<<setw(2)<<frHrs<<":"<<setfill('0')<<setw(2)<<ftrMns<<" "<<(frMerd)<<"."<<endl;

          cout<<"Another go for the time machine? Type 1 for a go and any other number of a no go=> ";

          cin>>conGo;

          if(conGo != 1)

          exit(0);

     }

}

Result:

Enter start time, in the format 'HH:MM xm', where 'xm' is either 'am' or 'pm' fo

r AM or PM: 12 05 am

Enter future time, in the format 'HH:MM xm', where 'xm' is either 'am' or 'pm' f

or AM or PM: 12 15 am

There are 10minutes (00 hours and 10 minutes) between 12:05 am and 12:15 am.

Another go for the time machine? Type 1 for a go and any other number of a no go

=> 1

Enter start time, in the format 'HH:MM xm', where 'xm' is either 'am' or 'pm' fo

r AM or PM: 12 58 am

Enter future time, in the format 'HH:MM xm', where 'xm' is either 'am' or 'pm' f

or AM or PM: 12 59 pm

There are 721minutes (12 hours and 01 minutes) between 12:58 am and 12:59 pm.

Another go for the time machine? Type 1 for a go and any other number of a no go

=>