{"id":6434,"date":"2024-06-08T00:02:36","date_gmt":"2024-06-08T07:02:36","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6434"},"modified":"2024-06-15T08:59:05","modified_gmt":"2024-06-15T15:59:05","slug":"a-grid-of-random-stars-part-iv","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6434","title":{"rendered":"A Grid of Random Stars, Part IV"},"content":{"rendered":"<p>I was so excited with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6423\">last week&#8217;s code<\/a> that I didn&#8217;t bother to confirm that it worked properly. No, I was eager to see graphic output of the found rectangles.<br \/>\n<!--more--><br \/>\nBecause the code already finds rectangular asterisk coordinates within the grid, outputting them graphically only involves an update to the <em>output_grid()<\/em> function. Before I wrote the update, I decided to create a structure to store the coordinate variables:<\/p>\n<p><code>struct rect {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int top;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int bottom;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int left;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;int right;<br \/>\n};<\/code><\/p>\n<p>Further, I updated the variable names used in the <em>scan_column()<\/em> and <em>find_right()<\/em> function to reference the <code>top<\/code>, <code>left<\/code>, <code>right<\/code>, and <code>bottom<\/code> coordinates. This change maakes reading the code easier.<\/p>\n<p>In the <em>main()<\/em> function, <code>struct rect r<\/code> is declared. This structure, filled with the coordinates of a found rectangle, is passed to the <em>output_grid()<\/em> function, along with the <code>grid<\/code> pointer.<\/p>\n<p>The <em>output_grid()<\/em> function performs tests on each row\/column offset, comparing these values with the coordinates stored in the <code>r<\/code> structure. When a coordinate pair match is found, ASCII characters are output to draw the found rectangle.<\/p>\n<p>I decided to draw the rectangles individually to ensure that they were all found and properly mapped. Here is the full, updated code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06_08-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_06_08-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\n<span class=\"comments\">\/* look in the current column `c` for a star *\/<\/span>\r\nint scan_column(char *g,int top,int left)\r\n{\r\n    int scandown;\r\n\r\n    for( scandown=top+1; scandown&lt;ROWS; scandown++ )\r\n    {\r\n        if( *(g+scandown*COLS+left) == '*' )\r\n            return(scandown);\r\n    }\r\n    return(0);\r\n}\r\n\r\nint find_right(char *g,int top,int left,int bottom)\r\n{\r\n    int right;\r\n\r\n    for( right=left+1; right&lt;COLS; right++ )\r\n    {\r\n        if( *(g+top*COLS+right)=='*' &amp;&amp; *(g+bottom*COLS+right)=='*' )\r\n            return right;\r\n    }\r\n    return(0);\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                r.bottom = scan_column(grid,r.top,r.left);\r\n                if(r.bottom)\r\n                {\r\n                    r.right = find_right(grid,r.top,r.left,r.bottom);\r\n                    if(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    printf(\"Found %d rectangles\\n\",count);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here&#8217;s abridged output from a sample run:<\/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>...<br \/>\nRectangle 19:<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 20:<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 20 rectangles<\/code><\/p>\n<p>Though I was delighted with the output, I noticed two things.<\/p>\n<p>First, the rectangles were found without needing recursion. Therefore, I could incorporate the <em>scan_column()<\/em> and <em>find_right()<\/em> functions directly into the <em>main()<\/em> function. But this step puzzled me because I knew some repetition was in order, which is the second thing I noticed: The program stops in a row when it finds the first rectangle. The code doesn&#8217;t return to a row to look for more rectangles. The output above showing &#8220;Found 20 rectangles&#8221; is far too low!<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6442\">next week&#8217;s Lesson<\/a>, I address both these issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to graphically output the found rectangles. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6434\">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-6434","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\/6434","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=6434"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6434\/revisions"}],"predecessor-version":[{"id":6461,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6434\/revisions\/6461"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}