C

Asked • 09/23/24

Defining and using functions and structures

You have been asked to recreate some C source code that was lost. The only remaining source code is the following:


int main()
{
char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int daysInMonth[] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int year, dayOfYear;
struct MonthAndDay result;
printf("Enter the year and the day in the year: ");
scanf("%d %d", &year, &dayOfYear);
if (dayOfYear < 0)
{
printf("Day cannot be negative");
}
else
{
if (isLeapYear(year))
{
daysInMonth[1] = 29;
}
result = getMonthAndDay(year, dayOfYear, 12, months, daysInMonth);
if (result.month == NULL)
{
printf("Day of the year not found");
}
else
{
printf("%s %d, %d", result.month, result.day, year);
}
}

return 0;
}


The executable can still be run and you are told that the following output should help you recreate the missing source code:


Enter the year and the day in the year: 1900 60
Mar 1, 1900

Enter the year and the day in the year: 2000 60
Feb 29, 2000

Enter the year and the day in the year: 2023 60
Mar 1, 2023

Enter the year and the day in the year: 2023 365
Dec 31, 2023

Enter the year and the day in the year: 2023 366
Day of the year not found

Enter the year and the day in the year: 2024 31
Jan 31, 2024

Enter the year and the day in the year: 2024 60
Feb 29, 2024

Enter the year and the day in the year: 2024 366
Dec 31, 2024

Enter the year and the day in the year: 2024 367
Day of the year not found


Indicate what code should be added to source code shown above that would make it compile and run to produce the same results as above.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.