The code i guess is right>? why is it throwing error?

#include
using namespace std;
int main() {

int M,N;
cin>>M>>N;
int a[M][N];
for(int row=0;row<M;row++){
	for(int col=0;col<N;col++){
		cin>>a[row][col];
	}
}
int sc,sr,ec,er;
sc=sr=0;
ec=N-1;
er=M-1;
while(sc<=ec && sr<=er ){
	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(sr<er){
	for(int i=ec;i>=sc;i--)
	cout<<a[er][i]<<", ";
	er--;
	}

	if(sc<ec){
		for(int i=er;i>=sr;i--)
	cout<<a[i][sc]<<", ";
	sc++;}
}
cout<<"END";
return 0;

}

Hello @rounaqkhandelwal24,

Change your conditions,
if(sr<er){} => if(sr<=er){}
if(sc<ec){} => if(sc<=ec){}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.