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

NOTE: For this assignment, DO NOT use arrays or other structured data types – yo

ID: 672174 • Letter: N

Question

NOTE: For this assignment, DO NOT use arrays or other structured data types – you do not need them. All you need are simple variables.

Specifically, your program should do the following:

1. Prompt the user to enter the numeric equivalent of a month, where 1 = January, 2 = February, ..., 12 = December. If the numeric value is invalid, print an error message and prompt the user again.

2. Prompt the user to enter the year, which should be an integer > 0. If the year is invalid, print an error message and prompt the user again.

3. If the month and year are valid, print the calendar for the month and year according to the format depicted in the sample output above.

Your program should contain at least the following three functions:

int length(int month, int year): return the length (number of days) of the given month and year. This function must take into account the fact that if year is a leap year, then February (month = 2) has 29 days instead of 28.

int day_of_week(int day, int month, int year): return the day of the week that the given day of the given month and year falls on. For this function, assume that the days of the week are encoded as follows: 0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday (see discussion below on Zeller’s congruence). For example, looking at the third run of the sample output above, we see that February 12, 2016 falls on a Friday. The function should therefore return the value 6.

void print_calendar(int start_day, int month_length): print the calendar for the month with month_length days and whose first day falls on start_day, where 0 ? start_day ? 6 encodes the days Saturday, Sunday, ..., Friday as described above.

For this assignment, assume the Gregorian calendar which is today’s internationally accepted civil calendar. It was introduced in 1582 in parts of Europe; it was adopted in 1752 in Great Britain and its colonies (including British America which later became the USA), and on various dates in other countries.

The lengths of months of the year follow an irregular pattern. There are many algorithms to calculate the day of the week that a date falls on. One such algorithm is Zeller’s congruence.

Explanation / Answer

#include <stdio.h>
int isLeapYear( int year );     
int leapYears( int year );      
int todayOf( int y, int m, int d);
long days( int y, int m, int d);
void calendar(int y, int m);     
int main(void)
{
int year,month;
printf("Enter the month and year: ");
scanf("%d %d", &month, &year);
calendar(year, month);
return 0;
}
int isLeapYear( int y )
{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}
int leapYears( int y )
{
return y/4 - y/100 + y/400;
}
int todayOf( int y, int m, int d)
static int DayOfMonth[] = { -1/*dummy*/,0,31,59,90,120,151,181,212,243,273,304,334};
return DayOfMonth[m] + d + ((m>2 && isLeapYear(y))? 1 : 0);
}
long days( int y, int m, int d)
{
int lastYear;
lastYear = y - 1;
return 365L * lastYear + leapYears(lastYear) + todayOf(y,m,d);
}
void calendar(int y, int m)
{
const char *NameOfMonth[] = { NULL/*dummp*/,"January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"};
char Week[] = "Su Mo Tu We Th Fr Sa";
int DayOfMonth[] ={ -1/*dummy*/,31,28,31,30,31,30,31,31,30,31,30,31 };
int weekOfTopDay;
int i,day;
weekOfTopDay = days(y, m, 1) % 7;
if(isLeapYear(y))
DayOfMonth[2] = 29;
printf("      %s %d %s ", NameOfMonth[m], y, Week);
for(i=0;i<weekOfTopDay;i++)
printf("   ");
for(i=weekOfTopDay,day=1;day <= DayOfMonth[m];i++,day++)
{
printf("%2d ",day);
if(i % 7 == 6)
printf(" ");
}
printf(" ");
}