Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol1.c File Reference

Problem 19 solution More...

#include <stdio.h>
Include dependency graph for sol1.c:

Functions

char get_month_days (short month)
 Function to get the number of days in a month. More...
 
char is_leap_year (short year)
 Check if input year is a leap year. More...
 
int main (int argc, char **argv)
 Main function. More...
 

Detailed Description

Problem 19 solution

Author
Krishna Vedala

Function Documentation

◆ get_month_days()

char get_month_days ( short  month)

Function to get the number of days in a month.

Parameters
monthmonth identified by an integer -

‍0 = Jan and 11 = December

Returns
number of days in given month
Note
For February, adjust for leap year outside the function.
16{
17 if (month == 1) /* February has 28 days. Adjust leap year in the loop */
18 return 28;
19 else if (month <= 6) /* odd months till July have 30 days - Jan = 0 (even)*/
20 {
21 if (month & 0x01)
22 return 30;
23 else
24 return 31;
25 }
26
27 // else if (month >= 7) /* odd months after July have 31 days*/
28
29 if (month & 0x01)
30 return 31;
31
32 return 30;
33}

◆ is_leap_year()

char is_leap_year ( short  year)

Check if input year is a leap year.

Parameters
yearyear to check
Returns
1 if input year is a leap year
0 if input year is not a leap year
42{
43 if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
44 return 1;
45
46 return 0;
47}

◆ main()

int main ( int  argc,
char **  argv 
)

Main function.

80{
81 int count_sundays = 0;
82 const short start_year = 1901;
83 const short end_year = 2000;
84
85 /*
86 * Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6
87 * respectively Jan 1 1901 was a Tuesday
88 */
89 char start_day = 2;
90
91 for (int year = start_year; year <= end_year; year++)
92 {
93 char is_leap = is_leap_year(year);
94 for (char month = 0; month < 12; month++)
95 {
96 /*
97 * These two for-loops count the start of day for the next month.
98 * Hence, we have to skip the last December count
99 */
100 if (year == end_year && month == 11)
101 continue;
102
103 int days = get_month_days(month);
104
105 if (is_leap && month == 1) /* for a leap year february, add a day */
106 days++;
107
108#ifdef DEBUG
109 if (year == end_year)
110 {
111 printf("Year: %d\t Month: %d\t Days: %d\t First of day: %s\n",
112 year, month, days, day_string(start_day));
113 }
114#endif
115
116 /* Main Algorithm:
117 * every week has 7 days hence, the start of next day would be
118 * modulo 7 add to this, the current start date and ensure the
119 * result is still modulo 7!
120 */
121 start_day = ((days % 7) + start_day) % 7;
122
123 /* If start-day is a Sunday, increment counter */
124 if (start_day == 0)
125 count_sundays++;
126 }
127 }
128
129 printf(
130 "Total number of Sundays that happened on the 1st of a month in the "
131 "last century: %d\n",
132 count_sundays);
133
134 return 0;
135}
char is_leap_year(short year)
Check if input year is a leap year.
Definition: sol1.c:41
char get_month_days(short month)
Function to get the number of days in a month.
Definition: sol1.c:15
Here is the call graph for this function: