{"id":4479,"date":"2020-12-05T00:01:42","date_gmt":"2020-12-05T08:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4479"},"modified":"2020-12-05T08:32:33","modified_gmt":"2020-12-05T16:32:33","slug":"my-chicken-mcnuggets-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4479","title":{"rendered":"My Chicken McNuggets Solution"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.mcdonalds.com\/is\/image\/content\/dam\/uk\/nfl\/nutrition\/nfl-product\/product\/products\/mcdonalds-20-Chicken-McNuggets-ShareBox.jpg\" alt=\"Box of 20 Chicken McNuggets, McDonalds\" \/><br \/>\nThe Chicken McNuggets problem, presented in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4470\">last week&#8217;s Lesson<\/a>, shows code that outputs the highest McNuggets number from 1 to 100. This value isn&#8217;t a combination of 6, 9, or 20, the number of the pieces offered in the McDonald&#8217;s Chicken McNuggets packages.<br \/>\n<!--more--><br \/>\nThe C language <a href=\"http:\/\/rosettacode.org\/wiki\/McNuggets_problem#C\" rel=\"noopener noreferrer\" target=\"_blank\">solution presented on Rosetta Code<\/a> works, but it uses the detested <em>goto<\/em> statement to cut through the nested loops. Obviously, nested loops are required to calculate the various permutations of 6, 9, and 20, but there must be some other way to code a solution without using <em>goto<\/em> statements.<\/p>\n<p>For my approach, I use nested loops but they&#8217;re coupled with a 100 element array, <code>nuggets[]<\/code>. This array is what keeps track of the McNugget numbers. It allows the nested loops to track the numbers by resetting values in the array. When the loops have run through all the permutations, a final loop reviews the array to find the highest non-McNuggets value.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_12_05-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_12_05-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x,i,s,n,t,max;\r\n    int nuggets[100];\r\n\r\n    <span class=\"comments\">\/* initialize array *\/<\/span>\r\n    for( x=0; x&lt;100; x++ )\r\n        nuggets[x] = 1;\r\n\r\n    <span class=\"comments\">\/* find the McNuggets number *\/<\/span>\r\n    i = 0;\r\n    while( i&lt;100 )\r\n    {\r\n        <span class=\"comments\">\/* loop through sixes first *\/<\/span>\r\n        for( s=0; s*6&lt;i; s++ )\r\n        {\r\n            if( s*6 == i )\r\n                nuggets[i] = 0;\r\n            <span class=\"comments\">\/* nines loop *\/<\/span>\r\n            for( n=0; n*9&lt;i; n++ )\r\n            {\r\n                if( s*6 + n*9 == i )\r\n                    nuggets[i] = 0;\r\n                <span class=\"comments\">\/* twenties loop *\/<\/span>\r\n                for( t=0; t*20&lt;i; t++ )\r\n                {\r\n                    if( s*6 + n*9 + t*20 == i )\r\n                        nuggets[i] = 0;\r\n                }\r\n            }\r\n        }\r\n        i++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* find largest value McNuggets number *\/<\/span>\r\n    max = 0;\r\n    for( x=0; x&lt;100; x++ )\r\n    {\r\n        if( nuggets[x]==1 )\r\n            max = x;\r\n    }\r\n    printf(\"Maximum non-McNuggets number is %d\\n\", max);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>nuggets[]<\/code> array is initialized at in the <em>for<\/em> loops at Lines 9 and 10. A value of 1 in the array indicates a non-McNuggets number. The rest of the code works to reset elements to 0 when a McNuggets number is found.<\/p>\n<p>Like the example on Rosetta Code, my code uses a master <em>while<\/em> loop at Line 14 to plow through values 0 through 99. Three nested <em>for<\/em> loops test the permutations of 6, 9, and 20, hunting for McNugget values. When a value is found, the element represented by <em>while<\/em> looping value <code>i<\/code> is reset:<\/p>\n<p><code>nuggets[i] = 0;<\/code><\/p>\n<p>My solution removes the <code>i++;<\/code> statements from within the <em>for<\/em> loops and sets it in the master <em>while<\/em> loop. Yes, this code churns more often than the original solution, but it avoids the dratted <em>goto<\/em> statements.<\/p>\n<p>A final <em>for<\/em> loop at Line 39 determines the highest element number that has retained its 1 (non-McNuggets) value. The output is the same as the <em>goto<\/em>-infested original:<\/p>\n<p><code>Maximum non-McNuggets number is 43<\/code><\/p>\n<p>I modified the code to calculate the highest McNuggets value below 1000. It was still 43, which is the same value the original code returned when I changed its limit as well. This change got me to thinking that 43 may be the highest non-McNuggets number possible.<\/p>\n<p>To test my theory, I changed the looping value to 1,000,000. This change made me notice how long it took the code to run. While the result continues to be 43, the original program and mine had two different runtimes, with the original taking far less time to execute. Though using <em>goto<\/em> statements may help with the speed, I wouldn&#8217;t recommend using them, especially because it isn&#8217;t the only option available.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes, it&#8217;s possible to code a solution without using the dratted <em>goto<\/em> statement. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4479\">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":[2],"tags":[],"class_list":["post-4479","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4479","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=4479"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4479\/revisions"}],"predecessor-version":[{"id":4503,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4479\/revisions\/4503"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}