{"id":476,"date":"2014-03-01T00:01:13","date_gmt":"2014-03-01T08:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=476"},"modified":"2014-02-15T09:30:30","modified_gmt":"2014-02-15T17:30:30","slug":"the-multi-dimensional-array-grid","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=476","title":{"rendered":"The Multi-Dimensional Array Grid"},"content":{"rendered":"<p>Data is is just data. Organize that data and it becomes information. When it comes to organizing data in the C language, the first tool you probably learned was the array.<br \/>\n<!--more--><br \/>\nAn array is just a collection of similar data types, one long parade of variables.<\/p>\n<p>Arrays are classified by their dimensions. A single-dimension array, which is really just called an <em>array<\/em>, is a long list of values. A two-dimension array is a grid, like a table. The values can be referenced by row and column. Finally, a three-dimension array is a cube, with rows, columns, and layers.<\/p>\n<blockquote><p>I suppose you could have four-dimensional arrays, which would be a series of three dimensional arrays, like a cube of cubes.<\/p><\/blockquote>\n<p>Internally, an array is just a series of variables. Array notation helps a programmer keep all that information organized in one, two, or three dimensions. It provides a handy reference so you don&#8217;t have to do a lot of array math in your head.<\/p>\n<p>Printing a one- or two-dimensional array is fairly easy. A single row or multiple rows of data is something programmers can easily code and something users can understand when they see it. A three-dimensional array, however, requires some finesse when printing. It&#8217;s not specifically that you have to concern yourself with the code, it&#8217;s the presentation.<\/p>\n<p>For this Exercise, you job is to properly display a three-dimensional array. The array contains the integer values 0 through 80 organized into nine grids of 3-by-3 squares. Here&#8217;s how that variable is created:<\/p>\n<p><code>int array[9][3][3];<\/code><\/p>\n<p>That declaration sets aside storage for 9 3-by-3 grids. Each grid is filled with the nine values, row by row. So, for example, the first grid would look like this:<\/p>\n<pre><code> 0  1  2 \r\n 3  4  5 \r\n 6  7  8<\/code><\/pre>\n<p>You&#8217;re exercise this month is to display the nine grids properly. Use this skeleton to get started:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int array[9][3][3];\r\n    int a,b,c,i;\r\n\r\n\/* Initialize the multidimensional array *\/\r\n    i = 0;\r\n    for(a=0;a&lt;9;a++)\r\n        for(b=0;b&lt;3;b++)\r\n            for(c=0;c&lt;3;c++)\r\n                array[a][b][c] = i++;\r\n\r\n\/* Display the multidimensional array *\/\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The multiple <em>for<\/em> loops in the code fill the three-dimensional array. It&#8217;s done in a specific manner, therefore your task is to display the grid in the same manner.<\/p>\n<p>As an example, this output is wrong:<\/p>\n<pre><code><span style=\"color:red\"> 0  1  2 \t 3  4  5 \t 6  7  8 \t\r\n 9 10 11 \t12 13 14 \t15 16 17 \t\r\n18 19 20 \t21 22 23 \t24 25 26 \t\r\n\r\n27 28 29 \t30 31 32 \t33 34 35 \t\r\n36 37 38 \t39 40 41 \t42 43 44 \t\r\n45 46 47 \t48 49 50 \t51 52 53 \t\r\n\r\n54 55 56 \t57 58 59 \t60 61 62 \t\r\n63 64 65 \t66 67 68 \t69 70 71 \t\r\n72 73 74 \t75 76 77 \t78 79 80<\/span><\/code><\/pre>\n<p>It may look correct, but when you review the code, and observe how each of the 3-by-3 grids are filled, you&#8217;ll understand that the output should really look like this:<\/p>\n<pre><code> 0  1  2 \t 9 10 11 \t18 19 20 \r\n 3  4  5 \t12 13 14 \t21 22 23 \r\n 6  7  8 \t15 16 17 \t24 25 26 \r\n\r\n27 28 29 \t36 37 38 \t45 46 47 \r\n30 31 32 \t39 40 41 \t48 49 50 \r\n33 34 35 \t42 43 44 \t51 52 53 \r\n\r\n54 55 56 \t63 64 65 \t72 73 74 \r\n57 58 59 \t66 67 68 \t75 76 77 \r\n60 61 62 \t69 70 71 \t78 79 80<\/code><\/pre>\n<p>As usual, please don&#8217;t peek at my solutions before you give this one a stab. I present two methods of solving this puzzle. Doubtless more exist. Have fun!<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/03\/03exercise.c\">Exercise Solution #1<\/a><\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/03\/03exercise-tighter.c\">Exercise Solution #2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Displaying a three dimensional array is one thing. Displaying it properly is something completely different. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=476\">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":[3],"tags":[],"class_list":["post-476","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/476","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=476"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":585,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions\/585"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}