{"id":6442,"date":"2024-06-15T00:01:53","date_gmt":"2024-06-15T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6442"},"modified":"2024-06-22T10:15:53","modified_gmt":"2024-06-22T17:15:53","slug":"a-grid-of-random-stars-part-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6442","title":{"rendered":"A Grid of Random Stars, Part V"},"content":{"rendered":"<p>The two problems I recognized in the code from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6434\">last week&#8217;s Lesson<\/a> were that <em>scan_column()<\/em> and <em>find_right()<\/em> don&#8217;t need to be separate functions. Also, the code fails to find all the rectangles in the grid, which is bad. Time to fix the code!<br \/>\n<!--more--><br \/>\nPulling the code from the two functions into the <em>main()<\/em> function was easy. Each function is basically a <em>for<\/em> statement with an <em>if<\/em> test inside &mdash; two lines. No value needs to be returned as the <em>if<\/em> test triggers whether the next <em>for<\/em> loop is executed.<\/p>\n<p>Further, by moving the functions into the main nested loop, the problem with not finding the next rectangle in the same row is immediately solved! Remember, my original assumption was that the <em>scan_column()<\/em> and <em>find_right()<\/em> functions would be called recursively. They don&#8217;t need to be as long as the loops continue to work within the main program loop.<\/p>\n<p>Here is the updated, consolidated code, which is rather wide on this page:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06_15-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_06_15-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 output_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                putchar('-');\r\n            else 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                putchar('|');\r\n            else\r\n                putchar( *(g+row*COLS+col) );\r\n        }\r\n        putchar('\\n');\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    char *grid;\r\n    int row,col,count;\r\n    struct rect r;\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 the grid *\/<\/span>\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                *(grid+row*COLS+col) = '.';\r\n            else\r\n                *(grid+row*COLS+col) = '*';\r\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                                count++;\r\n                                printf(\"Rectangle %d:\\n\",count);\r\n                                output_grid(grid,r);\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n    printf(\"Found %d rectangles\\n\",count);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>No changes were needed for the <em>output_grid()<\/em> function. My goal here is only to incorporate the <em>scan_column()<\/em> and <em>find_right()<\/em> and ensure that all the rectangles are found. Otherwise, the code runs the same as it did before, outputting subsequent rectangles as they&#8217;re found in the grid. Here is partial output:<\/p>\n<p><code>Rectangle 1:<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 \/>\nRectangle 2:<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...*...**.*........*.....**.....<\/p>\n<p>...<\/p>\n<p>Rectangle 82:<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 \/>\nRectangle 83:<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 83 rectangles<\/code><\/p>\n<p>The total of 83 rectangles seems more appropriate. I also confirmed the number by printing the output and checking each location. Yes, the computer does the job far faster than a human.<\/p>\n<p>My task is essentially complete. I&#8217;m able to generate a grid of random asterisks and use the computer to locate them, highlighting the output. Being a programmer nerd, however, I&#8217;m not quite done yet. I would like to create output similar to Figure 1, though in ASCII.<\/p>\n<div id=\"attachment_6424\" style=\"width: 358px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6424\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0601-figure1.png\" alt=\"grid pattern\" width=\"348\" height=\"342\" class=\"size-full wp-image-6424\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0601-figure1.png 348w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0601-figure1-300x295.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0601-figure1-305x300.png 305w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><p id=\"caption-attachment-6424\" class=\"wp-caption-text\">Figure 1. Output like this would be preferred, but in ASCII of course.<\/p><\/div>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6449\">next week&#8217;s Lesson<\/a>, I shorten the output to display the original grid and then another that shows all the triangles, or as many of them as can be mapped in text output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s time to pull the code together and fix a major problem. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6442\">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-6442","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\/6442","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=6442"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6442\/revisions"}],"predecessor-version":[{"id":6464,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6442\/revisions\/6464"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}