{"id":5758,"date":"2023-02-25T00:01:30","date_gmt":"2023-02-25T08:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5758"},"modified":"2023-03-04T09:22:38","modified_gmt":"2023-03-04T17:22:38","slug":"playing-with-a-grid","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5758","title":{"rendered":"Playing with a Grid"},"content":{"rendered":"<p>Grids, or matrixes, are a common data thingy, as information often appears in tables. Being able to fold, spindle, and mutilate a grid is a common computer programming task, something to entertain your idle hours even if you have no pressing need to manipulate a matrix.<br \/>\n<!--more--><br \/>\nBefore I abuse a matrix\/grid, it must be created. Two approaches are available: traditional array notation and pointer nightmare.<\/p>\n<p>Array notation is easier, so it comes first:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_02_25-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_02_25-Lesson-a.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 SIZE 3\r\n\r\nint main()\r\n{\r\n    int x,y;\r\n    int grid[SIZE][SIZE];\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* populate the grid *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        for( y=0; y&lt;SIZE; y++ )\r\n            grid[x][y] = rand()%10 + 1;\r\n\r\n    <span class=\"comments\">\/* output the grid *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n    {\r\n        for( y=0; y&lt;SIZE; y++ )\r\n            printf(\" %d \",grid[x][y]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The defined constant <code>SIZE<\/code> sets the grid&#8217;s row and column count &mdash; a square. In this example, the matrix is 3&#215;3 (or <code>SIZE<\/code> x <code>SIZE<\/code>) integers, which is how the <code>grid[][]<\/code> two-dimensional array is declared at Line 10.<\/p>\n<p>After seeding the randomizer, nested <em>for<\/em> loops populate the grid: rows (variable <code>x<\/code>) by columns (variable <code>y<\/code>). Random values in the range of 1 through 10 populate the matrix: <code>rand()%10 + 1<\/code><\/p>\n<p>A second set of nested <em>for<\/em> loops outputs the grid, setting a newline between each row.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>&nbsp;4&nbsp;&nbsp;7&nbsp;&nbsp;1<br \/>\n&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;7<br \/>\n&nbsp;8&nbsp;&nbsp;5&nbsp;&nbsp;9<\/code><\/p>\n<p>As I&#8217;m fond of writing, a two-dimensional array is merely a one-dimensional array all gussied up for programmer readability. Honestly, it&#8217;s all one chunk of data in memory anyway. But the double bracket notation makes things easier to read &mdash; as long as you don&#8217;t forget that the first array element is zero, not one.<\/p>\n<p>To pervert the same code into a pointer version, I rely upon the assumption that the matrix is just one long chunk of data. The same <code>SIZE<\/code> constant defines the dimensions and manipulates the data as a matrix. The code is a little tighter in some spots, but requires more statements as the matrix buffer must be allocated.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_02_25-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_02_25-Lesson-b.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 SIZE 3\r\n\r\nint main()\r\n{\r\n    int x;\r\n    int *grid;\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 the grid *\/<\/span>\r\n    grid = malloc( sizeof(int) * SIZE * 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\">\/* populate the grid *\/<\/span>\r\n    for( x=0; x&lt;SIZE*SIZE; x++ )\r\n        *(grid+x) = rand()%10 + 1;\r\n\r\n    <span class=\"comments\">\/* output the grid *\/<\/span>\r\n    for( x=0; x&lt;SIZE*SIZE; x++ )\r\n    {\r\n        printf(\" %d \",*(grid+x));\r\n        if( (x+1)%3 == 0 )\r\n            putchar('\\n');\r\n    }\r\n\r\n    free(grid);\r\n    return(0);\r\n}<\/pre>\n<p>Storage is allocated at Line 16, assigned to <em>int<\/em> pointer variable <code>grid<\/code>:<\/p>\n<p><code>grid = malloc( sizeof(int) * SIZE * SIZE );<\/code><\/p>\n<p>The <code>SIZE * SIZE<\/code> calculation allocates the same size as the two-dimensional array. Then a test is made to ensure storage was successfully allocated.<\/p>\n<p>Only a single <em>for<\/em> loop is required to assign values to the matrix, again using <code>SIZE*SIZE<\/code> to set the quantity of integers required. The expression <code>*(grid+x)<\/code> ensures that the values are set at the proper offset.<\/p>\n<p>A single <em>for<\/em> loop is all that&#8217;s needed to output the matrix. An <em>if<\/em> statement determines when <code>SIZE<\/code> items are output &mdash; <code>if( (x+1)%3 == 0 )<\/code> &mdash; at the end of a row, a newline is output. Here&#8217;s the sample run:<\/p>\n<p><code>&nbsp;1&nbsp;&nbsp;8&nbsp;&nbsp;5<br \/>\n&nbsp;9&nbsp;&nbsp;7&nbsp;&nbsp;3<br \/>\n&nbsp;5&nbsp;&nbsp;3&nbsp;&nbsp;1<\/code><\/p>\n<p>Yes, it&#8217;s the same output, just a different approach. Either way, the matrix is created and prepped for abuse.<\/p>\n<p>For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5764\">next week&#8217;s Lesson<\/a>, the task is to total each of the rows and columns.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first step toward grid nirvana, is to construct and populate the grid. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5758\">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-5758","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\/5758","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=5758"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5758\/revisions"}],"predecessor-version":[{"id":5796,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5758\/revisions\/5796"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}