Since this is tagged C++. I'd like to suggest usage of std::vector. It's dynamic container which is very useful. You can resize it, clear it fill it with ease. Once you understand it's basic usage, they would come really handy in your future C++ development. I modified your code slightly:
#include <iostream>
#include <vector>
using namespace std;
void read(vector<vector<int> >& arr,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>arr[i][j];
}
int main ()
{
int N;
cin>>N;
vector<vector<int> > arr(N, vector<int>(N));
read(arr, N);
}
They have many advantages over the primitive arrays like they can be initialized easily, suppose you want to initialize all to zero :
vector<vector<int> > arr(N, vector<int>(N, 0));
You don't have to worry about adding the array size whenever passing in functions. vector can easily handle this:
for(i = 0; i < arr.size(); i++) {
for(j = 0; j < arr[i].size(); j++) {
// do stuff
}
}
Moreover with the added methods of the Standard template library like fill
, swap
. Many operations can be handled easily.