Working with Data Chunks


I recently embarked upon a programming project where I must reduce large chunks of data into more manageable pieces. The general topic is computer graphics, so a bit of data loss when reducing an image is expected. But before working with the graphical data itself, I decided to run a test on a random chunk of data to confirm whether I was on the right track.

This approach is how I go about tackling any large programming project: Break it out into tiny pieces to ensure that each piece does its job. The first piece is to generate a data grid to play with.

The following code works with a grid of a given size, populating it with byte values zero through 255. (These values mimic pixel colors.) As the grid’s values are generated, data is gathered to report the minimum and maximum values. These results are output, along with the mean (average) of all values.

2026_04_04-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWS 17
#define COLUMNS 17
#define AREA ROWS*COLUMNS

int main()
{
    unsigned char grid[AREA];
    int x,y,min,max;
    unsigned long total,average;

    /* seed randomizer */
    srand( (unsigned)time(NULL) );

    max = 0;
    min = 255;
    total = 0;
    for( x=0; x<AREA; x++ )
    {
        grid[x] = rand()%256;    /* range is 0 through 255 */
        max = grid[x]>max ? grid[x] : max;
        min = grid[x]<min ? grid[x] : min;
        total += grid[x];
    }

    for( y=0; y<ROWS; y++ )
    {
        for( x=0; x<COLUMNS; x++ )
            printf("%02X ",grid[ y*COLUMNS + x ]);
        putchar('\n');
    }
    printf("min = %d\nmax=%d\n",min,max);
    printf("total is %ld\n",total);
    average = total/(AREA);
    printf("average is %lu/%d = %lu\n",total,AREA,average);

    return 0;
}

Two defined constants set the grid size, ROWS and COLUMNS. The grid area is calculated by defined constant AREA, which multiplies the other two constants. This approach works, but it’s not without posing a C language weakness that I’ll describe later.

After the randomizer is seeded, values for int variables max, min, and total are set:

max = 0;
min = 255;
total = 0;

Variables max and min keep track of the maximum and minimum values generated. Initializing their starting values (zero for max and 255 for min) helps with the tracking. Variable total tallies all the values for use in the average computation.

A nested for loop outputs the grid based on the ROWS and COLUMNS constants. Then a series of printf() statements output the data summary. The mean is calculated with this statement:

average = total/(AREA);

Defined constant AREA must be enclosed in parentheses, otherwise the average (mean) is improperly calculated. Remember that AREA expands to ROWS*COLUMNS, which expands to 17*17. Without the parentheses, the statement is read by the compiler as:

average = total/17*17;

The result of this calculation isn’t the average. Therefore, parentheses are required. This problem showcases why using defined constants (or macros) can be risky in C programming.

Here is output from a sample run:

0F 97 3E 16 74 20 61 01 12 70 16 59 B2 2F 52 93 4A
18 31 A1 24 71 F2 6E 50 A7 0D 73 5E C3 93 6E 5A D1
84 CE F1 E6 CF 04 56 E5 5D 08 15 B0 9B 5F C8 CC 00
EC 3D F2 5A 8E 9A 67 01 F8 2A 94 66 84 65 EB 53 56
D1 22 5A 27 08 B8 30 1D 68 CB 7C 30 98 7C 1D D5 6E
77 63 08 DF 64 01 09 F8 67 8E 5D 52 E1 B4 23 03 0E
4B 0B C6 7B 28 2E 46 A4 5F DE 20 7C B4 8F F3 17 97
D2 7C 98 DC 74 00 6A D2 52 4B 86 76 4E 94 C1 5A 5B
3C 82 89 82 27 E8 61 47 64 15 D6 58 2C 6E 2A A8 06
06 1D 06 70 EF 59 BB 75 CF 0A 09 90 64 64 CC E6 EE
4E 0D D6 AF 55 3B C4 2B 93 F1 99 BD 99 A0 C4 B6 A6
34 A5 FF F0 1A CE FA 24 5E 5E 88 2A 44 76 79 52 4D
28 A7 88 ED D2 1B DE 6C D8 77 0C 9C 2E B2 D1 D3 B2
C1 EE 80 BB 12 DF 19 9A 09 5D 11 82 AF 5E AB 56 E6
98 29 01 76 95 D9 ED A1 76 1B 53 47 EF 05 08 DD 86
C3 EF 65 DC 89 6E 39 9A F1 E9 F8 9C 3F DE 34 68 DF
AA FD B9 97 9E 2F B3 F2 76 A2 F7 7E 7F 7D 41 6E E2
min = 0
max=255
total is 35531
average is 35531/289 = 122

Each time you run the program, different results are output, which is the effect of the random number generator. But my point is to extract chunks from this grid and display them separately for evaluation. I continue this process in next week’s Lesson.

2 thoughts on “Working with Data Chunks

  1. #define AREA ROWS*COLUMNS
    Thinks “I didn’t know you could do calculations in a #define”.

    average = total/(AREA);
    Thinks “ah, you can’t”.

    How about:
    #define AREA (ROWS*COLUMNS)?

Leave a Reply