int row,col;
cout << "please input how many rows and columns you want accordingly: ";
cin>>row>>col;
//create array in heap.
int **arr=new int*[row];
for(int i=0;i<row;i++)
{
arr[i]=new int[col];
}
//getting value from user.
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<"Enter a number ";
cin>>arr[i][j];
}
}
//display Elements.
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<arr[i][j]<<" ";
}
}
return 0;
int a[2][3]= {
{1, 2, 3},
{4, 5, 6}
};
cout << a[1][1]; // Output is 5
// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 3;
int m = 4;
/*
We create a 2D vector containing "n"
elements each having the value "vector<int> (m, 0)".
"vector<int> (m, 0)" means a vector having "m"
elements each of value "0".
Here these elements are vectors.
*/
vector<vector<int>> vec( n , vector<int> (m, 0));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cout << vec[i][j] << " ";
}
cout<< endl;
}
return 0;
}
// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 3;
int m = 4;
/*
We create a 2D vector containing "n"
elements each having the value "vector<int> (m, 0)".
"vector<int> (m, 0)" means a vector having "m"
elements each of value "0".
Here these elements are vectors.
*/
vector<vector<int>> vec( n , vector<int> (m, 0));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cout << vec[i][j] << " ";
}
cout<< endl;
}
return 0;
}
//* 2D arrays
int n, m;
cout << "Enter row and column: ";
cin >> n >> m;
int arr[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
}
}
cout << "
Showing array
";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}