50048. Count the days

I'm a slow walker, but I never walk backwards.

Task Description

Write a function count_day to determine the numbers of each day of the week in a particular month.

Given a year, the day of January 1st of that year, and a month, calculate the numbers of Sundays, Mondays, … , Saturdays in that given month. You need to place the results into an array $\textit{results}\lbrack ]$. We use a number from 0 to 6 to represent the day of the week from Sunday to Saturday. For example, 0 means Sunday and 5 means Friday.

Let us illustrate the task with an example. If we are given the year of 2016, February, and the fact that January 1st is a Friday, we need to determine the numbers of each day of the week for February 2016. The answers are that there are 4 Sundays, 5 Mondays, 4 Tuesdays, 4 Wednesdays, 4 Thursdays, 4 Fridays, 4 Saturdays in February 2016. Your program should put the following results in the array $results\lbrack ]$ as follows.

results[0] results[1] results[2] results[3] results[4] results[5] results[6]
4 5 4 4 4 4 4

Note that the given year could be a leap year. A year is a leap year if it is divisible by 400, or it is divisible by 4 but not by 100.

The prototype of count_day is as follows:

1
void count_day(int year, int day, int month, int results[7]);

The main program is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>
#include "count_day.h"
 
int main(){
    int year, day, month, results[7] = {0}, i;
 
    while(scanf("%d %d %d", &year, &day, &month) == 3){
        for(i = 0; i < 7; i++)
            results[i] = 0;
        count_day(year, day, month, results);
        for(i = 0; i < 6; i++)
            printf("%d ", results[i]);
        printf("%d\n", results[i]);
    }
    return 0;
}

The header file count_day.h is as follows:

1
2
3
4
#ifndef COUNTDAY_H_INCLUDED
#define COUNTDAY_H_INCLUDED
void count_day(int, int, int, int [7]);
#endif

Input Format

The input contains multiple test cases. Each line of input contains three integers $year, day, month$ ($ 0 \lt year \lt 10000$, $0 \leq day \leq 6$, $1 \leq month \leq 12$).

Output Format

There are multiple lines in the output. Each line contains 7 integers that represent the numbers of each day of the week in the particular month.

Sample Input

1
2
2016 5 2
2015 4 2

Sample Output

1
2
4 5 4 4 4 4 4
4 4 4 4 4 4 4

Compile

gcc -std=c99 -O2 -c main.c -lm
gcc -std=c99 -O2 -c count_day.c -lm
gcc -std=c99 -O2 count_day.o main.o -lm

Discussion