How to see if there are time conflicts in schedule?

I have written this program that prints out your schedule in neat way, however, my problem is that I have to put a function that detect if there were any conflict time or not, so the program goes like: "
how many courses you have?

name of the first course?
ho many days?
enter the days?

at what time does your course start?
at what time does your course end?
"
Then it prints out a formatted schedule and shows it to you in a nice way, so that it is easy to read.

Could you please guide me on how to check if there is a conflicting time schedule? Also, my assignment requirement states that I have to make every function be at most 10 lines long!!!

My also instructor said that it would be easier for you to use a military time to find the conflicting time schedules, but the problem is, I do not know how to implement it!

Can you please help me with this or give hints that I may find helpful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  #include<iostream>

using namespace std;

struct course
{  
   string name;
   int num_days;
   char *days;
   int start_hr, start_min;
   int end_hr, end_min;
};

void get_info(struct course * courses, int num_cour);
void _free(struct course* courses, int num_cour);
void Help();
void pr(struct course *courses, int num_cour);
void pri(struct course *courses, int num_cour);
void prii(struct course *courses, int num_cour);
void priii(struct course *courses, int num_cour);
void priiii(struct course *courses, int num_cour);
void time(struct course *courses, int num_cour);
void conflict(struct course * courses,int num_cour);
int main(){
   course *courses;
   int num_cour;
   cout << " ****************************************************** \n";
/***** here i get the number of the courses ***/   
   cout << "pls enter number of course:"; cin>> num_cour;
   courses = new course[num_cour];

   get_info(courses, num_cour);
   pr(courses, num_cour);
   pri(courses, num_cour);
   prii(courses, num_cour);
   priii(courses, num_cour);
   priiii(courses, num_cour);

   _free(courses, num_cour);

   cout << " ****************************************************** \n";
   return 0;
}
/***** in this function i get the names of the courses and the days ******/ 
void get_info(struct course* courses, int num_cour)
{  
   for(int i=0; i< num_cour; i++)
   {  
      cout << "name:"; 
      cin>> courses[i].name;
      cout << "How many days" << endl;
      cin>> courses[i].num_days;
      courses[i].days=new char[courses[i].num_days];
      for(int x=0; x < courses[i].num_days ; x++){
         Help(); 
         cout << "Enter the day" << endl;
         cin >> courses[i].days[x];
      }
   }
   time(courses, num_cour); // this is a call for another function that asks the user to enter the time of the courses when it starts and ends. 
}
/******* this function prints out the courses on Mondays only ****/
void pr(struct course *courses, int num_cour){
   cout << "** On Monday **\n";
   for(int i=0; i<num_cour; i++){

      for(int x=0; x<5;x++){
         if(courses[i].days[x]=='m'){
            cout << courses[i].name<<": " << courses[i].start_hr << ":"<< courses[i].start_min << "-" << courses[i].end_hr<< ":" << courses[i].end_min<< endl;
         }

      }
   }
}


/******* this function prints out the courses on tusdays only ****/
void pri(struct course *courses, int num_cour){
   cout << "** On Tusday **\n";
   for(int i=0; i<num_cour; i++){

      for(int x=0; x<5;x++){
         if(courses[i].days[x]=='t'){
            cout << courses[i].name<<": " << courses[i].start_hr << ":"<< courses[i].start_min << "-" << courses[i].end_hr<< ":" << courses[i].end_min<< endl;
         }

      }

   }
}

