{"id":6455,"date":"2024-06-29T00:01:08","date_gmt":"2024-06-29T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6455"},"modified":"2024-06-22T11:52:56","modified_gmt":"2024-06-22T18:52:56","slug":"a-grid-of-random-stars-part-vii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6455","title":{"rendered":"A Grid of Random Stars, Part VII"},"content":{"rendered":"<p>The final (and merciful) update to my Grid of Random Stars program involves two major changes. First, because I call the <em>update_grid()<\/em> function only once, it can be incorporated into the <em>main()<\/em> function, no program. Second, I remove pointer notation.<br \/>\n<!--more--><br \/>\nFrom <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6449\">last week&#8217;s Lesson<\/a>, the code works as well as I want it to. Yes, I could colorize the output, but that just adds more code at this point. No, I sought to return to the original program, which uses two-dimensional array notation instead of pointer notation. After all, because the code has no functions to call, pointers are unnecessary. Further, array notation is easier to read.<\/p>\n<p>Here is the update, final version of the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06_29-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_06_29-Lesson.c<\/a><\/h3>\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 ROWS 16\r\n#define COLS ROWS*2\r\n#define SIZE COLS*ROWS\r\n#define PROB 5\r\n\r\nstruct rect {\r\n    int top;\r\n    int bottom;\r\n    int left;\r\n    int right;\r\n};\r\n\r\nint main()\r\n{\r\n    char grid[ROWS][COLS];\r\n    int row,col,count,x;\r\n    struct rect r,all[SIZE];\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* fill and show the grid *\/<\/span>\r\n    puts(\"Original grid:\");\r\n    for( row=0; row&lt;ROWS; row++ )\r\n    {\r\n        for( col=0; col&lt;COLS; col++ )\r\n        {\r\n            if( rand() % PROB )\r\n                putchar( grid[row][col] = '.');\r\n            else\r\n                putchar( grid[row][col] = '*');\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    count = 0;\r\n    <span class=\"comments\">\/* scan for two in a column *\/<\/span>\r\n    for( r.top=0; r.top&lt;ROWS-1; r.top++ )\r\n    {\r\n        for( r.left=0; r.left&lt;COLS; r.left++ )\r\n        {\r\n            <span class=\"comments\">\/* find a star in the row *\/<\/span>\r\n            if( grid[r.top][r.left] == '*' )\r\n            {\r\n                <span class=\"comments\">\/* look for a matching star in the same column *\/<\/span>\r\n                for( r.bottom=r.top+1; r.bottom&lt;ROWS; r.bottom++ )\r\n                {\r\n                    if( grid[r.bottom][r.left] == '*' )\r\n                    {\r\n                        for( r.right=r.left+1; r.right&lt;COLS; r.right++ )\r\n                        {\r\n                            if( grid[r.top][r.right]=='*' &amp;&amp; grid[r.bottom][r.right]=='*' )\r\n                            {\r\n                                <span class=\"comments\">\/* add the grid to the array *\/<\/span>\r\n                                all[count] = r;\r\n                                count++;\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* build the full grid *\/<\/span>\r\n    for( x=0; x&lt;count; x++ )\r\n    {\r\n        for( row=0; row&lt;ROWS; row++ )\r\n        {\r\n            for( col=0; col&lt;COLS; col++ )\r\n            {\r\n                if(\r\n                        (row==all[x].top &amp;&amp; (col&gt;all[x].left &amp;&amp; col&lt;all[x].right) ) ||\r\n                        (row==all[x].bottom &amp;&amp; (col&gt;all[x].left &amp;&amp; col&lt;all[x].right) )\r\n                  )\r\n                    if( grid[row][col] == '.' )\r\n                        grid[row][col] = '-';\r\n                if(\r\n                        (col==all[x].left &amp;&amp; (row&gt;all[x].top &amp;&amp; row&lt;all[x].bottom) ) ||\r\n                        (col==all[x].right &amp;&amp; (row&gt;all[x].top &amp;&amp; row&lt;all[x].bottom) )\r\n                       )\r\n                    if( grid[row][col] == '.' )\r\n                        grid[row][col] = '|';\r\n            }\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the final grid *\/<\/span>\r\n    printf(\"Found %d rectangles\\n\",count);\r\n    for( row=0; row&lt;ROWS; row++ )\r\n    {\r\n        for( col=0; col&lt;COLS; col++ )\r\n            putchar( grid[row][col] );\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The code weighs in at 102 lines with a lot of nested loops and <em>if-else<\/em> constructions. The output is the same, and it&#8217;s a wee bit more readable. At last, I can say that I&#8217;m done with this project, which turned out to far more complex than I had anticipated, but still didn&#8217;t require any recursion or other fancy programming footwork.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The final update consolidates the code &mdash; and removes pointer notation. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6455\">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-6455","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\/6455","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=6455"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6455\/revisions"}],"predecessor-version":[{"id":6465,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6455\/revisions\/6465"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}