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.
An array is just a collection of similar data types, one long parade of variables.
Arrays are classified by their dimensions. A single-dimension array, which is really just called an array, 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.
I suppose you could have four-dimensional arrays, which would be a series of three dimensional arrays, like a cube of cubes.
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’t have to do a lot of array math in your head.
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’s not specifically that you have to concern yourself with the code, it’s the presentation.
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’s how that variable is created:
int array[9][3][3];
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:
0 1 2
3 4 5
6 7 8
You’re exercise this month is to display the nine grids properly. Use this skeleton to get started:
#include <stdio.h>
int main()
{
int array[9][3][3];
int a,b,c,i;
/* Initialize the multidimensional array */
i = 0;
for(a=0;a<9;a++)
for(b=0;b<3;b++)
for(c=0;c<3;c++)
array[a][b][c] = i++;
/* Display the multidimensional array */
return(0);
}
The multiple for loops in the code fill the three-dimensional array. It’s done in a specific manner, therefore your task is to display the grid in the same manner.
As an example, this output is wrong:
0 1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80
It may look correct, but when you review the code, and observe how each of the 3-by-3 grids are filled, you’ll understand that the output should really look like this:
0 1 2 9 10 11 18 19 20
3 4 5 12 13 14 21 22 23
6 7 8 15 16 17 24 25 26
27 28 29 36 37 38 45 46 47
30 31 32 39 40 41 48 49 50
33 34 35 42 43 44 51 52 53
54 55 56 63 64 65 72 73 74
57 58 59 66 67 68 75 76 77
60 61 62 69 70 71 78 79 80
As usual, please don’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!