{"id":7536,"date":"2026-04-25T00:01:06","date_gmt":"2026-04-25T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7536"},"modified":"2026-04-18T11:42:07","modified_gmt":"2026-04-18T18:42:07","slug":"obtaining-the-averages","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7536","title":{"rendered":"Obtaining the Averages"},"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 point of extracting data chunks from a grid is to obtain the average of their values. This approach is how I reduce the massive amount of data stored in an image for representation as ASCII text. (See the thumbnail above.)<br \/>\n<!--more--><br \/>\nThis week&#8217;s code continues the process, though it removes a few statements from what was presented in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7520&#038;preview=true\">last week&#8217;s Lesson<\/a>. After all, the purpose of last week&#8217;s program was to demonstrate that the data was being chopped into chunks and that the chopping part worked. It does. So the next step is to examine (but not output) the chunks and obtain their average values.<\/p>\n<p>The code update is more compact and the output is mercifully shorter:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_04_25-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_04_25-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    int total;\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            total = 0;\r\n            printf(\"Chunk %d:%d\",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                    total += grid[row*COLUMNS+col];\r\n                }\r\n            }\r\n            printf(\" average = %2X\\n\",total\/(chunk_vert*chunk_horz));\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The update for this code occurs in the double nested loop. The inner nest calculates offsets within each data chunk, but instead of outputting the bytes (and colors) the values are accumulated in variable <code>total<\/code>: <code>total += grid[row*COLUMNS+col];<\/code><\/p>\n<p>After a chunk is examined, the average value is output:<\/p>\n<p><code>printf(\" average = %2X\\n\",total\/(chunk_vert*chunk_horz));<\/code><\/p>\n<p>The program still generates color for the initial display, but the secondary output is reduced to showing only the average values for each chunk. Figure 1 shows a sample run.<\/p>\n<div id=\"attachment_7537\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7537\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/04\/0425-figure1.png\" alt=\"Colorful text output in grids\" width=\"550\" height=\"559\" class=\"size-full wp-image-7537\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/04\/0425-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/04\/0425-figure1-295x300.png 295w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-7537\" class=\"wp-caption-text\">Figure 1. Program output showing the original grid and each chunk&#8217;s average value.<\/p><\/div>\n<p>The average values eyeball okay. Even so, I hand calculated a chunk&#8217;s value to confirm that the average was calculated. Everything looks good.<\/p>\n<p>The goal of this exercise was to reduce pixel data in an image so that it could be represented on the text screen as a character value. The character value is chosen based on the pixel&#8217;s intensity or luminosity. (I&#8217;m not a scientist, so I don&#8217;t know which is which.) Other elements come into play as well, such as calculating the ratio of the original image&#8217;s pixel size to a terminal window&#8217;s dimensions. But the part that fascinated me the most was the double-nested loop to examine a chunk within a grid. That part was the most fun to code.<\/p>\n<p>I may pick up on this process in a future Lesson. You see the results as the thumbnail atop this post, which uses the <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/04\/Girl-with-the-Pearl-Earring.jpg\">Girl with the Pearl Earring<\/a> as the sample JPEG file. Obviously, a lot more coding is required to generate an ASCII image from a JPEG. It was a fun exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The point of extracting data chunks from a grid is to obtain the average of their values. This approach is how I reduce the massive amount of data stored in an image for representation as ASCII text. (See the thumbnail &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7536\">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-7536","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\/7536","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=7536"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7536\/revisions"}],"predecessor-version":[{"id":7564,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7536\/revisions\/7564"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}