Multidimensional Arrays

Multidimensional Arrays

Multidimensional arrays may be represented as “arrays of arrays”. as an example, a bi-dimensional array may be imagined as a bi-dimensional table made of components, all of them of a same uniform type.

jimmy represents a bi-dimensional array of three per five components of type int. The method to declare this array in C++ would be:

int jimmy [3][5];

and, as an example, the method to reference the second part vertically and fourth horizontally in an expression
would be:


(remember that array indices continually begin by zero).
Multidimensional arrays don’t seem to be restricted to 2 indices (i.e., 2 dimensions). they will contain as several indices as needed. however be careful! the number of memory required for an array swiftly will increase with every dimension. For example:

char century [100][365][24][60][60];

declares associate array with a char part for every second during a century, that’s over three billion chars. So this declaration would consume over three gigabytes of memory!
Multidimensional arrays square measure simply associate abstraction for programmers, since we are able to acquire constant results with a simple array simply by just putting a factor between its indices:

int jimmy [3][5]; // is equivalent to

int jimmy [15]; // (3 * 5 = 15)

With the sole distinction that with multidimensional arrays the compiler remembers the depth of every unlikely dimension. Take as example these 2 items of code, with each precisely the same result. One uses a bi-dimensional array and therefore the alternative one uses a straightforward simple array:

None of the 2 supply codes on top of turn out any output on the screen, however each assign values to the memory block known as jimmy within the following way:

We have used “defined constants” (#define) to alter potential future modifications of the program. for instance,
in case that we tend to determined to enlarge the array to a height of four rather than three it might be done just by dynamical the line:


#define HEIGHT 3


#define HEIGHT 4

And there is no need to do any changes in the whole code other than this.

PDF articles and manuals