Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
sol1.c File Reference

Problem 19 solution More...

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

Functions

char get_month_days (short month)
 
char is_leap_year (short year)
 
int main (int argc, char **argv)
 

Detailed Description

Problem 19 solution

Author
Krishna Vedala

Function Documentation

◆ get_month_days()

char get_month_days ( short  month)

returns number of days in a month. Month is identified by an integer -

0 = Jan and 11 = December

For February, adjust for leap year outside the function.

15 {
16  if (month == 1) /* February has 28 days. Adjust leap year in the loop */
17  return 28;
18  else if (month <= 6) /* odd months till July have 30 days - Jan = 0 (even)*/
19  {
20  if (month & 0x01)
21  return 30;
22  else
23  return 31;
24  }
25  else if (month >= 7) /* odd months after July have 31 days*/
26  {
27  if (month & 0x01)
28  return 31;
29  else
30  return 30;
31  }
32  /* should never reach here! */
33  perror("Should never have reached this point!\n");
34  return -1;
35 }

◆ is_leap_year()

char is_leap_year ( short  year)

return 1 if input year is a leap year otherwise, return 0

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

Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6 respectively Jan 1 1901 was a Tuesday

These two for-loops count the start of day for the next month. Hence, we have to skip the last December count

Main Algorithm: every week has 7 days hence, the start of next day would be modulo 7 add to this, the current start date and ensure the result is still modulo 7!

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