{"id":7520,"date":"2026-04-18T00:01:18","date_gmt":"2026-04-18T07:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7520"},"modified":"2026-04-11T12:29:12","modified_gmt":"2026-04-11T19:29:12","slug":"extracting-data-chunks","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7520","title":{"rendered":"Extracting Data Chunks"},"content":{"rendered":"<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/04\/Text_Girl_Pearl.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/04\/Text_Girl_Pearl-150x150.png\" alt=\"\" width=\"150\" height=\"150\" class=\"alignnone size-thumbnail wp-image-7530\" \/><\/a><br \/>\nThe next step in dividing a grid of data into chunks is to extract each of the chunks. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7508\">Last week&#8217;s Lesson<\/a> used color to visually identify the data chunks. This week&#8217;s update to the code pulls these chunks from the grid, using their values and colors to confirm that everything matches up properly.<br \/>\n<!--more--><br \/>\nMy first update is to change the colors. Honestly, it&#8217;s easier to deal with four colors as opposed to doing the math to maintain the eight colors used in last week&#8217;s Lesson. My choice is to set the four colors are foreground and background, which is technically eight color combinations. So I&#8217;m good.<\/p>\n<p>Extracting the chunks requires a nested loop inside a nested loop. The outer nest works through the main grid, array <code>grid[]<\/code> in the code, and the inner nest operates to output each data chunk. Terrifying math is required to extract the values in the proper order, though the equation is made easier by splitting it up into pieces, which I describe later.<\/p>\n<p>Here is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_04_18-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_04_18-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define ROWS 16\r\n#define COLUMNS 16\r\n#define AREA ROWS*COLUMNS\r\n#define NORMAL \"\\e[m\"\r\n\r\nint main()\r\n{\r\n    unsigned char grid[AREA];\r\n    int x,y,v,h,row,col;\r\n    int chunk_horz,chunk_vert,grid_width,grid_height;\r\n\r\n    for( y=0; y&lt;ROWS; y++ )\r\n    {\r\n        for( x=0; x&lt;COLUMNS; x++ )\r\n        {\r\n            grid[y*COLUMNS+x] = (y*COLUMNS)+x;\r\n            if( y&lt;ROWS\/2 )\r\n                printf(\"\\e[3%cm%02X%s \",\r\n                        (x\/4+1)+'0',\r\n                        grid[y*COLUMNS+x],\r\n                        NORMAL\r\n                      );\r\n            else\r\n                printf(\"\\e[4%c;30m%02X%s \",\r\n                        (x\/4+1)+'0',\r\n                        grid[y*COLUMNS+x],\r\n                        NORMAL\r\n                      );\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    <span class=\"comments\">\/* split into chunks *\/<\/span>\r\n    chunk_horz = 4;\r\n    chunk_vert = 8;\r\n    grid_height = ROWS\/chunk_vert;\r\n    grid_width = COLUMNS\/chunk_horz;\r\n\r\n    <span class=\"comments\">\/* part out the chunks *\/<\/span>\r\n    for( y=0; y&lt;grid_height; y++ )\r\n    {\r\n        for( x=0; x&lt;grid_width; x++ )\r\n        {\r\n            printf(\"Chunk %d:%d\\n\",y,x);\r\n            for( v=0; v&lt;chunk_vert; v++ )\r\n            {\r\n                for( h=0; h&lt;chunk_horz; h++ )\r\n                {\r\n                    row = y*chunk_vert+v;\r\n                    col = x*chunk_horz+h;\r\n                    if( y&lt;grid_height\/2 )\r\n                        printf(\"\\e[3%cm%02X%s \",\r\n                                (col\/4+1)+'0',\r\n                                grid[row*COLUMNS+col],\r\n                                NORMAL\r\n                              );\r\n                    else\r\n                        printf(\"\\e[4%c;30m%02X%s \",\r\n                                (col\/4+1)+'0',\r\n                                grid[row*COLUMNS+col],\r\n                                NORMAL\r\n                              );\r\n                }\r\n                putchar('\\n');\r\n            }\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The abundance of variables helps to calculate the various offsets and other stuff to output the grid and its chunks. Plus, dealing with double-nested loops requires a few extra variables just because.<\/p>\n<p>The first set of nested for loops outputs the grid using foreground and background colors to visually identify the chunks. These loops also fill the grid with data:<\/p>\n<p><code>grid[y*COLUMNS+x] = (y*COLUMNS)+x;<\/code><\/p>\n<p>The color calculation is made with this expression: <code>(x\/4+1)+'0'<\/code>. The value is set as either the foreground or background color, depending on the value of variable <code>y<\/code>. Figure 1 shows output from this part of the program.<\/p>\n<div id=\"attachment_7521\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7521\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure1.png\" alt=\"Colorful grid in a terminal window.\" width=\"550\" height=\"384\" class=\"size-full wp-image-7521\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure1-300x209.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure1-430x300.png 430w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-7521\" class=\"wp-caption-text\">Figure3 1. The grid output from the first nested loop.<\/p><\/div>\n<p>With the grid identified, the next part of the code extracts each of the color data chunks, outputting them one at a time with the matching colors. The numbers are what&#8217;s important, to ensure that they match. But the colors help to visually identify the chunks, even though these colors aren&#8217;t necessary for the result I&#8217;m after.<\/p>\n<p>The outer nested loop uses the <code>grid_height<\/code> and <code>grid_width<\/code> variables to navigate the values in <code>grid[]<\/code> by chunk. The inner loop uses variables <code>v<\/code> and <code>h<\/code> for the specific chunk sizes set in variables <code>chunk_vert<\/code> and <code>chunk_horz<\/code>.<\/p>\n<p>Variables <code>row<\/code> and <code>col<\/code> help calculate the offsets for each chunk within the larger grid:<\/p>\n<p><code>row = y*chunk_vert+v;<br \/>\ncol = x*chunk_horz+h;<\/code><\/p>\n<p>This expression <code>grid[row*COLUMNS+col]<\/code> calculates the offset within the grid, which is less complex thanks to separating the values for <code>row<\/code> and <code>col<\/code>.<\/p>\n<p>Figure 2 shows the output from this part of the code, which is quite tall but I split it into two columns:<\/p>\n<div id=\"attachment_7522\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7522\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure2.png\" alt=\"colorful text output on a terminal (two columns)\" width=\"550\" height=\"1230\" class=\"size-full wp-image-7522\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure2.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure2-134x300.png 134w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0418-figure2-458x1024.png 458w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-7522\" class=\"wp-caption-text\">Figure 2. The second part of the program&#8217;s output (split into two columns)<\/p><\/div>\n<p>The point of this exercise is to ensure that the data chunks are properly extracted. Based on their values and colors, everything looks good. The next step (which doesn&#8217;t involve three feet of output) is to calculate the averages for each data chunk. I present this update in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use colors and codes to ensure that the grid is properly divided into separate chunks. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7520\">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-7520","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\/7520","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=7520"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7520\/revisions"}],"predecessor-version":[{"id":7551,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7520\/revisions\/7551"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}