{"id":5190,"date":"2022-02-08T00:01:56","date_gmt":"2022-02-08T08:01:56","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5190"},"modified":"2022-02-05T10:09:53","modified_gmt":"2022-02-05T18:09:53","slug":"the-microwave-problem-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5190","title":{"rendered":"The Microwave Problem &#8211; Solution"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/02\/my_smart_microwave.jpg\" alt=\"\" width=\"350\" height=\"179\" class=\"alignnone size-full wp-image-5188\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/02\/my_smart_microwave.jpg 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/02\/my_smart_microwave-300x153.jpg 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><br \/>\nThe challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5183\">this month&#8217;s Exercise<\/a> is to write a microwave oven input routine: The user types in a given number of seconds and your program translates the value into the proper number of hours, minutes, and seconds. This type of problem may sound familiar.<br \/>\n<!--more--><br \/>\nBack in 2014, I presented <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1083\">an Exercise<\/a> about making change: An amount of cents is presented and the solution is to calculate how many quarters, dimes, nickels, and pennies are required to equal that amount of change.<\/p>\n<p>My approach for the making change Exercise was an <em>if-else if-else<\/em> contraption that peeled away quarters, dimes, nickels, and eventually pennies from the total, tallying the result. When I presented this solution online, someone called my solution dumb. Perhaps. Regardless, for this month&#8217;s similar problem I refrained from using any decision structures. Nope, my solution is all math:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_02-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    <span class=\"comments\">\/* constants to set seconds in a minute and seconds in an hour *\/<\/span>\r\n    const int minutes = 60;\r\n    const int hours = 60 * minutes;\r\n    int h,m,s;\r\n\r\n    <span class=\"comments\">\/* accept input *\/<\/span>\r\n    printf(\"Enter time in seconds: \");\r\n    scanf(\"%d\",&amp;s);\r\n\r\n    <span class=\"comments\">\/* trim down the chunks *\/<\/span>\r\n    h = s\/hours;        <span class=\"comments\">\/* hours rounded *\/<\/span>\r\n    s -= h*hours;       <span class=\"comments\">\/* remaining seconds after removing hours (h) *\/<\/span>\r\n    m = s\/minutes;      <span class=\"comments\">\/* minutes rounded *\/<\/span>\r\n    s -= m*minutes;     <span class=\"comments\">\/* remaining seconds after removing minutes (m) *\/<\/span>\r\n\r\n    printf(\"That's %d:%02d:%02d\\n\",h,m,s);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Lines 6 and 7 set constants for <code>minutes<\/code> and <code>hours<\/code>: 60 seconds are in a minute and 60 minutes are in an hour. Lines 11 and 12 prompt for and receive input.<\/p>\n<p>The math starts at Line 15:<\/p>\n<p><code>h = s\/hours;<\/code><\/p>\n<p>The number of hours <code>h<\/code> is calculated as the input value, <code>s<\/code> (seconds) divided by <code>hours<\/code>, or 3600. As <em>int<\/em> variables are involved, the result is rounded automatically. The result is the number of hours in the given number of seconds &mdash; even if it&#8217;s zero.<\/p>\n<p>At Line 16, the number of hours <code>h<\/code> in seconds is subtracted from the original number of seconds <code>s<\/code>:<\/p>\n<p><code>s -= h*hours;<\/code><\/p>\n<p>The remainder is the number of minutes and seconds left to separate.<\/p>\n<p>At Line 17, math is echoed from Line 15, but to calculate the number of minutes:<\/p>\n<p><code>m = s\/minutes;<\/code><\/p>\n<p>The value in <code>s<\/code> is divided by constant <code>minutes<\/code>. The result, the number of minutes remaining, is stored in variable <code>m<\/code>.<\/p>\n<p>Line 18 subtracts the minutes value (total seconds in the minutes calculated) from variable <code>s<\/code>, leaving the number of seconds remaining:<\/p>\n<p><code>s -= m*minutes;<\/code><\/p>\n<p>Line 20 outputs the results: hours, minutes, and seconds stored in variables <code>h<\/code>, <code>m<\/code>, and <code>s<\/code>.<\/p>\n<p>No decisions are necessary in this code as the math removes a larger and larger chunk from the original value. Variable <code>s<\/code> always holds seconds. After hours (chunks of 3600 seconds) are pulled away, and minutes (chunks of 60 seconds) are removed, what&#8217;s left are seconds.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter time in seconds: 241<br \/>\nThat's 0:04:01<\/code><\/p>\n<p>I hope your solution met with success. Remember, many different solutions are possible so your&#8217;s doesn&#8217;t have to match mine. As long as the program runs and the output is correct, you&#8217;ve passed the Exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to write a microwave oven input routine: The user types in a given number of seconds and your program translates the value into the proper number of hours, minutes, and seconds. This type &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5190\">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-5190","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\/5190","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=5190"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5190\/revisions"}],"predecessor-version":[{"id":5205,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5190\/revisions\/5205"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}