One test case failed

Can you please tell me what is wrong in the code as one test case is failing
#include
#include
using namespace std;

void print2darray(int a[100][100],int n,int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<a[i][j]<<’ ';
}
cout<<endl;
}
}

int main(){
int n,m,num,sr,sc,er,ec,i,j,k,l,row,col;
cin>>n>>m;

int a[100][100] = {};
for(i=0;i<n;i++){
	for(j=0;j<m;j++){
		cin>>num;
		a[i][j] = num;
	}
}

sr = 0;
sc = 0;
er = n-1;
ec = m-1;

while(sr<=sc && sc<=ec){

	for(row=sr;row<=er;row++){
		cout<<a[row][sc]<<", ";
	}
	sc++;

	for(col=sc;col<=ec;col++){
		cout<<a[er][col]<<", ";
	}
	er--;

	if(sc<ec){

		for(row=er;row>=sr;row--){
			cout<<a[row][ec]<<", ";
		}
		ec--;
	}

	if(sr<er){

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

}

it should be sc<=ec

simillarly

this should also be sr<=er

But wouldn’t this print the values twice if of the same row??

I don’t think so
have you test the code after changes i told you

kindly share the link of code after changes
i will check it

it still shows that particular test case failed
#include
#include
using namespace std;

void print2darray(int a[100][100],int n,int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<a[i][j]<<’ ';
}
cout<<endl;
}
}

int main(){
int n,m,num,sr,sc,er,ec,i,j,k,l,row,col;
cin>>n>>m;

if(n>=1 && n<=10 && m>=1 && m<=10){

	int a[100][100] = {};
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin>>num;
			a[i][j] = num;
		}
	}

	sr = 0;
	sc = 0;
	er = n-1;
	ec = m-1;

	while(sr<=sc && sc<=ec){

		for(row=sr;row<=er;row++){
			cout<<a[row][sc]<<", ";
		}
		sc++;

		for(col=sc;col<=ec;col++){
			cout<<a[er][col]<<", ";
		}
		er--;

		if(sc<=ec){

			for(row=er;row>=sr;row--){
				cout<<a[row][ec]<<", ";
			}
			ec--;
		}

		if(sr<=er){

			for(col=ec;col>=sc;col--){
				cout<<a[sr][col]<<", ";
			}
			sr++;
		}
	}
	cout<<"END";
}
else{
	
}
return 0;

}

Modified Code

you while loop condition is also incorrect

1 Like

Oh sorry, I didn’t notice that.
Thanks very much sir

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.