{"id":3394,"date":"2018-12-08T00:01:00","date_gmt":"2018-12-08T08:01:00","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3394"},"modified":"2018-12-01T13:08:15","modified_gmt":"2018-12-01T21:08:15","slug":"the-whole-month-from-a-single-day-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3394","title":{"rendered":"The Whole Month from a Single Day &#8211; Solution"},"content":{"rendered":"<p>The key to creating a calendar is to know on which day of the week the first of the month falls. This datum can be extrapolated from any other day of the month, as long as you know on which day of the week it falls.<br \/>\n<!--more--><br \/>\nIf you look at a calendar, such as the one shown in Figure 1, you see how each day of the month has a corresponding day on the first week of the month. This pattern is consistent, so when the dates shift around, the first of the month shifts as well. The key to unlocking <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3382\">this month&#8217;s Exercise<\/a> is to calculate the relationship between each day of the month and the first day.<\/p>\n<div id=\"attachment_3399\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3399\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/11\/1208-figure1_month-300x242.png\" alt=\"\" width=\"300\" height=\"242\" class=\"size-medium wp-image-3399\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/11\/1208-figure1_month-300x242.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/11\/1208-figure1_month-372x300.png 372w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/11\/1208-figure1_month.png 500w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><p id=\"caption-attachment-3399\" class=\"wp-caption-text\">Figure 1. A typical month.<\/p><\/div>\n<p>To determine the first day of the month you need only two tidbits of information: the number of some other day of the month and the day of the week upon which it falls. These are the identical arguments to the <em>month()<\/em> function for the Exercise:<\/p>\n<p><code>void month(int day, int weekday)<\/code><\/p>\n<p>The weekdays are supplied as enumerated constants, values 0 through 6:<\/p>\n<p><code>enum { SUN, MON, TUE, WED, THU, FRI, SAT };<\/code><\/p>\n<p>Using the sample calendar from Figure 1, suppose you have that the 17th of the month is on a Friday (<code>FRI<\/code> or 6). Here&#8217;s the math I use to calculate the first:<\/p>\n<p><code>first = weekday - ( day % 7 ) + 1;<\/code><\/p>\n<p>Which works out as:<\/p>\n<p><code>first = 6 - ( 17 % 7 ) + 1;<br \/>\nfirst = 6 - ( 3 ) + 1;<br \/>\nfirst = 4;<\/code><\/p>\n<p>So the first of the month is on weekday 4, <code>WED<\/code> or Wednesday. And if you look at Figure 1, the answer is correct.<\/p>\n<p>If you&#8217;re given the 13th, which is a Monday, here&#8217;s the math:<\/p>\n<p><code>first = 1 - ( 13 % 7 ) + 1;<br \/>\nfirst = 1 - ( 5 ) + 1;<br \/>\nfirst = -3;<\/code><\/p>\n<p>The value -3 doesn&#8217;t translate into a day of the week that&#8217;s in range. Perhaps it&#8217;s Blurnsday, but until the Galactic Calendar Reform of 2622 arrives, more programming is necessary to determine upon which day of the week the first falls:<\/p>\n<p><code>if( first &lt; 0 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;first+=7;<\/code><\/p>\n<p>The result of -3 + 7 is 4, which is Wednesday, the proper day of the week upon with the first of the month falls.<\/p>\n<p>Once you know the first day of the month and its weekday, the month&#8217;s calendar can be output in a loop:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n<span class=\"comments\">\/* Display the month *\/<\/span>\r\n    puts(\"Sun Mon Tue Wed Thu Fri Sat\");\r\n    monthday = 1;\r\n    while(monthday &lt; 31)\r\n    {\r\n        <span class=\"comments\">\/* display a week *\/<\/span>\r\n        for(dow=0;dow&lt;7;dow++)\r\n        {\r\n            <span class=\"comments\">\/* don't display dates before the first *\/<\/span>\r\n            if(dow&lt;first &amp;&amp; monthday==1)\r\n            {\r\n                printf(\"    \");        <span class=\"comments\">\/* 4 spaces *\/<\/span>\r\n            }\r\n            else\r\n            {\r\n                printf(\" %2d \",monthday);\r\n                monthday++;\r\n                if(monthday &gt; 30)\r\n                    break;\r\n            }\r\n        }\r\n        putchar('\\n');\r\n    }<\/pre>\n<p>The outer <em>while<\/em> loop works by days of the month, which are fixed at 30 for this Exercise. The inner <em>for<\/em> loop displays each successive week of the month. Two <em>if<\/em> tests inside the <em>for<\/em> loop prevent days before the first and days after the 30th from being displayed.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/12exercise.c\">Click here<\/a> to view the full code for my solution.<\/p>\n<p>I hope your solution met with success. I assume other ways exist to populate a month full of dates based only a single day of the month and its day of the week. Your solution can be expanded to build a full-calendar app: Instead of using preset days and weekdays, use a time function to obtain the current date and then build your month. Or, if your ambitious, build an entire year!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The key to creating a calendar is to know on which day of the week the first of the month falls. This datum can be extrapolated from any other day of the month, as long as you know on which &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3394\">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":[5],"tags":[],"class_list":["post-3394","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3394","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=3394"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3394\/revisions"}],"predecessor-version":[{"id":3414,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3394\/revisions\/3414"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}