{"id":3741,"date":"2019-09-08T00:01:32","date_gmt":"2019-09-08T07:01:32","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3741"},"modified":"2021-05-10T20:38:53","modified_gmt":"2021-05-11T03:38:53","slug":"next-tuesday-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3741","title":{"rendered":"Next Tuesday &#8211; Solution"},"content":{"rendered":"<p>One way to discover next Tuesday&#8217;s date is just to wait. You could code that solution in C, which would be silly, but I hope that you instead chose to use some time functions to derive your answer.<br \/>\n<!--more--><br \/>\nThe first step to discovering next Tuesday&#8217;s date is to obtain the current date: Use the <em>time()<\/em> function to fetch a <em>time_t<\/em> value.<\/p>\n<p>Next, pass the current time, the <em>time_t<\/em> value, to the <em>localtime()<\/em> function. It returns a <em>tm<\/em> structure with details about the current date. Specifically you want the day of the week and day of the month, the <em>tm<\/em> structure&#8217;s <em>tm_wday<\/em> and <em>tm_mday<\/em> members, respectively.<\/p>\n<p>The <em>tm<\/em> structure holds Tuesday as value 2 in the <em>tm_wday<\/em> member. The algorithm I use in my solution subtracts 2 (Tuesday) from the current <em>tm_wday<\/em> value. The result is the number of days until next Tuesday. If the result is negative, add 7 to the result to obtain the proper value.<\/p>\n<p>Finally, you must compare the value returned (the number of days until next Tuesday) with the number of days in the current month. If next Tuesday&#8217;s date is greater than the number of days in the current month, you must subtract the number of days in the current month to obtain an accurate next-Tuesday value.<\/p>\n<p>After all this calculating and scheming, the result is the day of the month for next Tuesday. The following code implements such insanity:<\/p>\n<h3>2019_09-Exercise.c<\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nvoid ordinal(int d);\r\n\r\nint main()\r\n{\r\n    time_t now;\r\n    struct tm *today;\r\n    int tuesday;\r\n    char *days[] = {\r\n        \"Sunday\",\r\n        \"Monday\",\r\n        \"Tuesday\",\r\n        \"Wednesday\",\r\n        \"Thursday\",\r\n        \"Friday\",\r\n        \"Saturday\"\r\n    };\r\n    <span class=\"comments\">\/* this array assumes February to have 28 days *\/<\/span>\r\n    int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r\n\r\n    <span class=\"comments\">\/* obtain the current time *\/<\/span>\r\n    time(&amp;now);\r\n    <span class=\"comments\">\/* translate into date and time fields *\/<\/span>\r\n    today = localtime(&amp;now);\r\n\r\n    <span class=\"comments\">\/* output today's day and date *\/<\/span>\r\n    printf(\"Today is %s the %d\",\r\n            days[today-&gt;tm_wday],\r\n            today-&gt;tm_mday\r\n          );\r\n    ordinal(today-&gt;tm_mday);\r\n\r\n    <span class=\"comments\">\/* calculate next Tuesday *\/<\/span>\r\n        <span class=\"comments\">\/* Tuesday is tm_mday value 2 *\/<\/span>\r\n    tuesday = (2 - today-&gt;tm_wday);\r\n    if( tuesday &lt; 1 )\r\n        tuesday+=7+today-&gt;tm_mday;\r\n    else\r\n        tuesday+=today-&gt;tm_mday;\r\n        <span class=\"comments\">\/* check for the end of the month *\/<\/span>\r\n    if( tuesday &gt; months[today-&gt;tm_mon] )\r\n        tuesday -= months[today-&gt;tm_mon];\r\n        <span class=\"comments\">\/* output the result *\/<\/span>\r\n    printf(\"Next Tuesday is the %d\",tuesday);\r\n    ordinal(tuesday);\r\n\r\n    return(0);\r\n}\r\n\r\n<span class=\"comments\">\/* handle ordinal output *\/<\/span>\r\nvoid ordinal(int d)\r\n{\r\n    if( d&gt;10 &amp;&amp; d&lt;14 )\r\n        puts(\"th\");\r\n    else\r\n    {\r\n        switch(d % 10 )\r\n        {\r\n            case 1:\r\n                puts(\"st\");\r\n                break;\r\n            case 2:\r\n                puts(\"nd\");\r\n                break;\r\n            case 3:\r\n                puts(\"rd\");\r\n                break;\r\n            default:\r\n                puts(\"th\");\r\n        }\r\n    }\r\n}<\/pre>\n<p>The code works through the steps outlined earlier in this post. Here&#8217;s a sample run, which I ran on Saturday, August 31:<\/p>\n<p><code>Today is Saturday the 31st<br \/>\nNext Tuesday is the 3rd<\/code><\/p>\n<p>I hope your solution also yields the correct, &#8220;next Tuesday&#8221; value.<\/p>\n<ul>\n<li>For more details on the <em>ordinal()<\/em> function, refer to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=76\">this Exercise<\/a> from 2013.<\/li>\n<li>If you desire to implement the proper day count for February, refer to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3134\">this Exercise Solution<\/a> from 2018.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One way to discover next Tuesday&#8217;s date is just to wait. You could code that solution in C, which would be silly, but I hope that you instead chose to use some time functions to derive your answer.<\/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-3741","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\/3741","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=3741"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3741\/revisions"}],"predecessor-version":[{"id":4772,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3741\/revisions\/4772"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}