×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

Passing Dynamically Allocated Two dimensional Array to a Function [with C++ program]

Learn: How to pass dynamic 2D array to a user defined function? In this article, we’ll see different ways of accessing 2D array through a function.
Submitted by Abhishek Jain, on July 23, 2017

What is Two Dimensional Array?

The two dimensional array is the type of multidimensional array which is represented in the form of rows and columns, also known as matrix.

Dynamic allocation refers to the allocation of memory at runtime. Dynamic memory allocation allows your program to obtain more memory while execution, or to release it if it’s not required.

Passing Dynamically Allocated Two dimensional Array to a Function

Passing 2D array to a function requires two parameters rows and columns, which are runtime variables i.e. size of the 2D array (=rows*columns) decide/allocate at runtime.

Therefore to counter this situation we use pointers. Using pointer, it is easy to pass and access array through functions.

There are two ways to pass dynamic 2D array to a function:

1) Passing array as pointer to pointer( int **arr)

Using new operator we can dynamically allocate memory at runtime for the array. New operator returns the address of the space allocated .This method Passes array reference as double pointer to the function along with rows and columns.

Example

#include <iostream>
using namespace std;

// function to display 2D array
void show(int **arr, int x, int y) {
  int i, j;
  for (i = 0; i < x; i++) {
    for (j = 0; j < y; j++) {
      cout << arr[i][j] << " ";
    }
    cout << endl;
  }
}

// main program
int main() {
  int n, m;
  // input number of rows and columns
  cout << "Enter No. of rows: ";
  cin >> n;
  cout << "Enter No. of columns: ";
  cin >> m;

  // pointer to 2D array
  int **A = new int *[n];

  // pointer initialization
  for (int i = 0; i < n; i++) {
    A[i] = new int[m];
  }

  // input array elements
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> A[i][j];
    }
  }

  // display 2D array
  show(A, n, m);

  return 0;
}

Output

Enter No. of rows: 4
Enter No. of columns: 5
1 2 3 4 5 1 2 3 4 5 6 7 8 9 1 2 3 4 5 2 

1 2 3 4 5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 2

Passing array pointer(int *arr)

In this method we are passing array reference using pointer to the function and accessing 2D array as one dimensional array. Since 2D array stored in memory same as of one dimensional array.

Example

#include <iostream>
using namespace std;

// function to display 2D array
void show(int *arr, int x, int y) {
  int i, j, k = 0;

  for (i = 0; i < x; i++) {
    for (j = 0; j < y; j++) {
      cout << arr[k] << " ";
      k++;
    }
    cout << endl;
  }
}

// main program
int main() {
  int n, m;

  cout << "Enter No. of rows: ";
  cin >> n;
  cout << "Enter No. of columns: ";
  cin >> m;

  // 2D array declaration
  int A[n][m];

  // input array elements
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> A[i][j];
    }
  }

  // display 2D arrray
  show(*A, n, m);

  return 0;
}

Output

Enter No. of rows: 3
Enter No. of columns: 4
1 2 3 4 5 6 7 8 9 10 11 12

1 2 3 4
5 6 7 8
9 10 11 12

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.