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 ,julia Lobur
Chapter:
Procedural Abstraction And Functions That Return A Value
Exercise:
Programming Projects
Question:16 | ISBN:9780321531346 | Edition: 7

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

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
using namespace std;

//Function definition of computeDifference
int computeDifference(int startHours, int startMinutes, bool isStartAM, int futureHours, int futureMinutes, bool isFutureAM) {
    // Convert start and future times to minutes
    int startTotalMinutes = (isStartAM ? startHours : startHours + 12) * 60 + startMinutes;
    int futureTotalMinutes = (isFutureAM ? futureHours : futureHours + 12) * 60 + futureMinutes;
    
    // Calculate the time difference in minutes
    int difference = futureTotalMinutes - startTotalMinutes;
    if (difference < 0) {
        // Handle the case when the future time is before the start time (crossing midnight)
        difference += 24 * 60;
    }
    
    return difference;
}
//main program
int main()
{
    //Declare variables
    int startHours, startMinutes, futureHours, futureMinutes;
    char ampm;
    
    // Get input for start time
    cout << "Enter start time (hours minutes AM/PM): ";
    cin >> startHours >> startMinutes >> ampm;
    bool isStartAM = (ampm == 'A' || ampm == 'a');
    
    // Get input for future time
    cout << "Enter future time (hours minutes AM/PM): ";
    cin >> futureHours >> futureMinutes >> ampm;
    bool isFutureAM = (ampm == 'A' || ampm == 'a');
    
    // Calculate the time difference
    int difference = computeDifference(startHours, startMinutes, isStartAM, futureHours, futureMinutes, isFutureAM);
    
    // Output the result
    cout << "Time difference in minutes: " << difference << endl;
    
    return 0;
}

OUTPUT OF THE PROGRAM CODE:

OUTPUT 1:

Enter start time (hours minutes AM/PM): 11 59 AM
Enter future time (hours minutes AM/PM): 12 01 PM
Time difference in minutes: 2

OUTPUT 2:

Enter start time (hours minutes AM/PM): 11 59 AM
Enter future time (hours minutes AM/PM): 11 58 AM
Time difference in minutes: 1439

 

0 0

Discussions

Post the discussion to improve the above solution.