{"id":3179,"date":"2018-07-08T00:01:42","date_gmt":"2018-07-08T07:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3179"},"modified":"2018-06-30T13:21:44","modified_gmt":"2018-06-30T20:21:44","slug":"fill-my-grid-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3179","title":{"rendered":"Fill My Grid &#8211; Solution"},"content":{"rendered":"<p>The puzzle for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3165\">this month&#8217;s Exercise<\/a> was to create and fill a 20-by-20 character grid with 20 asterisks placed at random positions, no two asterisks in the same spot. I hope you found this Exercise interesting, but not too easy.<br \/>\n<!--more--><br \/>\nHere is my solution for Part A:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define SIZE 20\r\n\r\nint main()\r\n{\r\n    char grid[SIZE][SIZE];\r\n    int column,row,count;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* initialize the grid *\/<\/span>\r\n    for(column=0;column&lt;SIZE;column++)\r\n        for(row=0;row&lt;SIZE;row++)\r\n            grid[column][row] = '.';\r\n\r\n    <span class=\"comments\">\/* place the random asterisks *\/<\/span>\r\n    count = 0;\r\n    while(count &lt; SIZE)\r\n    {\r\n        column = rand() % SIZE;\r\n        row = rand() % SIZE;\r\n        if( grid[column][row] != '*' )\r\n        {\r\n            grid[column][row] = '*';\r\n            count++;\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* display the grid *\/<\/span>\r\n    for(column=0;column&lt;SIZE;column++)\r\n    {\r\n        for(row=0;row&lt;SIZE;row++)\r\n            printf(\" %c \",grid[column][row]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The randomizer is initialized at Line 13. The 20-by-20 <code>grid<\/code>, with the value 20 set as the <code>SIZE<\/code> constant, is initialized in Lines 16 through 18.<\/p>\n<p>Variable <code>count<\/code> tracks the number of asterisks placed, initialized at Line 21. Then a <em>while<\/em> loop spins as random locations are obtained in Lines 24 and 25. The <code>grid<\/code> location is tested at Line 26 to see whether an asterisk is already set at that location. If not, it&#8217;s set at Line 28 and variable <code>count<\/code> is increased at Line 29.<\/p>\n<p>Lines 34 through 39 display the results.<\/p>\n<p>I trust you coded a similar solution. The asterisk itself acts as a flag to determine whether a specific grid location is set. And the <em>while<\/em> loop keeps spinning as random locations are fetched until that last spot is found.<\/p>\n<p>To complicate matters, the Part B challenge to this Exercise required that the random location&#8217;s row and column be free of asterisks as well. You can use the code from the Part A solution to start your Part B solution, which is what I did. Extra overhead is required to scan not only the current row\/column position but all the same <code>row<\/code> and <code>column<\/code> positions for any asterisks.<\/p>\n<p>To perform the extra tests, I modified the <em>while<\/em> loop by adding two <em>for<\/em> loops. One scans the current <code>row<\/code> and the other scans the current <code>column<\/code>. If an asterisk is found in either row or column, a <code>found<\/code> flag is set. If the <code>found<\/code> flag remains zero, meaning both the row and column are devoid of asterisks, the asterisk is set at the random row\/column intersection. Here&#8217;s the modified <em>while<\/em> loop:<\/p>\n<pre class=\"screen\">\r\n    while(count &lt; SIZE)\r\n    {\r\n        column = rand() % SIZE;\r\n        row = rand() % SIZE;\r\n        found = 0;\r\n        if( grid[column][row] != '*' )\r\n        {\r\n            for(c=0;c&lt;SIZE;c++)\r\n                if( grid[c][row] == '*' )\r\n                    found++;\r\n            for(r=0;r&lt;SIZE;r++)\r\n                if( grid[column][r] == '*' )\r\n                    found++;\r\n            if( !found )\r\n            {\r\n                grid[column][row] = '*';\r\n                count++;\r\n            }\r\n        }\r\n    }<\/pre>\n<p>Variable <code>found<\/code> is initialized to zero just after the random <code>row<\/code> and <code>column<\/code> variables are set.<\/p>\n<p>The first <em>if<\/em> test checks the specific <code>row<\/code> and <code>column<\/code> position for an asterisk. After all, if it&#8217;s already set, why scan the rows and columns?<\/p>\n<p>Next, the <code>row<\/code> is scanned for asterisks. If one is present, the <code>found<\/code> variable is incremented. I could have stopped the loop once an asterisk is found. That&#8217;s because the code&#8217;s logic insists that no two asterisks will ever dwell in a single row or column. Still, time isn&#8217;t an issue in this code, so the loop keeps spinning.<\/p>\n<p>The <code>column<\/code> is scanned in the same manner as the <code>row<\/code>. This <em>for<\/em> loop could be avoided when an asterisk was found in the current row. Again, time isn&#8217;t an issue in the code so I didn&#8217;t add the necessary statements to skip this loop.<\/p>\n<p>Finally, the <code>found<\/code> variable is tested with &#8220;if not found.&#8221; When true, the rows and columns are clear, meaning the asterisk can be set.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/07exercise-b.c\">Click here<\/a> to view my entire solution for Part B.<\/p>\n<p>I hope that you enjoyed this Exercise and devised a solution that not only worked, but offered a clever way to solve the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The puzzle for this month&#8217;s Exercise was to create and fill a 20-by-20 character grid with 20 asterisks placed at random positions, no two asterisks in the same spot. I hope you found this Exercise interesting, but not too easy.<\/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-3179","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\/3179","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=3179"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3179\/revisions"}],"predecessor-version":[{"id":3190,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3179\/revisions\/3190"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}