It is not hard to dynamically create and initialize a 2d array in c++. we need to create an array that holds pointers. Let's see in the below code snippet how I made a 2d array that holds integers.
This example code creates a 2d array that holds values of a squared matrix.
#include <iostream> using namespace std; int main() { int dimen; cout << "Enters the matrix dimen : " << endl; // squared matrix cin >> dimen; int** arr = new int*[dimen * dimen]; cout << "enter row values line by line" << endl; for (int i = 0; i < dimen; i++) { arr[i] = new int[dimen]; for (int j = 0; j < dimen; j++) { cin >> arr[i][j]; } } // output cout << "Matrix ->" << endl; for (int i = 0; i < dimen; i++) { cout << "| "; for (int j = 0; j < dimen; j++) { cout << arr[i][j] << " "; } cout << "|" << endl; } // release for (int i = 0; i < dimen; i++) { delete[] arr[i]; } delete[] arr; }