{"id":2493,"date":"2017-05-08T00:01:53","date_gmt":"2017-05-08T07:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2493"},"modified":"2017-05-06T09:12:30","modified_gmt":"2017-05-06T16:12:30","slug":"day-of-the-year-calculation-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2493","title":{"rendered":"Day-of-the-Year Calculation &#8211; Solution"},"content":{"rendered":"<p>The solution for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2479\">this month&#8217;s Exercise<\/a> involves two steps. The first is to generate a random value from 1 to 365 (inclusive) as a day-of-the-year value. The second is to determine upon which month and day that value falls. That part may offer more difficulty than you anticipate.<br \/>\n<!--more--><br \/>\nTo generate the random value, I used this code:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* fetch random day of the year *\/<\/span>\r\n    srand((unsigned)time(NULL));\r\n    random_day = rand() % 365 + 1;<\/pre>\n<p>The <em>rand()<\/em> function returns an <em>int<\/em> value. That value is sliced down to 0 through 364 via the <code>%<\/code> (modulo) operator, then one is added to set the range from 1 to 365.<\/p>\n<p>The second step is to calculate the current month, which requires a few sub-steps.<\/p>\n<p>To reveal on which month a day-of-the-year falls, I created a <code>month[]<\/code> <em>int<\/em> array with the day values for 2017:<\/p>\n<pre class=\"screen\">\r\n    int month[12] = {\r\n        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31\r\n    };<\/pre>\n<p>I used a <em>for<\/em> loop to process the array, accumulating days in variable <code>d<\/code>. When the value of <code>d<\/code> is greater than the <code>random_day<\/code>, the current month is determined and saved in variable <code>m<\/code>:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* calculate month *\/<\/span>\r\n    d = 0;\r\n    for(x=0;x&lt;12;x++)\r\n    {\r\n        d += month[x];\r\n        if( d &gt; random_day)\r\n            break;\r\n    }\r\n    m = x;  <span class=\"comments\">\/* m is the month *\/<\/span><\/pre>\n<p>Variable <code>m<\/code> holds the month value for an array &mdash; an element number, used later in the code to display the month name.<\/p>\n<p>At this point, variable <code>d<\/code> contains the number of days in the months up to and including the day-of-the-year. So the current (element <code>x<\/code>) month&#8217;s number of days must be shaved off:<\/p>\n<pre class=\"screen\">\r\n    d -= month[x];\r\n    day = random_day-d;<\/pre>\n<p>The day of the month is calculated from the difference between <code>d<\/code> and <code>random_day<\/code> and saved in variable <code>day<\/code>. The result should result in the day of the month.<\/p>\n<p><em>Should<\/em>.<\/p>\n<p>A problem crops up, which I didn&#8217;t discover until I ran my solution a few times, when the result of <code>random_day-d<\/code> is zero. That result occurs on the last day of the month. So the final part of the code tests for the zero condition and, if true, assigns the value <code>day<\/code> to the number of days in the current month:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* deal with last day of the month *\/<\/span>\r\n    if( day == 0 )\r\n    {\r\n        m--;\r\n        day = month[m];\r\n    }<\/pre>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/05\/05exercise.c\">Click here<\/a> to view my entire solution.<\/p>\n<p>Here are some sample runs:<\/p>\n<pre><code>\r\nDay 49 of 2017 is February 18\r\n\r\nDay 100 of 2017 is April 10\r\n\r\nDay 151 of 2017 is May 31\r\n\r\nDay 219 of 2017 is August 7\r\n\r\nDay 270 of 2017 is September 27\r\n<\/code><\/pre>\n<p>Your solution may differ from mine, but if the results are good, then everything is okee-doke.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The solution for this month&#8217;s Exercise involves two steps. The first is to generate a random value from 1 to 365 (inclusive) as a day-of-the-year value. The second is to determine upon which month and day that value falls. That &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2493\">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-2493","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\/2493","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=2493"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2493\/revisions"}],"predecessor-version":[{"id":2509,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2493\/revisions\/2509"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}