{"id":3165,"date":"2018-07-01T00:01:01","date_gmt":"2018-07-01T07:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3165"},"modified":"2018-07-08T19:21:51","modified_gmt":"2018-07-09T02:21:51","slug":"fill-my-grid","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3165","title":{"rendered":"Fill My Grid"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/07\/0701-grid_figure1.png\" alt=\"\" width=\"265\" height=\"272\" class=\"alignnone size-full wp-image-3173\" \/><\/p>\n<p>I&#8217;m fascinated by grids. Consider the chessboard, which is a grid. Or think about the most basic game grid, a 3-by-3 array for a game of tic-tac-toe. Programming a grid is a common thing, so your best hope is that this month&#8217;s Exercise is yet another super cinchy silly grid program.<br \/>\n<!--more--><br \/>\nAnd it is! Kinda.<\/p>\n<p>Your challenge is to create a 20-by-20 character grid. Fill the grid with period characters, <code>'.'<\/code>. Then, at 20 random locations, set asterisks <code>'*'<\/code> into the grid. An asterisk cannot occupy a space where an asterisk already sits. So when the code is done, you must see 20 randomly-placed asterisks in a 20-by-20 grid, similar to what&#8217;s illustrated in Figure 1.<\/p>\n<div id=\"attachment_3167\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3167\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure1.png\" alt=\"\" width=\"500\" height=\"372\" class=\"size-full wp-image-3167\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure1.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure1-300x223.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure1-403x300.png 403w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-3167\" class=\"wp-caption-text\">Figure 1. Twenty randomly placed asterisks in a 20-by-20 character grid.<\/p><\/div>\n<p>&#8220;Well,&#8221; you say, as you swell your chest and crack your knuckles, &#8220;This one is easy!&#8221;<\/p>\n<p>If you think so, great! Go ahead and code your solution to the Exercise. Remember to label it <em>Part A<\/em> because after you get it to work, you must take on <em>Part B<\/em>.<\/p>\n<p>For the Part B solution to this month&#8217;s Exercise, modify your solution for Part A and add some additional logic: The Part B challenge is to ensure that no two asterisks sit in the same row or column.<\/p>\n<p>Yes, the asterisks are still randomly placed. But, in addition to ensuring that an asterisk doesn&#8217;t already exist at that random location, you must check the entire row and column at the intersection for any other asterisks. Only when the rows and columns are clear can you set the asterisk.<\/p>\n<p>Figure 2 illustrates how such a solution might appear.<\/p>\n<div id=\"attachment_3168\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3168\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure2.png\" alt=\"\" width=\"500\" height=\"370\" class=\"size-full wp-image-3168\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure2.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure2-300x222.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/06\/0701-figure2-405x300.png 405w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-3168\" class=\"wp-caption-text\">Figure 2. Twenty randomly placed asterisks in a 20-by-20 character grid with no two asterisks sharing a row or column.<\/p><\/div>\n<p>Please enjoy working on this Exercise on your own before you <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3179\">check my solutions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take a grid of a given size and fill it with asterisks at random locations. Sounds easy? Well, I have a request that makes it difficult. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3165\">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":[3],"tags":[],"class_list":["post-3165","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3165","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=3165"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3165\/revisions"}],"predecessor-version":[{"id":3199,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3165\/revisions\/3199"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}