0

Structures:

struct tod{
    int minute;
    int hour;
};
struct event
{
    int start, end;
}; 

Array:

int main (void)
{
    struct event schedule[] = {{{9,45},{9,55}},{{13,0},
        {14,20}},{{15,0},{16,30}}};
    printf ("%d\n", freetime (schedule,3,8,0));

}

How come when i do schedule[0].end I get 9 and not 45? or schedule[1].end I get 13 instead of 55. How do I get the minute values in the array? Aren't the first set of 3 curly braces the start times? and the second set the end times? I don't know how to use the structures above to save these values.

Here is my code

int freetime (struct event schedule[], int n, int hour, int min)
{
    struct tod time1;
    struct tod time2;
    int i;
    int result = 1;
    for (i=0; i<n; i++)
    {
        time1.hour = schedule[i].start;
        time1.minute = schedule[i].end;
        i++;
        time2.hour = schedule[i].start;
        time2.minute = schedule[i].end;
        if(hour >= time1.hour && hour < time2.hour)
        {
            if(min >= time1.minute && min < time2.minute)
            {
                result = 0;
                break;
            }
        }
    }
    return result;
}

freetime is supposed to return 1 if the specified time (hour and minute) is not part of any scheduled event; returns 0 otherwise. The value n specifies the size of the array containing the schedule.

3
  • {{{9,45},{9,55}},{{13,0},{14,20}},{{15,0},{16,30}}} represents a 2D array of type struct event Commented Oct 29, 2014 at 14:42
  • in this line: int freetime (struct event schedule[], int n, int hour, int min), schedule is actually a pointer to an array of schedule structs. so to reference a specific struct in that array you can use schedule[x] then to reference a specific field within that selected struct: schedule[x]->start or schedule[x]->end. Your current code, at compile time, should have resulted in a number of warnings/errors Commented Oct 30, 2014 at 0:16
  • on this line: struct event schedule[] = {{{9,45},{9,55}},{{13,0}, {14,20}},{{15,0},{16,30}}}; you could remove the outermost set of braces '{' and '}' as they imply a further level of nesting that does not exist. Commented Oct 30, 2014 at 0:23

3 Answers 3

2

Your event struct should probably look like this:

struct event
{
    tod start, end;
}; 

as you were trying to store tod's in an int. This led to you storing your first 'time' in the first event, and the second 'time' was pused into your second event (and so on)

Try this, for testing:

//returns true if tod1 comes before tod2
bool tod_before(tod tod1, tod tod2)
{
     return !((tod1.hour > tod2.hour) 
            || ((tod1.hour == tod2.hour) 
                && (tod1.minute > tod2.minute))
}


int freetime (struct event schedule[], int n, int hour, int min)
{
    struct tod test_time;
    struct tod time1;
    struct tod time2;
    int i;
    int result = 1;

    test_time.hour = hour;
    test_time.minute = min;

    //handle edge cases
    if (tod_before(test_time, schedule[0].start) || tod_before(schedule[n-1].end, test_time)
    {return 1;}

    //handle general case
    for (i=0; i<n-1; i++)
    {
        time1 = schedule[i].end;
        time2 = schedule[i+1].start;

        if (tod_before(time1, time) && tod_before(time, time2))
        {return 1;}
    }
    //if we get to here, then time wasn't found in a break
    return 0;
}

This is assuming that each event is in order of course.

Sign up to request clarification or add additional context in comments.

1 Comment

Just fixed a bug that would cause it to return 0 when before or after all events. (and a range fix)
1

Replace

struct event schedule[] = {{{9,45},{9,55}},{{13,0},
        {14,20}},{{15,0},{16,30}}};

by

struct event schedule[] =
{
    {9,45},
    {9,55},
    {13,0},
    {14,20},
    {15,0},
    {16,30}
} ;

2 Comments

I can't change it since it is the one that will be used to test the code.
Then the struct event schedule[] declaration is wrong. struct event schedule[] is a one dimensional array of struct event. So it makes no sense to initialize it the way you do.
0

Here the basic problem is that your structure contains only two variables and you have an array of that kind of structure. So

struct event schedule[] = {{{9,45},{9,55}},{{13,0},{14,20}},{{15,0},{16,30}}};

will have a size of 3. And your variables of schedule, start & end is initialized with first in the sets i.e 9,9 13,14 15,16 only.In this case the schedule array will have 3 elements

 schedule[] = {{9,45},{9,55},{13,0}, {14,20},{15,0},{16,30}};

will initialize the start & end variables with 9,15 9,55 13,0 and so on. In this case the schedule array will have 6 elements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.