Multidimensional Arrays In Cpp

Multidimensional arrays:

Multidimensional arrays can be described as “arrays of arrays”. For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type.

multidimentional array structure in c++

jimmy represents a bidimensional array of 3 per 5 elements of type int. The way to declare this array in C++ would be:

Syntax
int jimmy [3][5];

and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:

Syntax
jimmy[1][3]
Multidimensional arrays value selection in c++

(remember that array indices always begin by zero).

Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as many indices as needed. But be careful! The amount of memory needed for an array rapidly increases with each dimension.

For example :

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

declares an array with a char element for each second in a century, that is more than 3 billion chars. So this declaration would consume more than 3 gigabytes of memory!

Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices:

CODE/PROGRAM/EXAMPLE
int jimmy [3][5]; // is equivalent to
int jimmy [15]; // (3 * 5 = 15)

With the only difference that with multidimensional arrays the compiler remembers the depth of each imaginary dimension for us. Take as example these two pieces of code, with both exactly the same result. One uses a bidimensional array and the other one uses a simple array:

multidimensional array pseudo-multidimensional array
#define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT][WIDTH]; int n,m; int main () { for (n=0;n #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT * WIDTH]; int n,m; int main () { for (n=0;n

None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:

array assign values to the memory in c++

We have used “defined constants” (#define) to simplify possible future modifications of the program.

For example, in case that we decided to enlarge the array to a height of 4 instead of 3 it could be done simply by changing the line :

CODE/PROGRAM/EXAMPLE
#define HEIGHT 3
  to:
#define HEIGHT 4

with no need to make any other modifications to the program.

Arrays as parameters:

At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation.

In order to accept arrays as parameters the only thing that we have to do when declaring the function is to specify in its parameters the element type of the array, an identifier and a pair of void brackets [].

For example, the following function:

Syntax
void procedure (int arg[])

accepts a parameter of type “array of int” called arg. In order to pass to this function an array declared as:

CODE/PROGRAM/EXAMPLE
int myarray [40];

it would be enough to write a call like this:

Syntax
procedure (myarray);

Here you have a complete example:

CODE/PROGRAM/EXAMPLE
//arrays as parameters
#include <iostream>
using namespace std;
void printarray (int arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] << " ";
cout << "\n";
}
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0;
}

O/P : 5 10 15
2 4 6 8 10

As you can see, the first parameter (int arg[]) accepts any array whose elements are of type int, whatever its length.

For that reason we have included a second parameter that tells the function the length of each array that we pass to it as its first parameter. This allows the for loop that prints out the array to know the range to iterate in the passed array without going out of range.

In a function declaration it is also possible to include multidimensional arrays. The format for a tridimensional array parameter is:

Syntax
base_type[][depth][depth]

for example, a function with a multidimensional array as argument could be:

CODE/PROGRAM/EXAMPLE
void procedure (int myarray[][3][4])

Notice that the first brackets [] are left blank while the following ones are not. This is so because the compiler must be able to determine within the function which is the depth of each additional dimension.

Arrays, both simple or multidimensional, passed as function parameters are a quite common source of errors for novice programmers.

I recommend the reading of the chapter about Pointers for a better understanding on how arrays operate.

#multidimensional_arrays_in_c++ #multidimensional_arrays_in_cpp # _pseudo-multidimensional_array_in_c++ #Arrays_as_parameters_in_c++ #void_procedure_in_c++

(New page will open, for Comment)

Not yet commented...