{"id":3134,"date":"2018-06-08T00:01:47","date_gmt":"2018-06-08T07:01:47","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3134"},"modified":"2018-06-02T10:37:54","modified_gmt":"2018-06-02T17:37:54","slug":"the-leap-year-function-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3134","title":{"rendered":"The Leap Year Function &#8211; Solution"},"content":{"rendered":"<p>I&#8217;ve tackled the leap year program on this blog before. My <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=414\">Month Program series<\/a> addressed the issue specifically. That code offers the <em>february()<\/em> function, which returns 28 or 29, depending on whether the current year is the leap year.<br \/>\n<!--more--><br \/>\nHere is the <em>february()<\/em> function:<\/p>\n<pre class=\"screen\">\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>The <code>y<\/code> argument is a year value, but this function has a flaw: If <code>y<\/code> equals 2000, the function returns 28 days. But if <code>y<\/code> equals 1900, it also returns 28 days. Effectively, the function always returns 28 days because both 100 and 400 are evenly-divisible by 4, the second block of <em>if<\/em> statements is never executed.<\/p>\n<p>Yes, sometimes my code is wrong.<\/p>\n<p>The solution for the <em>february()<\/em> function is easy: Move the first <em>if<\/em> block below the second. That&#8217;s pretty much how I handled the solution to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3111\">this month&#8217;s Exercise<\/a> with the <em>leap_year()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint leap_year(int year)\r\n{\r\n    <span class=\"comments\">\/* if the year is divisible by both 100 and\r\n       400, it's a leap year *\/<\/span>\r\n    if( (year%400)==0 )\r\n        return(1);\r\n    <span class=\"comments\">\/* if the year is divisble by 100, it's not\r\n       a leap year *\/<\/span>\r\n    if( (year%100)==0 )\r\n        return(0);\r\n    <span class=\"comments\">\/* check for 4 year interval, which is redundant\r\n       here, but I'll do it anyway *\/<\/span>\r\n    if( (year%4) != 0 )\r\n        return(0);\r\n    <span class=\"comments\">\/* otherwise, it's a leap year *\/<\/span>\r\n    return(1);\r\n}\r\n\r\nint main()\r\n{\r\n    int y;\r\n\r\n    for(y=1584;y&lt;=2100;y+=4)\r\n    {\r\n        if(leap_year(y))\r\n            printf(\"%d is a leap year!\\n\",y);\r\n        else\r\n            printf(\"%d is not a leap year.\\n\",y);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The modulus expression <code>year%n<\/code> returns zero when <code>year<\/code> is evenly divisible by <code>n<\/code>. The ! (NOT) operator reverses this condition, so the result in the various <em>if<\/em> comparisons proves TRUE.<\/p>\n<p>The <em>leap_year()<\/em> function takes its argument and first checks to see if <code>year<\/code> is divisible by 400. If so, it&#8217;s a leap year and 1 (TRUE) is returned. I test only 400 here, and not cross-check 100, because all years divisible by 400 are also divisible by 100 (and 4 as well).<\/p>\n<p>Next, if <code>year<\/code> is divisible by 100, which also means it isn&#8217;t divisible by 400 (because that condition has been eliminated), the code returns 0 (FALSE).<\/p>\n<p>Finally, if <code>year<\/code> isn&#8217;t divisible by 4, it&#8217;s not a leap year. The comment states that this test is &#8220;redundant&#8221; because the <em>for<\/em> loop in the <em>main()<\/em> function skips every 4 years. So in this code, the <em>leap_year()<\/em> function&#8217;s <code>year%4<\/code> test is redundant, but when the function is used in other code, it serves as a valid test.<\/p>\n<p>The code&#8217;s output is long, but here are the highlights:<\/p>\n<pre><code>1700 is not a leap year.\r\n1800 is not a leap year.\r\n1900 is not a leap year.\r\n2100 is not a leap year.<\/code><\/pre>\n<p>Every other year displayed, from 1584 through 2099 (skipping every 4 years), is a leap year. If your solution generated similar results, congratulations!<\/p>\n<p>Now I must go back and fix my Month program . . .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve tackled the leap year program on this blog before. My Month Program series addressed the issue specifically. That code offers the february() function, which returns 28 or 29, depending on whether the current year is the leap year.<\/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-3134","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\/3134","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=3134"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3134\/revisions"}],"predecessor-version":[{"id":3151,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3134\/revisions\/3151"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}