-
Notifications
You must be signed in to change notification settings - Fork 0
/
019.c
71 lines (56 loc) · 1.43 KB
/
019.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// How many Sundays fell on the first of the month during the twentieth century?
#include <stdio.h>
struct date {
int year;
int month;
int day;
};
int main () {
void incrementDate(struct date *d);
void incrementDayOfWeek(int *i);
char day_strs[7][10] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
struct date d = {1901, 1, 1};
int sunday_on_first_count = 0;
// 0 = Monday, and increments every day (6 = Sunday)
int day_of_week = 1;
while (d.year < 2001) {
if (day_of_week == 6 /* sunday */ && d.day == 1) {
sunday_on_first_count++;
}
incrementDate(&d);
incrementDayOfWeek(&day_of_week);
}
printf("%i/%i/%i is a %s\n", d.year, d.month, d.day, day_strs[day_of_week]);
printf("Number of Sundays: %i\n", sunday_on_first_count);
return 0;
}
void incrementDate(struct date *d) {
// Last day of month (index 0 is null)
int last_day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// Check for leap year
if (d->month == 2) {
if ( (d->year % 4 == 0) && (d->year % 100 != 0) ||
(d->year % 400 == 0) ) {
last_day[2] = 29;
}
}
if (d->day == last_day[d->month]) {
if (d->month == 12) {
d->year++;
d->month = 1;
} else {
d->month++;
}
d->day = 1;
return;
}
d->day++;
}
void incrementDayOfWeek(int *i) {
if (*i == 6) {
*i = 0;
} else {
*i += 1;
}
}