{"id":6415,"date":"2024-05-25T00:01:57","date_gmt":"2024-05-25T07:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6415"},"modified":"2024-05-18T16:35:26","modified_gmt":"2024-05-18T23:35:26","slug":"a-grid-of-random-stars-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6415","title":{"rendered":"A Grid of Random Stars, Part II"},"content":{"rendered":"<p>To count all possible rectangles in a random grid requires a lot of scanning. My first thought was that the process would involve recursion. I wasn&#8217;t 100 percent certain, but I wanted to prep the code for such a possibility.<br \/>\n<!--more--><br \/>\nThe first issue to deal with is the two-dimensional array, <code>grid[][]<\/code>. It&#8217;s difficult to pass such a beast to a function. A single-dimension array can be passed, similar to a pointer. As the overhead required to calculate row\/column offsets is identical for a one-dimension array as it would be for a pointer, I converted <code>grid[][]<\/code> into <code>*grid<\/code>.<\/p>\n<p>Another modification I made was to place the output statements into their own function:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05_25-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_05_25-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\r\nint main()\r\n{\r\n    char *grid;\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\">\/* 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    <span class=\"comments\">\/* close it up *\/<\/span>\r\n    free(grid);\r\n    return 0;\r\n}<\/pre>\n<p>To allocate storage for the grid, I added defined constant <code>SIZE<\/code>. It&#8217;s set equal to the number of columns (<code>COLS<\/code>) and rows (<code>ROWS<\/code>) in the grid, also defined constants. The <em>malloc()<\/em> function allocates a character buffer for the grid:<\/p>\n<p><code>grid = malloc( sizeof(char) * SIZE );<\/code><\/p>\n<p>I don&#8217;t need to add one to the buffer size as it&#8217;s not a character array.<\/p>\n<p>To reference cells in the grid requires a bit of math. Here&#8217;s the format I use in the code:<\/p>\n<p><code>*( <em>base<\/em> + <em>current_row<\/em> * <em>column_count<\/em> + <em>current_column<\/em> )<\/code><\/p>\n<p>The <em>base<\/em> is the member buffer&#8217;s address, stored in the pointer. <em>current_row<\/em> is the row to place the character. <em>column_count<\/em> is the total number of columns. <em>current_column<\/em> is the column to place the character. This expression determines the proper offset within the buffer, <em>base<\/em>. It&#8217;s used to fill the grid with asterisk or period characters. It&#8217;s also used in the <em>output_grid()<\/em> function.<\/p>\n<p>This update to the code generates the same output as the original code, but now it&#8217;s prepped for me to add functions that start scanning for matching asterisks. These must be included in a second nested loop that scans the grid for asterisk characters. I present this update in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once created, the next step is to hunt for rectangles in the random grid of stars. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6415\">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-6415","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\/6415","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=6415"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6415\/revisions"}],"predecessor-version":[{"id":6429,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6415\/revisions\/6429"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}