Question: I would like to know part B, I am adding the first part to make easy for you to understand .... Lets define a datatype

I would like to know part B, I am adding the first part to make easy for you to understand....

Lets define a datatype for storing time HH:MM

We want to create this new datatype for representing time in the 24-hour format. It is reasonable to define it to have two integer fields:

h for the number of hours, and

m for the number of minutes.

Notice the semicolon after the closing }, it is necessary:

struct Time { int h; int m; }; 

Alright, lets try to use this new fancy type. There are several ways for creating variables of this type Time. The most explicit one is to create a variable and then update its fields individually:

Time now; // creates a new variable now.h = 17; // assigns its hours field now.m = 45; // assigns its minutes field  

The variable is called now, and it is storing 5:45 PM. The fields of now can be accessed as now.h ans now.m.

Alternatively, we can create a variable and immediately initialize all its fields with the following literal syntax (the order of values in curly braces is important):

Time t = { 17, 45 }; 

Lets write a printer function that prints a Time value on the screen in HOURS:MINUTES format:

void printTime(Time time) { cout  time.h  ":"  time.m; } 

One convenient feature, which you might not need but should be aware of, is that the assignment operator works for structs, it is copying the all elements of the struct, field by field:

Time morningClass = {8, 10}; Time myAlarm; // make another variable myAlarm = morningClass; // copying printTime(morningClass); // will print 8:10 printTime(myAlarm); // will print 8:10 as well // You may be late for the class tho 

A remark on C++ 11 and using C++ stuct literals

Prior to C++ 2011 standard, to use a struct, one first had to create a variable of the struct type, like in the examples above. The curly brace syntax for struct literals, such as {8, 10}, was allowed only when initilizing a struct variable, not at an arbitrary place in the program.

Thus, for instance, one couldnt create a struct object on the fly and pass it into a function, or return it from a function:

printTime({8, 10}); // was illegal  return {17, 45}; // was illegal  

You had to write explicitly:

Time t1 = {8, 10}; // first, had to create and initialize printTime(t1); // then call the function Time t2 = {17, 45}; // create  return t2; // then return 

The C++ 11 standard allows creating and initializing struct objects on the fly without making a variable to store it, so the previously illegal code is now accepted and valid. To compile your program according to the C++11 standard, use an additional command line option -std=c++11:

g++ -std=c++11 program.cpp 

Task A. Simple functions for time

Create a new program time.cpp. (Copy the struct Time declaration in your program, it should be placed before main() function.)

Implement two new functions:

int minutesSinceMidnight(Time time); int minutesUntil(Time earlier, Time later); 

The first function should return the number of minutes from 0:00AM until time.

The second function should receive two Time arguments earlier and later and report how many minutes separate the two moments. For example, when passing 10:30AM and 1:40PM:

minutesUntil( {10, 30}, {13, 40} ) // ==> should return 190 minutes 

(A caveat: If the earlier moment of time happens to be after the later moment of time, report a negative number of minutes. Although its not difficult to achieve this if your implementation for the first function is correct.)

For testing purposes, implement a simple user interface:

$ ./time Enter first time: 10 30 Enter second time: 13 40 These moments of time are X and Y minutes after midnight. The interval between them is Z minutes. 

----------------part B is the part I`m interested----------------------------------------------------------------

Task B. Making it more interesting

Add a new function to your program time.cpp:

Time addMinutes(Time time0, int min); 

The function should create and return a new moment of time that is min minutes after time0. Example:

addMinutes({8, 10}, 75) // ==> should return {9, 25} 

(We will not test how your function behaves if the new returned time will be on the next day, feel free to assume that it will remain withing the same day, ? 23:59.)

Adjust the main function for testing this function. Feel free to add additional tests to check the correctness of your code.

Scheduling time slots for a movie theater

Lets add a few more datatypes:

enum Genre {ACTION, COMEDY, DRAMA, ROMANCE, THRILLER}; 

(Enum types work as sets of named values. A variable of type Genre can assume any of the values listed in the curly braces, example: Genre myFavorite = COMEDY;)

struct Movie { string title; Genre genre; // only one genre per movie int duration; // in minutes }; struct TimeSlot { Movie movie; // what movie Time startTime; // when it starts }; 

I would like to know part B, I am adding the first

A usage example:

Movie movie1 = {"Back to the Future", COMEDY, 116}; Movie movie2 = {"Black Panther", ACTION, 134}; TimeSlot morning = {movie1, {9, 15}}; TimeSlot daytime = {movie2, {12, 15}}; TimeSlot evening = {movie2, {16, 45}}; 

For testing purposes, we would want to make functions printMovie(Movie m) and printTimeSlot(Timeslot ts). Lets write the first of them:

void printMovie(Movie mv){ string g; switch (mv.genre) { case ACTION : g = "ACTION"; break; case COMEDY : g = "COMEDY"; break; case DRAMA : g = "DRAMA"; break; case ROMANCE : g = "ROMANCE"; break; case THRILLER : g = "THRILLER"; break; } cout  mv.title  " "  g  " ("  mv.duration  " min)"; } 

(It does not print endl at the end, and printTime, which we defined in the beginning, did not print endl either. Thanks to that, both functions can be uses as part of printTimeSlotfunction.)

struct Time struct Movie struct TimeSlot title movie startTime title genre genre duratiorn duration

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!