{"id":522,"date":"2014-01-25T00:01:28","date_gmt":"2014-01-25T08:01:28","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=522"},"modified":"2021-12-25T07:50:19","modified_gmt":"2021-12-25T15:50:19","slug":"the-month-program-phase-iv","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=522","title":{"rendered":"The Month Program, Phase IV"},"content":{"rendered":"<p>Last week&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=456\">Lesson<\/a> demonstrates a pretty decent calendar program. It works for every month of the year that has 31 days. Obviously that&#8217;s not every month, so more fine tuning must be done, especially to deal with the variable number of days in February.<br \/>\n<!--more--><br \/>\nDates and times are two things that can utterly infuriate a programmer. Unlike so much other data, the calendar just isn&#8217;t logical: A year has 365 days 75 percent of the time. Eleven of the 12 months feature discrete days. February fluctuates between 28 and 29 days. Leap years are every four years, unless the year is divisible by 100 but not 400.<\/p>\n<p>Got it?<\/p>\n<p>To make a calendar program work, you need to craft a function that properly calculates the number of days in February for a given year. On the positive side, rules exist for determining that calculation, so your task is to code those rules. Here are the rules:<\/p>\n<ul>\n<li>If the year is divisible by 4, it&#8217;s a leap year. February has 29 days. Otherwise, February has 28 days.<\/li>\n<li>If the leap year is divisible by 100, then it&#8217;s not a leap year and February has 28 days, unless:<\/li>\n<li>If the leap year is divisible by both 100 and 400, then February has 29 days.<\/li>\n<\/ul>\n<p>There. Cinchy.<\/p>\n<p>The following code contains the <em>february()<\/em> function. It&#8217;s passed a year value <code>y<\/code> and then it uses way too many modulus calculations to return either the value 28 for non-leap years or 29 for a leap year.<\/p>\n<p>The rest of the code simply plows through the months of the current year, displaying their discrete number of days.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint february(int y);\r\n\r\nint main()\r\n{\r\n\tchar *months[12] = {\r\n\t\t\"January\", \"February\", \"March\", \"April\",\r\n\t\t\"May\", \"June\", \"July\", \"August\",\r\n\t\t\"September\", \"October\", \"November\", \"December\" };\r\n\tint month_length[] = {\r\n\t\t31, 28, 31, 30, 31, 30,\r\n\t\t31, 31, 30, 31, 30, 31 };\r\n\ttime_t tictoc;\r\n\tstruct tm *rightnow;\r\n\tint x,year;\r\n\r\n\ttime(&amp;tictoc);\r\n\trightnow = localtime(&amp;tictoc);\r\n\tyear = rightnow-&gt;tm_year + 1900;\r\n\r\n\tmonth_length[1] = february(year);\r\n\r\n\tprintf(\"For %d:\\n\",year);\r\n\tfor(x=0;x&lt;12;x++)\r\n\t\tprintf(\"%s has %d days\\n\",\r\n\t\t\tmonths[x],\r\n\t\t\tmonth_length[x]);\r\n\r\n\treturn(0);\r\n}\r\n\r\nint february(int y)\r\n{\r\n\tif( y % 4 )\r\n\t\treturn(28);\r\n\tif( !(y % 100))\r\n\t{\r\n\t\tif( y % 400 )\r\n\t\t\treturn(28);\r\n\t\telse\r\n\t\t\treturn(29);\r\n\t}\r\n\treturn(29);\r\n}<\/pre>\n<p>Here&#8217;s sample output, which is relevant to the current year, 2014:<\/p>\n<pre><code>For 2014:\r\nJanuary has 31 days\r\nFebruary has 28 days\r\nMarch has 31 days\r\nApril has 30 days\r\nMay has 31 days\r\nJune has 30 days\r\nJuly has 31 days\r\nAugust has 31 days\r\nSeptember has 30 days\r\nOctober has 31 days\r\nNovember has 30 days\r\nDecember has 31 days<\/code><\/pre>\n<p>To incorporate this code into a calendar program, I&#8217;ve made some modifications. Specifically, you don&#8217;t really need a <em>february()<\/em> function for a calendar program as much as you need a function that returns the number of days in any month for a given year. That function, <em>daysInMonth()<\/em>, is demonstrated in the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint daysInMonth(int month,int year);\r\n\r\nint main()\r\n{\r\n\tchar *months[12] = {\r\n\t\t\"January\", \"February\", \"March\", \"April\",\r\n\t\t\"May\", \"June\", \"July\", \"August\",\r\n\t\t\"September\", \"October\", \"November\", \"December\" };\r\n\ttime_t tictoc;\r\n\tstruct tm *rightnow;\r\n\tint x,year;\r\n\r\n\ttime(&amp;tictoc);\r\n\trightnow = localtime(&amp;tictoc);\r\n\tyear = rightnow-&gt;tm_year + 1900;\r\n\r\n\tprintf(\"For %d:\\n\",year);\r\n\tfor(x=0;x&lt;12;x++)\r\n\t\tprintf(\"%s has %d days\\n\",\r\n\t\t\tmonths[x],\r\n\t\t\tdaysInMonth(x,year));\r\n\r\n\treturn(0);\r\n}\r\n\r\nint daysInMonth(int month,int year)\r\n{\r\n\tint month_length[] = {\r\n\t\t31, 28, 31, 30, 31, 30,\r\n\t\t31, 31, 30, 31, 30, 31 };\r\n\r\n\tif(month != 1)\r\n\t\treturn(month_length[month]);\r\n\r\n\tif( year % 4 )\r\n\t\treturn(28);\r\n\tif( !(year % 100))\r\n\t{\r\n\t\tif( year % 400 )\r\n\t\t\treturn(28);\r\n\t\telse\r\n\t\t\treturn(29);\r\n\t}\r\n\treturn(29);\r\n}<\/pre>\n<p>The <em>daysInMonth()<\/em> function eats two arguments: The <code>month<\/code> and <code>year<\/code>. The <code>month<\/code> is value from 0 through 11 for January through December. This function works it way into the calendar program, supplying the proper number of days of the month for whichever month is being displayed. You&#8217;ll see how that all wraps up with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=529\">next week&#8217;s final installment for this series of Lessons<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The number of days in February can change, which is bad news. The good news is that an algorithm can be written to deal with the differences. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=522\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-522","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=522"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":5125,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions\/5125"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}