Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C language Consider the following struct that represents time of day. struct tim

ID: 674092 • Letter: C

Question

C language

Consider the following struct that represents time of day.

struct timeofday {
  int hours; /* 1 through 12 */
  int minutes; /* 0 through 59 */
  int pm; /* 1 if PM, 0 if AM */
};
Write a definition of the compareTimes() function (declared below), which compares two times. To avoid copying, we pass a pointers, instead of structs. Return -1 if the first time is less than (earlier than) the second time; return 1 if the first time is greater than the second; return 0 if the times are equal .
int compareTimes(struct timeofday *t1, struct timeofday *t2);
NOTE: 12:00am is the earliest time (midnight). 12:00pm is noon. 11:59pm is the latest time.

Instructor Notes: If the AM/PM values are different, the other values don't matter. If the AM/PM values are the same, consider converting "hours and minutes" to "minutes past 12:00".

Explanation / Answer

#include <stdio.h>

struct timeofday{

int hours;

int minutes;

int pm;

};

void compareTimes(struct timeofday t1, struct timeofday t2, struct timeofday *diff);

int main()

{

    struct timeofday t1,t2,diff;

    printf("Enter start time: ");

    printf("Enter hours, minutes and pm respectively: ");

    scanf("%d%d%d",&t1.hours,&t1.minutes,&t1.pm);

    printf("Enter stop time: ");

    printf("Enter hours, minutes and pm respectively: ");

    scanf("%d%d%d",&t2.hours,&t2.minutes,&t2.pm);

    Difference(t1,t2,&diff);

    printf(" TIME DIFFERENCE: %d:%d:%d - ",t1.hours,t1.minutes,t1.pm);

    printf("%d:%d:%d ",t2.hours,t2.minutes,t2.pm);

    printf("= %d:%d:%d ",diff.hours,diff.minutes,diff.pm);

    return 0;

}

void Difference(struct timeofday t1, struct timeofday t2, struct timeofday *differ)

{

    if(t2.minutes>t1.minutes)

{

        --t1.hours;

        t1.minutes+=60;

    }

    differ->minutes=t1.minutes-t2.minutes;

    differ->hours=t1.hours-t2.hours;

}