{"id":7508,"date":"2026-04-11T00:01:00","date_gmt":"2026-04-11T07:01:00","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7508"},"modified":"2026-04-04T14:00:19","modified_gmt":"2026-04-04T21:00:19","slug":"colorful-data-chunks","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7508","title":{"rendered":"Colorful 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 \/>\nContinuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7491\">last week&#8217;s Lesson<\/a>, my goal for the grid is to split it into chunks which are then reduced based on the average value of each chunk. To best visualize this, I&#8217;d like to keep the byte patterns in the grid consistent and &mdash; as a bonus &mdash; color-code them.<br \/>\n<!--more--><br \/>\nThe following code generates a grid with dimensions ROWS by COLUMS. Each item in the grid is coded by row and column. The grid is output as it&#8217;s stored:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_04_11-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_04_11-Lesson-a.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\r\nint main()\r\n{\r\n    unsigned char grid[AREA];\r\n    int x,y;\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[x] = x;\r\n            printf(\"%02X \",x);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Nested <em>for<\/em> loops generate the grid&#8217;s contents, assigning elements to the grid and outputting them one row at a time:<\/p>\n<pre>00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F<\/pre>\n<p>With the grid&#8217;s data organized in this manner it&#8217;s easier for me to confirm how the chunks are split. But what would be even better is to color-code the chunks.<\/p>\n<p>Color output at the terminal is made possibly by applying <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270\">ANSI codes<\/a> to standard output. While I code define these codes as I&#8217;ve done in other source code files, here I can take advantage of the <code>x<\/code> looping value to apply different colors to the grid. The following code shows this update:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_04_11-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_04_11-Lesson-b.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;\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[x] = x;\r\n            if( y&lt;8 )\r\n                printf(\"\\e[3%cm%02X%s \",(x\/4+1)+'0',x,NORMAL);\r\n            else\r\n                printf(\"\\e[3%cm%02X%s \",(x\/4+5)+'0',x,NORMAL);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The specific color code values are listed in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270\">this post from 2022<\/a>. The escape sequences appear in the <em>printf(<\/em>) statements. The specific color code is calculated by dividing the <code>x<\/code> value by four and then adding one: <code>(x\/4+1)<\/code> or adding five for the second row: <code>(x\/4+5)<\/code><\/p>\n<p>The program&#8217;s output generates a grid with eight distinct color areas, as shown here:<\/p>\n<div id=\"attachment_7510\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7510\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0411-figure1-300x207.png\" alt=\"A colorful grid of numbers\" width=\"300\" height=\"207\" class=\"size-medium wp-image-7510\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0411-figure1-300x207.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0411-figure1-434x300.png 434w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0411-figure1.png 765w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><p id=\"caption-attachment-7510\" class=\"wp-caption-text\">Figure 1. The colorful grid output.<\/p><\/div>\n<p>With the grid divided into eight regions, my next task is to separate the regions. The goal is to handle the grid as eight separate chunks, each of which can be evaluated. I cover this process in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Separating parts of a grid by color. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7508\">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-7508","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\/7508","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=7508"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7508\/revisions"}],"predecessor-version":[{"id":7532,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7508\/revisions\/7532"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}