Why run time error


#1

vector<vector > Solution::generateMatrix(int A)
{
vector<vector>res;
int k=0,l=0,m=A,n=A;
int i;
int num=1;
int index=0;
for(int i=0;i<A;i++)
{
for(int j=0;j<A;j++)
{
res[i][j]=0;
}
}
while(k<m && l<n && num<=(A*A))
{
for(i=l;i<n;i++)
{
res[k][i]=num;
num++;
}
k++;
for(i=k;i<m;i++)
{
res[i][n-1]=num;
num++;
}
n–;
if(k<m)
{
for(i=n-1;i>=l;i–)
{
res[m-1][i]=num;
num++;
}
m–;
}
if(l<n)
{
for(i=m-1;i>=k;i–)
{
res[i][l]=num;
num++;
}
l++;
}
}
return res;

}


#2

while initialising res, you need to declare the size as well. eg.

vector<vector<int>> res(n,vector<int>(n);