First in C++, the size of an array must be a compile-time constant.So, take for example the following code snippets:
int n = 10;
int arr[n]; //INCORRECT because n is not a constant expression
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, the following(which you did in your code example) is incorrect:
int dim1, dim2;
cout << "Enter first dimension: ";
cin >> dim1;
cout << endl << "Enter second dimension: ";
cin >> dim2;
int arr[dim1][dim2];//INCORRECT
You should instead use std::vector<>
as shown below:
#include <iostream>
#include <vector>
int main()
{
int dim1, dim2;
std::cout << "Enter first dimension: ";
std::cin >> dim1;
std::cout << std::endl << "Enter second dimension: ";
std::cin >> dim2;
std::vector<std::vector<int>> arr(dim1, std::vector<int>(dim2));
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cin >> arr[i][j];
}
}
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}