4.7: Time Machine 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 is 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 (23 hours and 59 minutes).You may need “AM” or “PM” from the user’s input by reading in two character values. (Display 2.3 illustrates character input.) Characters can be compared just like numbers. for example, if the variable a_char is of type char, then (a_char == 'A')is a Boolean expression that evaluates to true if a_char contains the letter A.step by step explanation

Question
Asked Oct 18, 2019
186 views

4.7: Time Machine 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 is 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 (23 hours and 59 minutes).


You may need “AM” or “PM” from the user’s input by reading in two character values. (Display 2.3 illustrates character input.) Characters can be compared just like numbers. for example, if the variable a_char is of type char, then (a_char == 'A')is a Boolean expression that evaluates to true if a_char contains the letter A.

step by step explanation

check_circle

Expert Answer

Step 1

Following is the c++ program that takes start time and future time as input from user and finally prints the difference (in minutes) using computeDifference function.

Logic used:

  • First convert start time and future time into minutes.
  • If both start time and future time belongs to the same meridiem, then directly subtract the start minutes from future minutes.
  • If both start time and future time belongs to different meridiem, then subtract the start minutes from 720 minutes(total minutes in 12 hours) and finally add future minutes to it.
Step 2

#include <iostream> //header file for input output

using namespace std;

// computeDifference will calculate the time difference(in minutes) between start time and future time.

int computeDifference(int sHours, int sMins, bool isSAM, int fHours, int fMins, bool isFAM) {

  int differ = 0; //variable to store difference

  sMins = sHours * 60 + sMins; //converting start time in minutes.

  fMins = fHours * 60 + fMins; //converting future time in minutes.

  //applying logic for time difference.

  if (isSAM == isFAM) {

    differ = fMins - sMins;

  } else if (isSAM == true && isFAM == false) {

    differ = (12 * 60 - sMins) + fMins;

  }

}

int main() {

  int sHours, sMins; //variables to store start time.

  int fHours, fMins; //variables to store future time.

  string xAM; //variables to store meridiem.

  bool isSAM; //boolean variables to check if start time is AM or not.

  bool isFAM; //boolean variables to check if future time is AM or not .

  int diff = 0; //variables to store difference.

  cout << "Enter start time as 'HH M...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: a. Construct a Huffman code for the following data: Symbol A L I Z P Frequency 4.5 .5 3.0 1.0 1...

A: a)Constructing Hauffman code for the given data:

question_answer

Q: Design a reasonably efficient algorithm for solving each of the following problems and determine its...

A: Instructions:Initialize 50 state counters Count[] to zero.Traverse the given list of n student recor...

question_answer

Q: in python 1. Write a program named filemaker.py that will be used to store the first name and age of...

A: Program code:

question_answer

Q: Suppose you have two tables: EMPLOYEE and EMPLOYEE_1. The EMPLOYEE table contains the records for th...

A: Two tables are given named as EMPLOYEE and EMPLOYEE1. Each table has its records. EMPLOYEE table has...

question_answer

Q: Using the initial dependency diagram (attached), remove all partial dependencies, draw the new depen...

A: According to the given dependency diagram, there is no partial dependency but there are transitive d...

question_answer

Q: Assume that you will be given a *.txt file, which in the same format of RandSource.txt(attached). Th...

A: A C++ program to generate random numbers between given range is provided below;//Declare necessary h...

question_answer

Q: Can someone help me on solving this? Thank you!

A: For loop:A loop is used repeatedly to execute a sequence of statements until a given condition is fu...

question_answer

Q: Python 3.7.4: Write a loop that reads positive integers from standard input and that terminates when...

A: Following is the program code that takes input from the user all positive integer and terminates if ...

question_answer

Q: I can't figure out what is wrong with my assembly code it seems to error out when I add the follow l...

A: Error:The statement “movl %eax, %rdx” gives an error saying that destination operand incorrect size.