Intro to Array data structure Part 2 – 2D Array

Continuing from the last post I am going to discuss more about the array data structure which is 2D array.
In this post, I will focus on the basic concept and usage.

What is 2D Array?

2D array is basically array within array.
Typically array contains specified data type in its element.
However, it is possible that the element can contain another array instead!

This is a 2D array with 3 rows and 5 columns.
In each row, there is an array of size 5 which is the column size.
In other words, there is an array of size 3 (row) and there is another array size 5 within each element.

Code Example

Here is a code example of usage of the 2D array.

This is a basic access usage example.

#include <iostream>

using namespace std;

int main()
{
    // declaration of 2D array without initializing elements
    int two_d_arr[3][5];
    
    // you can also initialize 2D array just like 1D array
    // you just need to make sure there is an array for each element.
    // 3 rows. Each row contains an array of size 5
    int two_d_arr2[3][5] = {{10, 15, 23, 31, 3}, {13, 72, 29, 19, 85}, {61, 42, 1, 5, 27}};
    
    // when initializing 2D array you don't need to specify row size
    // compiler will figure out row size for you as long as correct column size is provided
    // int two_d_arr2[][5] = {{10, 15, 23, 31, 3}, {13, 72, 29, 19, 85}, {61, 42, 1, 5, 27}};
    
    // you can use for loop to access 2D array
    // this will print out each column per row first
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 5; ++j)
        {
            // first bracket [i] represents row index
            // second bracket [j] represents column index
            cout << two_d_arr2[i][j] << " ";
        }
        
        cout << endl;
    }
    cout << endl;
    
    /*
     * output
     * 10 15 23 31 3
     * 13 72 29 19 85
     * 61 42 1  5  27
     */
    
    // or you can switch row and column if you want
    // this will print out each row per column first
    for (int i = 0; i < 5; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            // first bracket [i] represents row index
            // second bracket [j] represents column index
            cout << two_d_arr2[j][i] << " ";
        }
        
        cout << endl;
    }
    
    /*
     * output
     * 10 13 61
     * 15 72 42
     * 23 29 1
     * 31 19 5
     * 3  85 27
     */
    
    return 0;
}

Accessing 2D array is really like 1D array except you need to specify row and column index.

Here is another code example that 2D array is used as a function parameter.

#include <iostream>

using namespace std;

// array parameter needs to know column size. 
// but row size is still necessary as another parameter in order to loop it
void printArray(int arr[][5], int rowSize)
{
    for (int i = 0; i < rowSize; ++i)
    {
        for (int j = 0; j < 5; ++j)
        {
            cout << arr[i][j] <<  " ";
        }
        
        cout << endl;
    }
}

int main()
{
    int two_d_arr2[][5] = {{10, 15, 23, 31, 3}, {13, 72, 29, 19, 85}, {61, 42, 1, 5, 27}};
    
    printArray(two_d_arr2, 3);
    return 0;
}

Overall, it’s fairly simple to use 2D array that we just need to provide row and column index accordingly.
Sometimes you can skip row index when declaring 2D array with initialization or using as a function parameter.

Please note that you can have 3D array or more!
You just need to have a proper index when you access them.

Conclusion

We have taken a look at the basics of array data structure.
However, there are still more topics to discuss about array!
I will try to have another post about it.
You might also want to take a look at my post about linked list so you can have apple to apple comparison.

Leave a Reply

Your email address will not be published.