Thread: Checking to see if an array is filled

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    25

    Checking to see if an array is filled

    I'm working on two programs that both involve scheduling. The program reads in a data file, calculates some times (all in military), and fills in the appropriate spots with a 1. It then checks to see if spots conflict with each other. (For instance, if an event runs from 13 to 18 on one day, and another runs on the same day from 17 to 20). If no conflicts appear, then the schedules are okay to use. If there are conflicts, then the program says that the user has double booked. This is what I came up with at the moment to see if it would do what I want:

    Code:
    if((start_index <= schedule[i]) || (schedule[i] <= end_index))
                printf("Sorry, you double booked yourself again. \n");
            else
                printf("Good job, no conflicts! \n");
    I know this isn't right, but I'm still thinking of other combinations and am not having much luck. Can you guys steer me on the right path?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have an array of 168 slots, one for every hour of the week, 24 per day. You can use a 1 dimension array[168], or you can use array[7][24]. I believe the latter is the more intuitive choice, but either works perfectly fine.

    In your initialization, you'll set the array contents to all zero's. Then in a for loop from start_hr to < end_hr, you will mark up the array with a 1 value. Remember that if the end_hr is say, 12 - and the start is 9, then you'll mark up only 9, 10, and 11, NOT 12.

    Before you start writing code, study the problem, and no, you don't need the code you posted, at all. Use a for loop:

    This is a simplified code. You will have to add the day portion to it (depending on what kind of array you use), before it can be of any actual use.
    Code:
    for(i=start_hr;i<stop_hr;i++) {
       if(schedule[i] == 1) {
           printf("You have a conflict, buddy!\n");
       return 1; // or set a flag to indicate that there was a conflict, then break or return.
       //once a conflict is found, there's no reason to keep looping through, checking.
    }
    Last edited by Adak; 10-17-2011 at 12:02 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    Thing is, I use a for loop like that to fill in the spots. I kind of see how I might use another loop with if statements to see if there are conflicts or not, but the picture doesn't come to mind.

    By the way, how did you know that I was using an array set to 168? I guess it was kind of obvious, huh? lol
    Last edited by ljgerr93; 10-17-2011 at 12:17 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ljgerr93 View Post
    Thing is, I use a for loop like that to fill in the spots. I kind of see how I might use another loop with if statements to see if there are conflicts or not, but the picture doesn't come to mind.
    It does not have to be a separate loop.

    However, with the single loop you will run into the issue of filling in (eg) two hours, then hitting a conflict on the third hour, then having to go back and remove the two hours filled in if the user wants to reschedule the conflict. The simplest way to deal with that would be to use two loops, one which checks and one which actually fills in if there is no conflict (but there are other ways, I think).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    Hmm...you know, that's a good point. A loop that checks to see if an event is filled or not. If the spot in the array is still 0, which means free, then the spots for the times will be set to 1. If they are filled, there's a conflict.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    All right, it works now! Thanks for the tip, MK27!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. File handling with Array filled with a class
    By MarlonDean in forum C++ Programming
    Replies: 18
    Last Post: 06-27-2008, 10:53 AM
  3. Replies: 3
    Last Post: 11-16-2007, 03:10 PM
  4. Filled array
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 05:36 AM