{"id":6423,"date":"2024-06-01T00:01:33","date_gmt":"2024-06-01T07:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6423"},"modified":"2024-06-08T08:21:08","modified_gmt":"2024-06-08T15:21:08","slug":"a-grid-of-random-stars-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6423","title":{"rendered":"A Grid of Random Stars, Part III"},"content":{"rendered":"<p>The next update to my pattern-finding program is to hunt down and find a clutch of asterisks in the grid that form a rectangle. Figure 1 illustrates what I&#8217;m after.<br \/>\n<!--more--><br \/>\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. Just a few of the many possible rectangles to be found in the grid.<\/p><\/div><\/p>\n<p>My approach is has three steps:<\/p>\n<ol>\n<li>Scan the entire grid. Use a nested loop to look for any asterisk.<\/li>\n<li>When an asterisk is found, look down the same row for an asterisk in that column.<\/li>\n<li>Scan across both rows searching for matching asterisks.<\/li>\n<\/ol>\n<p>These steps mimicked how I found rectangles when I tried this exercise on paper: I scanned down columns first for matching asterisks, then I checked each columns&#8217; rows. You could instead do it by columns and then rows as well. Same difference.<\/p>\n<p>I updated the code from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6415\">last week&#8217;s Lesson<\/a>, which uses pointers instead of a two-dimensional array.<\/p>\n<p>In the <em>main()<\/em> function, I constructed the second nested loop, which re-uses variables <code>row<\/code> and <code>col<\/code> to plow through the entire grid. A test is added to check for an asterisk:<\/p>\n<p><code>if( *(grid+row*COLS+col) == '*' )<\/code><\/p>\n<p>If true, I call the <em>scan_column()<\/em> function, which scans down the same column for another asterisk. It&#8217;s passed the grid&#8217;s address and the current row, column values:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* look in the current column `c` for a star *\/<\/span>\r\nint scan_column(char *g,int r,int c)\r\n{\r\n    int scandown;\r\n\r\n    for( scandown=r+1; scandown&lt;ROWS; scandown++ )\r\n    {\r\n        if( *(g+scandown*COLS+c) == '*' )\r\n            return(scandown);\r\n    }\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop scans down the column, using variable <code>scandown<\/code> to track the current row. When an asterisk is found, its row value is returned to the <em>main()<\/em> function. Otherwise, zero is returned.<\/p>\n<p>Upon success (a non-zero value) a second function is called from within the nested loop, <em>find_right()<\/em>. This function scans both rows for matching asterisks. When found, the column number is returned:<\/p>\n<pre class=\"screen\">\r\nint find_right(char *g,int r1,int c1,int r2)\r\n{\r\n    int f;\r\n\r\n    for( f=c1+1; f&lt;COLS; f++ )\r\n    {\r\n        if( *(g+r1*COLS+f)=='*' &amp;&amp; *(g+r2*COLS+f)=='*' )\r\n            return f;\r\n    }\r\n    return(0);\r\n}<\/pre>\n<p>Upon success, the <em>main()<\/em> function has the coordinates of four asterisks in the same column\/row positions &mdash; a rectangle. A <code>count<\/code> variable is incremented and the coordinate pairs are output.<\/p>\n<p>Here is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_06_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_06_01-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\nvoid output_grid(char *g)\r\n{\r\n    int r,c;\r\n\r\n    for( r=0; r&lt;ROWS; r++ )\r\n    {\r\n        for( c=0; c&lt;COLS; c++ )\r\n        {\r\n            putchar( *(g+r*COLS+c) );\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 r,int c)\r\n{\r\n    int scandown;\r\n\r\n    for( scandown=r+1; scandown&lt;ROWS; scandown++ )\r\n    {\r\n        if( *(g+scandown*COLS+c) == '*' )\r\n            return(scandown);\r\n    }\r\n    return(0);\r\n}\r\n\r\nint find_right(char *g,int r1,int c1,int r2)\r\n{\r\n    int f;\r\n\r\n    for( f=c1+1; f&lt;COLS; f++ )\r\n    {\r\n        if( *(g+r1*COLS+f)=='*' &amp;&amp; *(g+r2*COLS+f)=='*' )\r\n            return f;\r\n    }\r\n    return(0);\r\n}\r\n\r\nint main()\r\n{\r\n    char *grid;\r\n    int row,col,r,c,count;\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    <span class=\"comments\">\/* output the grid *\/<\/span>\r\n    output_grid(grid);\r\n\r\n    count = 0;\r\n    <span class=\"comments\">\/* scan for two in a column *\/<\/span>\r\n    for( row=0; row&lt;ROWS-1; row++ )\r\n    {\r\n        for( col=0; col&lt;COLS; col++ )\r\n        {\r\n            <span class=\"comments\">\/* find a star in the row *\/<\/span>\r\n            if( *(grid+row*COLS+col) == '*' )\r\n            {\r\n                <span class=\"comments\">\/* look for a matching star in the same column *\/<\/span>\r\n                r = scan_column(grid,row,col);\r\n                if(r)\r\n                {\r\n                    c = find_right(grid,row,col,r);\r\n                    if(c)\r\n                    {\r\n                        count++;\r\n                        printf(\"%2d %02d:%02d - %02d:%02d\\n\",\r\n                                count,row,col,row,c);\r\n                        printf(\"   %02d:%02d - %02d:%02d\\n\",\r\n                                r,col,r,c);\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here is a sample run (though not all of it):<\/p>\n<p><code>......**......*............*....<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&nbsp;1&nbsp;01:04&nbsp;-&nbsp;01:20<br \/>\n&nbsp;&nbsp;&nbsp;02:04&nbsp;-&nbsp;02:20<br \/>\n&nbsp;2&nbsp;01:11&nbsp;-&nbsp;01:13<br \/>\n&nbsp;&nbsp;&nbsp;09:11&nbsp;-&nbsp;09:13<br \/>\n&nbsp;3&nbsp;01:13&nbsp;-&nbsp;01:15<br \/>\n&nbsp;&nbsp;&nbsp;03:13&nbsp;-&nbsp;03:15<br \/>\n. . .<br \/>\n23&nbsp;09:04&nbsp;-&nbsp;09:13<br \/>\n&nbsp;&nbsp;&nbsp;12:04&nbsp;-&nbsp;12:13<br \/>\n24&nbsp;10:02&nbsp;-&nbsp;10:09<br \/>\n&nbsp;&nbsp;&nbsp;11:02&nbsp;-&nbsp;11:09<br \/>\n25&nbsp;11:23&nbsp;-&nbsp;11:31<br \/>\n&nbsp;&nbsp;&nbsp;12:23&nbsp;-&nbsp;12:31<\/code><\/p>\n<p>I got so excited with the results that I forgot two things: First, that recursion obviously isn&#8217;t need. But more important, the program isn&#8217;t really finding all the rectangles; a flaw exists in the code.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6434\">next week&#8217;s Lesson<\/a>, I update the code to graphically output the rectangles. Only then did I discover the flaw.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This update to the grid locates rectangular patterns and reports the coordinates. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6423\">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-6423","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\/6423","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=6423"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6423\/revisions"}],"predecessor-version":[{"id":6448,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6423\/revisions\/6448"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}