{"id":6405,"date":"2024-05-18T00:01:03","date_gmt":"2024-05-18T07:01:03","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6405"},"modified":"2024-05-25T10:01:25","modified_gmt":"2024-05-25T17:01:25","slug":"analyzing-a-grid-of-random-stars","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6405","title":{"rendered":"Analyzing a Grid of Random Stars"},"content":{"rendered":"<p>Ever since I was a little boy and coded my first nested loop, I&#8217;ve enjoyed messing with grids in C. I&#8217;ve done monthly calendars in grids, rotated grids, manipulated grids mathematically &mdash; all sorts of fun girddy stuff. But I&#8217;m not done yet.<br \/>\n<!--more--><br \/>\nPopulating a grid randomly fascinates me. I look for patterns in the randomness. To sate this desire, I wrote the program below. It outputs a two-dimensional array randomly populated with asterisks.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05_18-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_05_18-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 PROB 5\r\n\r\nint main()\r\n{\r\n    char grid[ROWS][COLS];\r\n    int row,col;\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 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][col] = '.';\r\n            else\r\n                grid[row][col] = '*';\r\n            putchar(grid[row][col]);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Three defined constants set two aspects of the grid: its dimensions and the randomness of the asterisks. <code>ROWS<\/code> sets the number rows and <code>COLS<\/code> is set to twice the number of rows, which looks better on the standard text screen. <code>PROB<\/code> sets the probability of a character being an asterisk as opposed to the default period.<\/p>\n<p>Nested <em>for<\/em> loops fill the grid. In the inner loop, an <em>if<\/em> statement bases its condition on the return value from the <em>rand()<\/em> function modulo constant <code>PROB<\/code>. For non-zero values (most of them), a period is set in the matrix. Otherwise, an asterisk is set. A <em>putchar()<\/em> function outputs the character set in the grid.<\/p>\n<p>Here&#8217;s sample output:<\/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.*.......*...*.*......*.....**..<\/code><\/p>\n<p>Adjusting the value of <code>PROB<\/code> increases or decreases the density of the asterisk characters. If I set <code>PROB<\/code> equal to 20, here is the output:<\/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.*.............*......*.........<\/code><\/p>\n<p>If I set it to two, you see this grid:<\/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.**....**..*.***..**.**.....*.*.<\/code><\/p>\n<p>I think five is a good value. But going back to my point, what catches my eye in the grid are patterns. What I want to do with the grid is to find rectangles based on coordinates of the asterisks. For example:<\/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....*------*------*------****-*.<\/code><\/p>\n<p>Above you see output from the final rendition of this program. The program searches for a pair of asterisks that are in the same columns across two rows, which forms a rectangle. Once found, the program draws ASCII lines between the asterisks, setting them in the grid. Above, 79 rectangles are found in the grid. Each rectangle is marked, though you see a lot of overlap.<\/p>\n<p>Over the next few Lessons, I will modify the base program to scan for, document, and show the various rectangles in the random grid. This task took several generations of coding to accomplish. Many times I&#8217;d start down one path, then realize a better way exists. I document all of that, failures and successes, to show you how the final program was concocted.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6415\">next week&#8217;s Lesson<\/a>, I convert the two-dimensional array into pointer notation so that it can be easily passed to various functions. It&#8217;s my assumption that perhaps some form of recursion is necessary to accomplish this task. I&#8217;ll explain how it works (or not) as the program progresses.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s time for me to sate my grid fascination. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6405\">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-6405","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\/6405","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=6405"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6405\/revisions"}],"predecessor-version":[{"id":6431,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6405\/revisions\/6431"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}