Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Multidimensional Arrays

C++ Programming Series: Multidimensional Arrays

In the last two previous posts, we discussed a very useful thing called ‘Arrays’. This topic is not yet completed and this post is going to end it! Multidimensional arrays!

Previously, we discussed 1D (or single-dimensional) arrays. They are pretty simple.

int arr[10];
arr[0] = 2;
arr[3] = 10;
arr[7] = 40;
//arr[10] is not possible, will give error!
cout << arr[0] + arr[3] + arr[7] << endl; //Outputs 52

But now, time for 2D and 3D arrays. We won’t go into details with 3D because they are like 2D, the understanding of the 2D array will give us the understanding of 3D arrays as well. Also, 3D arrays are rarely used.

int arr1d[3] = {1, 2, 3};

int arr2d[3][3] =
{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

int arr3d[3][3][3] =
{
    {
        {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},
    }
};

cout << "1D Array:-\n";
for (int x = 0; x < 3; x++)
    cout << arr1d[x] << " ";
cout << endl;

cout << "2D Array:-\n";
for (int y = 0; y < 3; y++)
{
    for (int x = 0; x < 3; x++)
    {
        cout << arr2d[y][x] << " ";
    }
    cout << endl;
}

cout << "3D Array:-\n";
for (int z = 0; z < 3; z++)
{
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            cout << arr3d[z][y][x] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

Dimensions are increased by putting another pair of square brackets. The number of elements in a multidimensional array will be the product of the values in each pair of square brackets that are typed in during the initialization of that array. For example, in the above cases, arr2d has 9 elements and arr3d has 27 elements. Below is how you can access elements of arr2d, individually.

//cout << arr2d[0][3] << endl; Gives error or garbage value
cout << arr2d[0][2] << endl; //Outputs 3
//cout << arr2d[3][0] << endl; Gives error or garbage value
cout << arr2d[2][0] << endl; //Outputs 7
cout << arr2d[1][1] << endl; //Outputs 5 (middle element)
cout << arr2d[2][2] << endl; //Outputs 9 (last element)
cout << arr2d[1][2] << endl; //Outputs 6
cout << arr2d[2][1] << endl; //Outputs 8

Remember, the array index number starts from 0 and not 1. The concept behind multidimensional arrays is the fact that each 2D array consists of 1D arrays, which contain elements and so, each 3D array consists of 2D arrays, which further, consists of 1D arrays, which contain elements. I know that sounds complicated!

//Number of 1D arrays present in the 2D array
//is the number typed in the 1st pair of square brackets
int arr2d_2[1][3] =
{
    { 1, 2, 3 } // -> Only one 1D array in a 2D array
};

The tricky question is, how to access all elements of multidimensional arrays with sizeof()? It is pretty simple but tricky! Simply, to get the total number of sub-arrays or max index value of the first pair of square brackets, divide the memory size of the whole multidimensional array with the memory size of sub-array. And then, to get the total number of elements in each sub-array, divide the memory size of a sub-array with the memory size of an element.

string machines[2][4] =
{
    { "Calculator", "Computer", "Printer", "Scanner" },
    { "Microwave Oven", "Blender", "Refrigerator", "Juicer" }
};

//Memory size of the whole array: sizeof(machines)
//Memory size of sub-array: sizeof(machines[0])
//Memory size of element: sizeof(machines[0][0])

//Total number of elements = sizeof(machines) / sizeof(machines[0][0])

//sizeof(machines) / sizeof(machines[0]) = 2
for (int y = 0; y < sizeof(machines) / sizeof(machines[0]); y++)
{
    //sizeof(machines[0]) / sizeof(machines[0][0]) = 4
    for (int x = 0; x < sizeof(machines[0]) / sizeof(machines[0][0]); x++)
    {
        cout << machines[y][x];

        //To avoid putting comma after the last element of each sub-array
        if (x != (sizeof(machines[0]) / sizeof(machines[0][0])) - 1)
            cout << ", ";
    }
    cout << endl;
}

I am pretty sure that it is complex. But if you code and practice on your own, you will found it to be easy! The good thing about 2D arrays is the fact that you can manage data in the form of tables! Try to make a table of numbers ranging from 0 to 100 where each sub-array or 1D array in the 2D array, will have 10 elements. It will be better if you use sizeof() for outputting the table.

That’s all! I hope you got it all right!

Leave a Reply

Your email address will not be published. Required fields are marked *