{"id":7491,"date":"2026-04-04T00:01:28","date_gmt":"2026-04-04T07:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7491"},"modified":"2026-04-04T10:02:53","modified_gmt":"2026-04-04T17:02:53","slug":"working-with-data-chunks","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7491","title":{"rendered":"Working with 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 \/>\nI 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.<br \/>\n<!--more--><br \/>\nThis 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.<\/p>\n<p>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&#8217;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.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_04_04-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_04_04-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define ROWS 17\r\n#define COLUMNS 17\r\n#define AREA ROWS*COLUMNS\r\n\r\nint main()\r\n{\r\n    unsigned char grid[AREA];\r\n    int x,y,min,max;\r\n    unsigned long total,average;\r\n\r\n    <span class=\"comments\">\/* seed randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    max = 0;\r\n    min = 255;\r\n    total = 0;\r\n    for( x=0; x&lt;AREA; x++ )\r\n    {\r\n        grid[x] = rand()%256;    <span class=\"comments\">\/* range is 0 through 255 *\/<\/span>\r\n        max = grid[x]&gt;max ? grid[x] : max;\r\n        min = grid[x]&lt;min ? grid[x] : min;\r\n        total += grid[x];\r\n    }\r\n\r\n    for( y=0; y&lt;ROWS; y++ )\r\n    {\r\n        for( x=0; x&lt;COLUMNS; x++ )\r\n            printf(\"%02X \",grid[ y*COLUMNS + x ]);\r\n        putchar('\\n');\r\n    }\r\n    printf(\"min = %d\\nmax=%d\\n\",min,max);\r\n    printf(\"total is %ld\\n\",total);\r\n    average = total\/(AREA);\r\n    printf(\"average is %lu\/%d = %lu\\n\",total,AREA,average);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Two defined constants set the grid size, <code>ROWS<\/code> and <code>COLUMNS<\/code>. The grid area is calculated by defined constant <code>AREA<\/code>, which multiplies the other two constants. This approach works, but it&#8217;s not without posing a C language weakness that I&#8217;ll describe later.<\/p>\n<p>After the randomizer is seeded, values for <em>int<\/em> variables <code>max<\/code>, <code>min<\/code>, and <code>total<\/code> are set:<\/p>\n<p><code>max = 0;<br \/>\nmin = 255;<br \/>\ntotal = 0;<\/code><\/p>\n<p>Variables <code>max<\/code> and <code>min<\/code> keep track of the maximum and minimum values generated. Initializing their starting values (zero for <code>max<\/code> and 255 for <code>min<\/code>) helps with the tracking. Variable <code>total<\/code> tallies all the values for use in the average computation.<\/p>\n<p>A nested <em>for<\/em> loop outputs the grid based on the <code>ROWS<\/code> and <code>COLUMNS<\/code> constants. Then a series of <em>printf()<\/em> statements output the data summary. The mean is calculated with this statement:<\/p>\n<p><code>average = total\/(AREA);<\/code><\/p>\n<p>Defined constant <code>AREA<\/code> <span style=\"text-decoration:underline\">must<\/span> be enclosed in parentheses, otherwise the average (mean) is improperly calculated. Remember that <code>AREA<\/code> expands to <code>ROWS*COLUMNS<\/code>, which expands to <code>17*17<\/code>. Without the parentheses, the statement is read by the compiler as:<\/p>\n<p><code>average = total\/17*17;<\/code><\/p>\n<p>The result  of this calculation isn&#8217;t the average. Therefore, parentheses are required. This problem showcases why using defined constants (or macros) can be risky in C programming.<\/p>\n<p>Here is output from a sample run:<\/p>\n<pre>0F 97 3E 16 74 20 61 01 12 70 16 59 B2 2F 52 93 4A\r\n18 31 A1 24 71 F2 6E 50 A7 0D 73 5E C3 93 6E 5A D1\r\n84 CE F1 E6 CF 04 56 E5 5D 08 15 B0 9B 5F C8 CC 00\r\nEC 3D F2 5A 8E 9A 67 01 F8 2A 94 66 84 65 EB 53 56\r\nD1 22 5A 27 08 B8 30 1D 68 CB 7C 30 98 7C 1D D5 6E\r\n77 63 08 DF 64 01 09 F8 67 8E 5D 52 E1 B4 23 03 0E\r\n4B 0B C6 7B 28 2E 46 A4 5F DE 20 7C B4 8F F3 17 97\r\nD2 7C 98 DC 74 00 6A D2 52 4B 86 76 4E 94 C1 5A 5B\r\n3C 82 89 82 27 E8 61 47 64 15 D6 58 2C 6E 2A A8 06\r\n06 1D 06 70 EF 59 BB 75 CF 0A 09 90 64 64 CC E6 EE\r\n4E 0D D6 AF 55 3B C4 2B 93 F1 99 BD 99 A0 C4 B6 A6\r\n34 A5 FF F0 1A CE FA 24 5E 5E 88 2A 44 76 79 52 4D\r\n28 A7 88 ED D2 1B DE 6C D8 77 0C 9C 2E B2 D1 D3 B2\r\nC1 EE 80 BB 12 DF 19 9A 09 5D 11 82 AF 5E AB 56 E6\r\n98 29 01 76 95 D9 ED A1 76 1B 53 47 EF 05 08 DD 86\r\nC3 EF 65 DC 89 6E 39 9A F1 E9 F8 9C 3F DE 34 68 DF\r\nAA FD B9 97 9E 2F B3 F2 76 A2 F7 7E 7F 7D 41 6E E2\r\nmin = 0\r\nmax=255\r\ntotal is 35531\r\naverage is 35531\/289 = 122<\/pre>\n<p>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&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More Grid Madness! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7491\">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-7491","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\/7491","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=7491"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7491\/revisions"}],"predecessor-version":[{"id":7533,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7491\/revisions\/7533"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}