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

*Program is in C* Declare a structure called Date to store information for dates

ID: 3806675 • Letter: #

Question

*Program is in C*

Declare a structure called Date to store information for dates. Include day (int), month(int) and year(int) in the structure. Implement the following functions that use the structure Date. int before(struct Date d1, struct Date d2); int within30days(struct Date d1, struct Date d2); The function before returns 1 if dl is before d2 and 0 otherwise. The function within30days returns 1 if d1 and d2 are within 30 days of eachother and 0 otherwise. The parameters for this function need not be in order and d1 could be before d2 or after d2. You can assume that each month is 30 days for this function.

Explanation / Answer

#include <stdio.h>

struct Date{
   int day;
   int month;
   int year;
};

int before(struct Date d1,struct Date d2){
   if(d1.year < d2.year){
       return 1;
   }
   else if(d1.year > d2.year){
       return 0;
   }
   else{
       if(d1.month < d2.month){
           return 1;
       }
       else if(d1.month > d2.month){
           return 0;
       }
       else{
           if(d1.day < d2.day){
               return 1;
           }
           else if(d1.day >= d2.day){
               return 0;
           }  
       }
   }
}

int within30days(struct Date d1,struct Date d2){
   long d1Days,d2Days;
   d1Days = d1.year*12*30+d1.month*30+d1.day;
   d2Days = d2.year*12*30+d2.month*30+d2.day;
   if(d1Days>d2Days){
       if(d1Days<=d2Days+30)
           return 1;
   }
   else{
       if(d2Days<=d1Days+30)
           return 1;
   }
   return 0;
}

int main(void) {
   struct Date d1,d2;
   int a,b;

   printf("Enter d1.day:");
   scanf("%d",&d1.day);
   printf("Enter d1.month:");
   scanf("%d",&d1.month);
   printf("Enter d1.year:");
   scanf("%d",&d1.year);

   printf("Enter d2.day:");
   scanf("%d",&d2.day);
   printf("Enter d2.month:");
   scanf("%d",&d2.month);
   printf("Enter d2.year:");
   scanf("%d",&d2.year);

   a = before(d1,d2);
   printf("before() output: %d",a);
  
   b=within30days(d1,d2);
   printf("within30days() output: %d",b);

   return 0;
}