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

C language problem is below, would anyone be able to provide a working solution

ID: 3860382 • Letter: C

Question

C language problem is below, would anyone be able to provide a working solution please?

Input n (1<=n<=10000) and then followed by n lines. Each line corresponds to a valid date, consisting of one string ("January", "February", ..., or "December"), one integer between 1 and 31, and one two digit integer representing the year (from 90 to 99, and then from 00 to 12). You do not have to worry about date validation. All dates in the input are valid dates. Please use structures to store the dates. Please use malloc to dynamically allocate just enough space for n structures.

You are asked to sort the dates chronologically using the built-in qsort function. Please output the sorted list, one date per line, from most recent date to oldest. Please also use the built-in bsearch function to allow for a user query to check whether a specific date is in the list, and output either "Yes" or "No".

Input

• n, the number of dates to sort,

• followed by n dates (i.e January 18 01 or June 7 98

• followed by a user query in format day month year (e.g. “1 1 00” or “31 3 68”). (Note this is a different format as the rest of the dates are presented in)

Output

• Sorted list of dates,

• followed by “Yes” or “No” to indicate whether the query date input by the user (e.g. 1 1 00 day month year) is in the list.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int isSameDecade(int year1, int year2)
{
return ((year1 >= 90 && year1 <= 99 && year2 >= 90 && year2 <= 99) ||
(year1 >= 0 && year1 <= 12 && year2 >= 0 && year2 <= 12 ));
}

int compInt(int a, int b)
{
if (a > b)
{
return 1;
}
else
{
if (a < b)
{
return -1;
}
else
{
return 0;
}
}
}

int cmpfuncInc (const void * a, const void * b)
{
struct date *date1 = (struct date *)a;
struct date *date2 = (struct date *)b;

if (date1->year == date2->year)
{

if (compInt(date1->month, date2->month) == 0)
{
return compInt(date1->day, date2->day);
}
else
{
return compInt(date1->month, date2->month);
}
}

if (isSameDecade(date1->year, date2->year))
{
return compInt(date1->year, date2->year);
}
else
{
if (date1->year <= 12)
{
return 1;
}
else
{
return -1;
}
}
}

int cmpfuncDec (const void * a, const void * b)
{
int comp = cmpfuncInc (a, b);
comp = -1*comp;
return comp;
}

int getMonth(char *month)
{
if (strcmp(month, "January") == 0)
{
return 1;
}
if (strcmp(month, "February") == 0)
{
return 2;
}
if (strcmp(month, "March") == 0)
{
return 3;
}
if (strcmp(month, "April") == 0)
{
return 4;
}
if (strcmp(month, "May") == 0)
{
return 5;
}
if (strcmp(month, "June") == 0)
{
return 6;
}
if (strcmp(month, "July") == 0)
{
return 7;
}
if (strcmp(month, "August") == 0)
{
return 8;
}
if (strcmp(month, "September") == 0)
{
return 9;
}
if (strcmp(month, "October") == 0)
{
return 10;
}
if (strcmp(month, "November") == 0)
{
return 11;
}
if (strcmp(month, "December") == 0)
{
return 12;
}
}

char *getMonthString(int month)
{
switch(month)
{
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
}
}

int main()
{
int n;
scanf("%d", &n);
struct date *dates = malloc (n*sizeof (struct date));

int i;
for (i = 0; i < n; i++)
{
char month[10];
int day, year;
scanf("%s%d%d", month, &day, &year);
dates[i].month = getMonth(month);
dates[i].day = day;
dates[i].year = year;
}

qsort(dates, n, sizeof(struct date), cmpfuncDec);

printf("After sorting ");
for(i = 0; i < n; i++)
{
char *month = getMonthString(dates[i].month);
printf("%s %d %d ", month, dates[i].day, dates[i].year);
}

int day, month, year;
scanf("%d%d%d", &day, &month, &year);

struct date searchedDate;
searchedDate.month = month;
searchedDate.year = year;
searchedDate.day = day;

int *result = bsearch(&searchedDate, dates, n , sizeof(struct date), cmpfuncDec);
if (result!=NULL)
printf ("YES ");
else
printf ("NO ");
return 0;
}