i have written a code for 2d array which takes no. of rows and column as an input from user …and after that i have applied my code for printing elements of array in spiral way…i am writing my code down here please review it.
#include
using namespace std;
int main() {
int m,n;
int a[m][n];
int sr = 0;
int sc = 0;
int er = m-1;
int ec = n-1;
cout<<"Enter the no of rows in matrix : "<<endl;
cin>>m;
cout<<"Enter the no of colums in matrix : "<<endl;
cin>>n;
for(int row =0;row<m;row++)
{
for(int col = 0;col<n;col++)
{
cout<<"enter the element at row “<<row<<” and at “<<col<<” : “;
cin>>a[row][col];
}
}
while(sr <= er && sc<= ec)
{
for(int i = sc;i<=ec;i++)
{
cout<<a[sr][i]<<” “;
}
sr++;
for(int i = sr;i<=er;i++)
{
cout<<a[i][ec]<<” ";
}
ec–;
if(er>sr)
{
for(int i = ec;i>=sc;i–)
{
cout<<a[er][i]<< " “;
}
er–;
}
if(ec>sc)
{
for(int i = er;i>=sr;i–)
{
cout<<a[i][sc]<<” ";
}
sc–;
}
}
return 0;
}
