Extracting Data Chunks


The next step in dividing a grid of data into chunks is to extract each of the chunks. Last week’s Lesson used color to visually identify the data chunks. This week’s update to the code pulls these chunks from the grid, using their values and colors to confirm that everything matches up properly.

My first update is to change the colors. Honestly, it’s easier to deal with four colors as opposed to doing the math to maintain the eight colors used in last week’s Lesson. My choice is to set the four colors are foreground and background, which is technically eight color combinations. So I’m good.

Extracting the chunks requires a nested loop inside a nested loop. The outer nest works through the main grid, array grid[] 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.

Here is the full code:

2026_04_18-Lesson.c

#include <stdio.h>

#define ROWS 16
#define COLUMNS 16
#define AREA ROWS*COLUMNS
#define NORMAL "\e[m"

int main()
{
    unsigned char grid[AREA];
    int x,y,v,h,row,col;
    int chunk_horz,chunk_vert,grid_width,grid_height;

    for( y=0; y<ROWS; y++ )
    {
        for( x=0; x<COLUMNS; x++ )
        {
            grid[y*COLUMNS+x] = (y*COLUMNS)+x;
            if( y<ROWS/2 )
                printf("\e[3%cm%02X%s ",
                        (x/4+1)+'0',
                        grid[y*COLUMNS+x],
                        NORMAL
                      );
            else
                printf("\e[4%c;30m%02X%s ",
                        (x/4+1)+'0',
                        grid[y*COLUMNS+x],
                        NORMAL
                      );
        }
        putchar('\n');
    }

    /* split into chunks */
    chunk_horz = 4;
    chunk_vert = 8;
    grid_height = ROWS/chunk_vert;
    grid_width = COLUMNS/chunk_horz;

    /* part out the chunks */
    for( y=0; y<grid_height; y++ )
    {
        for( x=0; x<grid_width; x++ )
        {
            printf("Chunk %d:%d\n",y,x);
            for( v=0; v<chunk_vert; v++ )
            {
                for( h=0; h<chunk_horz; h++ )
                {
                    row = y*chunk_vert+v;
                    col = x*chunk_horz+h;
                    if( y<grid_height/2 )
                        printf("\e[3%cm%02X%s ",
                                (col/4+1)+'0',
                                grid[row*COLUMNS+col],
                                NORMAL
                              );
                    else
                        printf("\e[4%c;30m%02X%s ",
                                (col/4+1)+'0',
                                grid[row*COLUMNS+col],
                                NORMAL
                              );
                }
                putchar('\n');
            }
        }
    }

    return 0;
}

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.

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:

grid[y*COLUMNS+x] = (y*COLUMNS)+x;

The color calculation is made with this expression: (x/4+1)+'0'. The value is set as either the foreground or background color, depending on the value of variable y. Figure 1 shows output from this part of the program.

Colorful grid in a terminal window.

Figure3 1. The grid output from the first nested loop.

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’s important, to ensure that they match. But the colors help to visually identify the chunks, even though these colors aren’t necessary for the result I’m after.

The outer nested loop uses the grid_height and grid_width variables to navigate the values in grid[] by chunk. The inner loop uses variables v and h for the specific chunk sizes set in variables chunk_vert and chunk_horz.

Variables row and col help calculate the offsets for each chunk within the larger grid:

row = y*chunk_vert+v;
col = x*chunk_horz+h;

This expression grid[row*COLUMNS+col] calculates the offset within the grid, which is less complex thanks to separating the values for row and col.

Figure 2 shows the output from this part of the code, which is quite tall but I split it into two columns:

colorful text output on a terminal (two columns)

Figure 2. The second part of the program’s output (split into two columns)

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’t involve three feet of output) is to calculate the averages for each data chunk. I present this update in next week’s Lesson.

Leave a Reply