{"id":1945,"date":"2016-06-04T00:01:43","date_gmt":"2016-06-04T07:01:43","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1945"},"modified":"2016-05-28T11:25:31","modified_gmt":"2016-05-28T18:25:31","slug":"a-solution-for-100-doors","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1945","title":{"rendered":"A Solution for 100 Doors"},"content":{"rendered":"<p>When I code a program, I start out by slapping together the various elements. I setup the variables, I write some quick routines, and I add comments to the tune of <code>\/* Do something here*\/<\/code>. With those bricks in place, I go back and fill in the mortar to make it all work. If the code runs, great! That rarely happens, so more work is involved.<br \/>\n<!--more--><br \/>\nFor the 100 Doors puzzle, I first created two parts of the program, which I discussed in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1937\">last week&#8217;s Lesson<\/a>: the array of 100 doors and the routine that opens or closes a door. The steps that remain are processing the puzzle &mdash; the loop &mdash; and displaying the results.<\/p>\n<p>The loop is the heart of the puzzle, the virtual representation of opening and closing 100 doors. According to the puzzle&#8217;s rules, you must visit all doors, 1 through 100. To solve that problem I wrote a prototype loop:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* start at door 1 and work up to door DOORS *\/<\/span>\r\n    for(pass=0; pass&lt;DOORS; pass++)\r\n    {\r\n        door[thisdoor] = !door[thisdoor];\r\n    }<\/pre>\n<p>The <em>for<\/em> loop uses the <code>pass<\/code> (<em>int<\/em>) variable to process all 100 doors. I tossed the open\/close statement into the loop. At this point, the code opens all the doors. (The <code>door[]<\/code> array is initialized to 0, all doors closed.) So step one of the problem is solved, and my code has a placeholder. At this point, I review the rest of the puzzle, i.e., to skip a given number of doors and continue processing.<\/p>\n<p>So on the second pass, you skip every other door. On the third pass you skip every third door. As I thought of the process, I mapped out the skipping pattern:<\/p>\n<p>First pass, skip 1 door<br \/>\nSecond pass, skip 2 doors<br \/>\nThird pass, skip 3 doors<\/p>\n<p>This pattern represents a loop. Indeed, the loop is already in the code:<\/p>\n<pre><code>    for(pass=0; pass&lt;DOORS; pass++)<\/code><\/pre>\n<p>My conclusion is that a nested <em>for<\/em> loop is needed. It can conveniently use the first loop&#8217;s <code>pass<\/code> variable as both the starting point and skip value. Here&#8217;s what I came up with:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* start at door 1 and work up to door DOORS *\/<\/span>\r\n    for(pass=0; pass&lt;DOORS; pass++)\r\n    {\r\n        for(thisdoor=pass; thisdoor&lt;DOORS; thisdoor+=pass)\r\n            door[thisdoor] = !door[thisdoor];\r\n    }<\/pre>\n<p>The inner <em>for<\/em> loop starts at the value of variable <code>pass<\/code> and processes all the doors, skipping in increments of variable <code>pass<\/code> in the process. It&#8217;s this loop that opens\/closes the doors.<\/p>\n<p>To me, this code looked good. I added a simple output routine to display the results:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* Display results *\/<\/span>\r\n    for(x=0;x&lt;DOORS;x++)\r\n        printf(\"Door %d is %s\\n\",\r\n                x+1,\r\n                door[x] ? \"open\" : \"closed\");<\/pre>\n<p>I saved. I compiled. I ran. And . . .<\/p>\n<p>It didn&#8217;t work. The code generated no output and didn&#8217;t stop. I knew it got stuck in an endless loop, but where? So I read the source code again and again.<\/p>\n<p>In my C programming books, I mention reading code aloud as one way to track down bugs. In this case, that method helped me to find the problem, which occurs in the inner <em>for<\/em> loop: <code>thisdoor+=pass<\/code> When <code>pass<\/code> is equal to zero (its starting value), the loop becomes endless; variable <code>thisdoor<\/code> never increments.<\/p>\n<p>To fix the code, I switched from initializing variable <code>pass<\/code> at at zero and used 1 instead. Here are the modified nested loop statements:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* start at door 1 and work up to door DOORS *\/<\/span>\r\n    for(pass=1; pass&lt;=DOORS; pass++)\r\n    {\r\n        for(thisdoor=pass-1; thisdoor&lt;DOORS; thisdoor+=pass)\r\n            door[thisdoor] = !door[thisdoor];\r\n    }<\/pre>\n<p>The outer <em>for<\/em> loop starts at 1. The inner <em>for<\/em> loop must subtract 1 from <code>pass<\/code> in order to reference the first element of array <code>door[]<\/code>. Now the statement <code>thisdoor+=pass<\/code> properly calculates the skip values.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/05\/0604-100_doors.c\">Click here<\/a> to see the end result of my efforts.<\/p>\n<p>My solution is rather mundane when compared with other programmers&#8217; solutions. Some coders attempt to optimize the code, some to obfuscate. Mine is rather straightforward and not particularly clever, but more than the solution I wanted to illustrate how I approach such programming puzzles.<\/p>\n<p>By the way, the solution for the 100 Doors puzzle is that after the process is complete, only the door numbers matching the squares of the first 10 numbers remain open: 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Putting together the pieces and solving a programming puzzle. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1945\">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-1945","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\/1945","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=1945"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1945\/revisions"}],"predecessor-version":[{"id":1957,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1945\/revisions\/1957"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}