{"id":6449,"date":"2024-06-22T00:01:10","date_gmt":"2024-06-22T07:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6449"},"modified":"2024-06-14T21:48:37","modified_gmt":"2024-06-15T04:48:37","slug":"a-grid-of-random-stars-part-vi","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6449","title":{"rendered":"A Grid of Random Stars, Part VI"},"content":{"rendered":"<p>The program may output what I want, but the code isn&#8217;t done yet. Continuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6442\">last week&#8217;s Lesson<\/a>, I&#8217;d like the program to output a single grid showing a summary of all the rectangles found. This update requires more changes to the existing code.<br \/>\n<!--more--><br \/>\nMy first attempt to create a single grid output resulted in a surprising reduction in the number of rectangles found. I don&#8217;t have a screenshot of the result, but the program went from finding 50+ rectangles to just a handful.<\/p>\n<p>After much gnashing of teeth, I discover that mapping out rectangles as each one was found resulted in many asterisks being overwritten. Therefore, fewer asterisks remained in the grid. Not good.<\/p>\n<p>To resolve this issue, I add a new array to the code: <code>all[]<\/code>. It&#8217;s a <em>rect<\/em> structure array, which stores the coordinates for each found rectangle. This data is gathered in the code&#8217;s main nested loop. When complete, the <code>all[]<\/code> array is used to plot the rectangles. At that point, it doesn&#8217;t matter if any asterisks are overwritten as their location is already recorded.<\/p>\n<p>I also modified the <em>update_grid()<\/em> function to avoid overwriting as much of the grid as possible. I don&#8217;t know if this change affected the output as it&#8217;s going to be a mess anyway.<\/p>\n<p>Here is the updated code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06_22-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_06_22-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\nvoid update_grid(char *g,struct rect r)\r\n{\r\n    int row,col;\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==r.top &amp;&amp; (col&gt;r.left &amp;&amp; col&lt;r.right) ) ||\r\n                    (row==r.bottom &amp;&amp; (col&gt;r.left &amp;&amp; col&lt;r.right) )\r\n              )\r\n                if( *(g+row*COLS+col) == '.' )\r\n                    *(g+row*COLS+col) = '-';\r\n            if(\r\n                    (col==r.left &amp;&amp; (row&gt;r.top &amp;&amp; row&lt;r.bottom) ) ||\r\n                    (col==r.right &amp;&amp; (row&gt;r.top &amp;&amp; row&lt;r.bottom) )\r\n                   )\r\n                if( *(g+row*COLS+col) == '.' )\r\n                    *(g+row*COLS+col) = '|';\r\n        }\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char *grid;\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\">\/* allocate grid *\/<\/span>\r\n    grid = malloc( sizeof(char) * SIZE );\r\n    if( grid==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\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*COLS+col) = '.');\r\n            else\r\n                putchar( *(grid+row*COLS+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*COLS+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*COLS+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*COLS+r.right)=='*' &amp;&amp; *(grid+r.bottom*COLS+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        update_grid(grid,all[x]);\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*COLS+col) );\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the <em>main()<\/em> function, the original, pristine grid is output first. Then, in the nested loop, the call to <em>update_grid()<\/em> is replaced with:<code>all[count] = r;<\/code> The coordinates the <code>struct rect<\/code> variable <code>r<\/code> are saved to the <code>all[]<\/code> array.<\/p>\n<p>After the main nested loop finishes, a <em>for<\/em> loop calls <em>update_grid()<\/em> to plot each of the rectangles found:<\/p>\n<p><code>for( x=0; x&lt;count; x++ )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;update_grid(grid,all[x]);<\/code><\/p>\n<p>Finally, the updated grid is output, along with the count:<\/p>\n<p><code>Original grid:<br \/>\n*..........*.*.*...*.**..*......<br \/>\n.......*..........*........**.*.<br \/>\n....*......*............*..***.*<br \/>\n.*.......***...**..**.....*.....<br \/>\n..**.**..*......***.*......*....<br \/>\n*.....*.....*....*..*......*....<br \/>\n................**.*..*.*.......<br \/>\n......*.......*..*.*....**.**...<br \/>\n*..............*..**............<br \/>\n...*....*.....*.....**...*......<br \/>\n..........*..*.....*..*...**....<br \/>\n.....*.*..*......*...........*..<br \/>\n..*.*........*.................*<br \/>\n.........*...**......*..........<br \/>\n...*.......................**..*<br \/>\n..*...*......*....*.......**..*.<br \/>\nFound 71 rectangles<br \/>\n*----------*-*-*---*-**--*......<br \/>\n|......*...|.|.|..*|-||--|-**-*.<br \/>\n|...*------*-|-|--||-||-*|-***|*<br \/>\n|*..|....***---**--**||-||*||.||<br \/>\n|.**|**--*---|-|***|*||--|-*|.||<br \/>\n*.||||*---|-*|-||*||*||-|||*|.||<br \/>\n|.|||||...|..|.|**|*-|*-*||||.||<br \/>\n|.||||*---|--|*|-*|*-||-**-**.||<br \/>\n*--------------*--**|||..||||.||<br \/>\n..|*|-|-*-|--|*---||**|--*|||.||<br \/>\n..|||||...*--*-----*--*---**|.||<br \/>\n..|||*|*--*--|---*|..|....|||*||<br \/>\n..*|*--------*----|--|-----||-|*<br \/>\n..||..|..*...**------*....|||.||<br \/>\n..|*--|-----------|--------**-|*<br \/>\n..*---*------*----*-------**--*.<\/code><\/p>\n<p>This output could be made pretty by adding colors, which I may do in the future. But I did notice one more change that make the code easier to read: removing pointer notation. This update is presented in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More fine tuning is required to get the code to where I want. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6449\">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-6449","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\/6449","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=6449"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6449\/revisions"}],"predecessor-version":[{"id":6453,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6449\/revisions\/6453"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}