/******* this function prints out the courses on Wednesday only ****/
void prii(struct course *courses, int num_cour){

   cout << "** On Wednesday **\n";
   for(int i=0; i<num_cour; i++){

      for(int x=0; x<5;x++){
         if(courses[i].days[x]=='w'){
            cout << courses[i].name<<": " << courses[i].start_hr << ":"<< courses[i].start_min << "-" << courses[i].end_hr<< ":" << courses[i].end_min<< endl;
         }

      }

   }
}
/******* this function prints out the courses on thursdays only ****/
void priii(struct course *courses, int num_cour){
   cout << "** On Thursday **\n";
   for(int i=0; i<num_cour; i++){

      for(int x=0; x<5;x++){
         if(courses[i].days[x]=='r'){
            cout << courses[i].name<<": " << courses[i].start_hr << ":"<< courses[i].start_min << "-" << courses[i].end_hr<< ":" << courses[i].end_min<< endl;
         }

      }

   }
}
/******* this function prints out the courses on fridays only ****/
void priiii(struct course *courses, int num_cour){
   cout << "** On Friday **\n";
   for(int i=0; i<num_cour; i++){

      for(int x=0; x<5;x++){
         if(courses[i].days[x]=='f'){
            cout << courses[i].name<<": " << courses[i].start_hr << ":"<< courses[i].start_min << "-" << courses[i].end_hr<< ":" << courses[i].end_min<< endl;
         }

      }

   }
}

/**** this function to frees out the memory ******/ 
void _free(struct course* courses, int num_cour)
{
   for(int i=0; i<num_cour; i++)
   {
      delete [] courses[i].days;
   }
   delete [] courses;
}

void Help(){

   cout <<"*** NOTE: ENTER (m) for Monday, (t) Tusday, (w) Wednesday, (r) Thursday, (f) Friday ***\n";
}

/******* this function is to get the time where the course starts and ends and it is also been called in an earlier function ****/
void time(struct course *courses, int num_cour){
   for( int i=0; i<num_cour; i++){
      cout << " at what hour is your "<< courses[i].name << " class start: ";
      cin >> courses[i].start_hr;
      cout << " at what minute is your "<< courses[i].name << " class start: ";
      cin >> courses[i].start_min;
      cout << " at what hour is your "<< courses[i].name << " class end: ";
      cin >> courses[i].end_hr;
      cout << " at what minute is your "<< courses[i].name << " class end: ";
      cin >> courses[i].end_min;
   }
}
void conflict(struct course * courses, int num_cour){

}
Lets think about this logically for minute:

First you need to check if two classes are on the same day (they can't be conflicting if they are not even on the same day). Then you need to see:

if start hr and minutes of first class >= start hr and min of second class AND <= end hour and min of second class
OR end hr and min for first class >= star hr and min of second class AND <= end hr and min of second class
OR star hr and min or first class <= star hr and min of second class AND end hr and min of first class >= end hr and min of second class.

In other words, there are 3 cases here:

First class: 4:15 - 6:00
Second class: 3:00 - 4:30
Here the start of first class is within the time period of the second class.

OR

First class: 4:15 - 6:00
Second class: 5:30 - 7:00
Here the end of the first class is within the time period of the second class.

OR

First class: 4:15 - 6:00
Second class: 4:30 - 5:45
Here the second class is entirely within the first class.

You will need to make an an if-statement out of this.

I would create a separate conflict function for each day of the week and send each class to its respective function where its pushed into a vector. If a second class is sent to a function, its compared with the first using the if-statement and then also pushed into the vector. If a third class comes in, its compared with the first two (for-loop iterating through vector) using if-statement and then pushed into vector as well. Keep doing this for every class sent to that function.

If you dont want to make a separate function for each day of the week, you can simply check if the two classes being compared are on the same day (in a separate or the same if-statement).

Hope this helps.


Last edited on
thanks a lot man that is helpful
I implemented a similar data structure for work many years ago. It went something like this:

- a TimeInterval is a an interval of time, stored as the start and end time, where I represented as the number of second since 00:00 on Sunday.
- Two time intervals do NOT intersect if one starts after then end of the other. They DO
intersect if that's false:
1
2
3
4
bool TimeInterval::intersects(const TimeInterval &other) const
{
    return !(start > other.end || other.start >end);
}

- There is also a collection of intervals, sorted by start time.

Topic archived. No new replies allowed.