{"id":2498,"date":"2017-05-13T00:01:03","date_gmt":"2017-05-13T07:01:03","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2498"},"modified":"2017-05-20T07:15:06","modified_gmt":"2017-05-20T14:15:06","slug":"a-single-dimension-array-pretends-to-be-two-dimensions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2498","title":{"rendered":"A Single-Dimension Array Pretends to be Two-Dimensions"},"content":{"rendered":"<p>Multi-dimension array notation is just a handy shortcut for you, the human programmer. Internally, an array is a single-file line of values, one marching after another. The dimensional aspect helps humans organize the array&#8217;s data, but all that organization is superficial.<br \/>\n<!--more--><br \/>\nIn the following code, the <code>grid[]<\/code> array is declared with only one dimension, though it&#8217;s still a magic square. The manipulations performed use single-array calculations to pretend that two dimensions are present, yielding the same results as from a 2D array, but requiring a lot more math and overhead:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int grid[25] = {\r\n        25, 13,  1, 19,  7,\r\n        16,  9, 22, 15,  3,\r\n        12,  5, 18,  6, 24,\r\n         8, 21, 14,  2, 20,\r\n         4, 17, 10, 23, 11\r\n    };\r\n    int x;\r\n\r\n    puts(\"Magic Square\");\r\n    for(x=0;x&lt;25;x++)\r\n    {   \r\n        printf(\"  %2d\",grid[x]);\r\n        if(x%5 == 4)\r\n            putchar('\\n');\r\n    }\r\n\r\n    for(x=0;x&lt;5;x++)\r\n        printf(\"Sum of row %d: %d\\n\",\r\n                x+1,\r\n                grid[(x*5)+0]+\r\n                grid[(x*5)+1]+\r\n                grid[(x*5)+2]+\r\n                grid[(x*5)+3]+\r\n                grid[(x*5)+4]\r\n              );\r\n\r\n    for(x=0;x&lt;5;x++)\r\n        printf(\"Sum of column %d: %d\\n\",\r\n                x+1,\r\n                grid[0+x]+\r\n                grid[5+x]+\r\n                grid[10+x]+\r\n                grid[15+x]+\r\n                grid[20+x]\r\n              );\r\n\r\n    printf(\"Sum of diagonal NW-SE: %d\\n\",\r\n            grid[0]+\r\n            grid[6]+\r\n            grid[12]+\r\n            grid[18]+\r\n            grid[24]\r\n          );\r\n\r\n    printf(\"Sum of diagonal SW-NE: %d\\n\",\r\n            grid[4]+\r\n            grid[8]+\r\n            grid[12]+\r\n            grid[16]+\r\n            grid[20]\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code runs identically to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2484\">last week&#8217;s example<\/a>, but it&#8217;s a lot messier! I had to hand-code all those values to simulate the 2D array when adding rows, columns, and diagonals. That&#8217;s definitely not the point of programming. In fact it&#8217;s just silly because even a mediocre programmer would instinctively create a 2D array and use its nomenclature to perform the calculations.<\/p>\n<p>Still the point is that all arrays, no matter how you declare them, are stored internally as a single line of values. Multi-dimensional array notation is for your convenience and, I must add, to make your code more readable.<\/p>\n<p>So, everything is just wonderful! Except for a problem you&#8217;d only discover when you must send a multidimensional array to a function. In that situation, you must either continue to use a single-dimension array or resort to ugly <code>**<\/code> pointer notation.<\/p>\n<p><em>Fret not!<\/em><\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2505\">next week&#8217;s Lesson<\/a>, I demonstrate code that uses a function to validate a magic square grid without requiring pointer notation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The only difference in array dimensions is the math used to access the elements. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2498\">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-2498","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\/2498","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=2498"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2498\/revisions"}],"predecessor-version":[{"id":2525,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2498\/revisions\/2525"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}