Beginning Day of a Month

  1. Since you know how to calculate what day January 1st is, you can go on to calculate the 1st day of each month according to the following algorithm.
  2. Let days[i] denote the number of days in month m. For example, days[4] == 30 and days[12] == 31. As for days[2], it can be 28 or 29, depending on whether that is a leap year.
  3. Suppose January 1st is on Day d, then the first day of month m can be calculated by
    
    for (i=1; i<m; i++)
        d = ( (d + days) % 7 );
    
    
  4. Test the above code with some examples for m = 1, 2, 3, 4, .... You will see how it accumulates to the beginning day of a month.
  5. Although we shall not learn how to work with C++ arrays until Chapter 4, you can calculate that with a switch statement:
    
    switch (i)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            days = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days = 30;
            break;
    }
    
    

The program may run as follows:

2016 3
2