Ask Question
10 March, 09:02

Write a program that allows the user to enter a start time and a future time. Include a function named compute Difference 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.

+2
Answers (1)
  1. 10 March, 12:43
    0
    Step 1

    Following is the c+ + program:

    Variables used:

    startHours, startMins are used to store the starting time of time machine.

    futureHours, futureMins are used to store the future time of time machine.

    xAM variable is used to store meridiem of start and future time.

    isSAM is boolean variable that is true if starting time is AM.

    isFAM is boolean variable that is true if future time is AM.

    Functions:

    main () is used to call computeDifference () function that will calculate the difference between the start and future time.

    computeDifference () takes as input six variables from the user and then apply the following logic to compute the difference between the start and future time:

    If Meridien of both start and future time are the same, then simply subtract the start time from the future time.

    Difference = future minutes - start minutes

    If Meridien of both start and future time are different, then apply the following formula to calculate difference:

    Difference = (12 * 60 - start minutes) + future minutes

    Step 2

    Program:

    #include / /header file for input and output

    using namespace std;

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

    int computeDifference (int startHours, int startMins, bool isSAM, int futureHours, int futureMins, bool isFAM) {

    int difference = 0; / /to store difference

    startMins = startHours * 60 + startMins; / /converting in minutes.

    futureMins = futureHours * 60 + futureMins; / /converting in minutes.

    //applying formula for difference.

    if (isSAM = = isFAM) {

    difference = futureMins - startMins;

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

    difference = (12 * 60 - startMins) + futureMins;

    }

    }

    int main () {

    int startHours, startMins; / /it will store start time

    int futureHours, futureMins; / /it will store future time.

    string xAM; / /it will store meridiem.

    bool isSAM; / /it will be true if start time is AM.

    bool isFAM; / /it will be true if future time is in AM.

    int difference = 0; / /it will store difference.

    cout << "Enter start time of time machine as 'HH MM AM/PM': "; / /take input from
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that allows the user to enter a start time and a future time. Include a function named compute Difference that takes the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers