SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Parameters And Overloading
Exercise:
Programming Projects
Question:14 | ISBN:9780132846813 | Edition: 5

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 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
using namespace std;
//Function defintion of calculateTimeDifference
int calculateTimeDifference(int startTime, int endTime) 
{
    // Extract hours and minutes from the start and end times
    int startHour = startTime / 100;
    int startMinute = startTime % 100;
    int endHour = endTime / 100;
    int endMinute = endTime % 100;

    // Convert start and end times to minutes since midnight
    int startMinutes = startHour * 60 + startMinute;
    int endMinutes = endHour * 60 + endMinute;

    // Calculate the time difference in minutes
    int difference = endMinutes - startMinutes;

    // Handle the case where the end time is on the next day
    if (difference < 0) {
        difference += 24 * 60;  // Add 24 hours in minutes
    }

    return difference;
}
//Program starts with a main method
int main()
{
    //Declare variables
    int startTime, endTime;

    // Get the start time from the user
    cout << "Enter the start time in military notation (HHMM): ";
    cin >> startTime;

    // Get the end time from the user
    cout << "Enter the end time in military notation (HHMM): ";
    cin >> endTime;

    // Calculate the time difference
    int difference = calculateTimeDifference(startTime, endTime);

    // Display the time difference in minutes
    cout << "The difference in minutes between the start and end times is: " << difference << " minutes" << endl;

    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Enter the start time in military notation (HHMM): 2000
Enter the end time in military notation (HHMM): 40000
The difference in minutes between the start and end times is: 22800 minutes

 

0 0

Discussions

Post the discussion to improve the above solution.