How to pass a 2D array as a parameter in C++

Hi guys, today we will see how to pass a 2D array as a parameter in C++.

Before moving to the topic let us discuss what is a 2D array.

2D Array: It can be defined as an array of arrays. Or in other words, we can say that arrays are stored as an element in an array. You will understand it better if you consider it as a matrix of ‘n’ rows and ‘m’ columns.

Methods To Declare 2D Array

We can declare the 2D array in 2 ways:

  • Static memory allocation: When an array is created at compile-time, it is referred to as a static array.
  • Dynamic memory allocation: When an array is created at run time, it is called a dynamic array.

In static array, we need to specify the size of the array but in the dynamic array, we can ask the size from the user.
So, this is the advantage of using the dynamic array that no space/memory is wasted.

In STATIC, we declare an array as:

int arr[5][5];

So, the array can take values less than or equal to 5 for the number of rows and columns.

In DYNAMIC, we use the new operator to declare an array:

int ** arr= new int * [n];
for(int i=0;i<n;i++)
{
        arr[i]= new int [m];
}

The value of n and m will be given by the user.

Whenever we declare an array using a new operator, we should delete that array using the delete operator. It is not necessary but it is a good practice. This is done to prevent the system from a memory leak.

So, we delete the array like this:

for(int i=0;i<n;i++)
        delete [] arr[i];
    delete [] arr;

How to pass a 2D array as a parameter

We can pass the array in 2 ways:

  • When we are using the static array
    #include<bits/stdc++.h>
    using namespace std;
    void print(int arr[][10], int n,int m)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                cout<<arr[i][j]<<" ";
            cout<<endl;
        }
    }
    int main()
    {
        int n,m,arr[10][10];
        cin>>n>>m;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                cin>>arr[i][j];
        print(arr,n,m);  // This statement will pass the array as parameter.
        return 0;
    }
    

In this code, the user will enter the number of rows and columns but it should be less than or equal to 10 because I declare the array with the size of 10 x 10.

After entering the size, the user will enter the elements of that matrix.
The program will print the elements in the matrix form.

Let us assume that the input entered by the user is:

3 3
1 2 3 4 5 6 7 8 9

So, its output will be:

1 2 3
4 5 6
7 8 9
  • When we are using the dynamic array
    #include<bits/stdc++.h>
    using namespace std;
    void print(int **arr,int n,int m)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                cout<<arr[i][j]<<" ";
            cout<<endl;
        }
    }
    int main()
    {
        int n,m;
        cin>>n>>m;
        int **arr=new int*[n];
        for(int i=0;i<n;i++)
        {
            arr[i]=new int[m];
            for(int j=0;j<m;j++)
                cin>>arr[i][j];
        }
        print(arr,n,m);  //This statement will pass the array as parameter.
        for(int i=0;i<n;i++)
            delete [] arr[i];
        delete [] arr;
        return 0;
    }
    

So, in this program, we will ask the user to enter the number of rows and columns as an input and then we will create a 2D array of that size.

After that, the user will enter the elements of that matrix.
The program will print the elements in the form of a matrix.

At last, we are deallocating the memory or deleting the array from the memory.

Let us assume that the input entered by the user is:

2 3
1 2 3 4 5 6

The output will be:

1 2 3
4 5 6

Here is a sample program that will help you to understand 2D array better. Program

Leave a Reply

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