Before you read my code and such, yes I know my code looks similar to another thread on this forum. To be honest i find it pretty weird. Just to let you know, I did not in anyway copy that person's code, we just must have a similar process in mind.
Anyway here's the assignment: To read in values from a file that give a number of schedules in a week, a number of events in a week, the day of the week, and the starting and ending hour of each event. Here's the sample file:
Code:
2
10
0 8 12
1 8 12
2 8 12
3 8 12
4 8 12
5 8 12
6 8 12
3 12 15
4 20 22
5 18 20
3
1 13 18
2 8 17
1 17 20
If there are any event hours that conflict with another event, I am to output "Sorry, you double booked yourself again.". If there are no conflicts for that schedule, output "Good job, no conflicts!".
So far my array has stored all the correct values as 1 for the sample file BUT, in these cases: "3 8 12" "3 12 15", it counts it as a conflict because the end time and start time are the same. I'm not sure how to fix that. Here's my code:
Code:
#include <stdio.h>
#define WEEK_HOURS 168
#define DAY_HOURS 24
int main()
{
FILE *ifp = fopen("schedule.txt", "r");
int week[WEEK_HOURS];
int num_events, num_schedules, day, start_hour, end_hour;
int assign, schedule_counter, events_counter, between_hours;
for(assign = 0; assign < WEEK_HOURS; assign++)
{
week[assign] = 0;
}
fscanf(ifp, "%d", &num_schedules);
for(schedule_counter = 0; schedule_counter < num_schedules; schedule_counter++)
{
int conflict = 0;
fscanf(ifp, "%d", &num_events);
for(events_counter = 0; events_counter < num_events; events_counter++)
{
fscanf(ifp, "%d%d%d", &day, &start_hour, &end_hour);
if(day == 0)
{
if(week[start_hour] == 1 || week[end_hour] == 1)
{
conflict = 1;
}
else
{
week[start_hour] = 1;
week[end_hour] = 1;
for(between_hours = start_hour + 1; between_hours < end_hour; between_hours++)
{
if(week[between_hours] == 1)
{
conflict = 1;
}
else
{
week[between_hours] = 1;
}
}
}
}
else
{
start_hour = start_hour + (day * DAY_HOURS);
end_hour = end_hour + (day * DAY_HOURS);
if(week[start_hour] == 1 || week[end_hour] == 1)
{
conflict = 1;
}
else
{
week[start_hour] = 1;
week[end_hour] = 1;
for(between_hours = start_hour + 1; between_hours < end_hour; between_hours++)
{
if(between_hours == 1)
{
conflict = 1;
}
else
{
week[between_hours] = 1;
}
}
}
}
}
if(conflict == 1)
{
printf("Schedule #%d: Sorry, you double booked yourself again.\n", schedule_counter + 1);
}
else if (conflict == 0)
{
printf("Schedule #%d: Good job, no conflicts!\n", schedule_counter + 1);
}
}
fclose(ifp);
return 0;
}
Other than that, I hope everything looks well. Feel free to point out stuff.