The Whole Month from a Single Day

Months have a pattern: All months feature weeks of seven days. The month can start on any day of the week. And months have a varying number of total days, 28, 29, 30, or 31. Given this data, you can display the days and weeks of any month when given only a specific date and the day of the week upon which it falls.

For example, this month is December. It has 31 days, spread across several weeks of 7 days each. The 25th of the month, Christmas, falls on a Tuesday this year. Given this data, you can accurately reconstruct the entire month:

Sun Mon Tue Wed Thu Fri Sat
                          1 
  2   3   4   5   6   7   8 
  9  10  11  12  13  14  15 
 16  17  18  19  20  21  22 
 23  24  25  26  27  28  29 
 30  31

Your task for this month’s Exercise is to write a function, month(). The function accepts two arguments, day and weekday: day is the day of the month, such as 25 for the 25th, and weekday is an integer value 0 through 6 representing Sunday through Saturday.

Here is a skeleton to help get you started, along with a few arguments you need to process to complete the Exercise:

#include <stdio.h>

enum { SUN, MON, TUE, WED, THU, FRI, SAT };

void month(int day, int weekday)
{

}

int main()
{
    month(15,WED);
    month(9,MON);
    month(23,SUN);

    return(0);
}

The main() function contains three statements that the month() function must process and output in the calendar-like format shown earlier. For simplicity’s sake, assume that each month has only 30 days.

Please try this Exercise on your own before you check out my solution.

Leave a Reply