Thread: First array problem

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    First array problem

    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I haven't written a program exactly like this, but:

    your program is too verbose. Try to always keep your programs as simple and direct, and efficient, as you can. Accuracy is absolutely primary, but these are important secondary considerations.

    In your problem, if the end time is 12, and the start time is 12 for another job, you need to have a separation between them. A simple way would be to mark your 168 element array as end time-1, and think of the job as ending at 11:59:59. Like Midnight is not 24:00:00. It stops at 23:59:59, and then rolls over to 00:00:00.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Is this what you meant? It seems to work with other test cases too .
    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, conflict = 0;
        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++)
        {
            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);
                conflict = 0;
    
                if(day ==   0)
                {
                    if(week[start_hour] == 1 || week[end_hour] == 1)
                    {
                        conflict = 1;
                    }
    
                    else
                    {
                        week[start_hour] = 1;
                        week[end_hour - 1] = 1;
    
                        for(between_hours = start_hour + 1; between_hours < end_hour - 1; 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] = 1;
    
                        for(between_hours = start_hour + 1; between_hours < end_hour - 1; 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;
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    And I get what you mean about the verbose thing. I usually end up changing the values at the end. It's just for my understanding at first
    .

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought schedule would be a two dimension array:

    sched[days][hours]

    So you'd have a day per row, and one hour per column. There are other ways to do it, though.

    I like the way you use descriptive variable names - very good! The logic though? Not related to Rube Goldberg are you?

    I've been related to RG in the past, believe me.
    Last edited by Adak; 10-16-2011 at 09:54 PM.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    If I were related to him, would I get special treatment? . We're confined to a one dimensional array. All joking aside, do you approve of my code SUR?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I haven't run it, and that's the real test - will it compile, link, and run accurately?

    Personally, I don't like code like this:

    Code:
    ...
        week[start_hour] = 1;
        week[end_hour - 1] = 1;
    
        for(between_hours = start_hour + 1; between_hours < end_hour - 1; between_hours++)
    
    
    ...
    
    //why not:
    for(between_hours=start_hour; between_hours < end_hour; between_hours++)
    You might think I'm fussy, but simplicity and clarity are more important in a program, than beginners believe, every time.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Found an issues, but I changed
    Code:
    if(between_hours == 1)
    to
    Code:
    if(week[between_hours] == 1)
    and it's working now.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good! Test it thoroughly, because the big human tendency is to quit testing, before we should. Some programs actually need other programs to test their accuracy, on a lot of data. Humans just can't do enough of it.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Yeah. I'll work on that tomorrow. Gotta work on an essay .

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    Exclamation

    For my given schedule 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
    I also need to figure out how many hours of scheduled activity are taking place within each schedule. Schedule one has 35 hours of activity because there are no conflicts. Schedule two would only have 5 hours of scheduled activity right? Since
    Code:
    2 8 17
    1 17 20
    create conflicts. According to the assignment, schedule 2 is supposed to have 14 hours of activity scheduled. Am I missing something?... -.-

    Here's the original assignment:
    Code:
    Ultimately, the only way to resolve the scheduling problems is to put different priorities
    on each activity and schedule activities from most important to least important. If a less
    important activity conflicts with a previously scheduled activity, you simply don’t do it.
    
    In this program, you’ll take the same input as the previous problem, assuming that the
    order of events in the file is in the order of importance of those events. Thus, you’ll put in
    the schedule every event that fully fits at the time you try to schedule it. Once an event is
    placed in the schedule, no event that conflicts with it may be placed in the schedule.Once
    the schedule has been processed, you’ll calculate the total number of scheduled hours
    (out of 168) for that week.
    
    Output Specification
    For each schedule, output a single line of the following form:
    Schedule #k contains X hours of scheduled activity.
    where X is a positive integer less than or equal to 168, representing the number of hours
    that are scheduled for the given week, and k (≤ n) is the schedule number starting at 1.
    
    Corresponding Output (for original input file in part B)
    Schedule #1 contains 35 hours of scheduled activity.
    Schedule #2 contains 14 hours of scheduled activity.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I read schedule two as:

    Code:
    (schedule 1 up here)
    3
    1 13 18    5 hrs
    2  8 17    9 hrs
    1 17 20    conflict from 17-18 hrs. on day 1 maybe 2 hrs?
    So, at least 14 hours of scheduled time, and maybe 2 more, depending on how your assignment wants you to handle it.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Oops. I was trying to compare schedule two with schedule one. If you do that, it's 5 hours. I see how it's 14 now... thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. problem with array i think
    By taurus in forum C Programming
    Replies: 5
    Last Post: 09-21-2009, 02:44 AM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  5. problem with array
    By stormy in forum C Programming
    Replies: 4
    Last Post: 08-24-2005, 05:01 PM