Code not working despite the correct logic

I have used the same code that Kartik sir used in the video but it is faling one testcase.
Here’s the code: https://ide.codingblocks.com/s/644413
The code is failing for the 3rd inner loop.(sr<er). As when the input is m=2 and n=3 this case fails.

hi @mitrapranav0027_5d705c8b616e0add
u are printing additional x,y,z,w which are not expected in o/p… just remove then ur code will work fine and pass all test cases…
corrected code -->

Yeah my bad about that. I was doing that just to check where the error was. I have removed them and the error is still there.

The code: https://ide.codingblocks.com/s/644413

hi @mitrapranav0027_5d705c8b616e0add
ur code is correct… and I can see u have already successfully submitted the code and gained full points…

no I passed the testcases by removing the if statement on the 3rd loop. This code return fail for the 0th testcase.

@mitrapranav0027_5d705c8b616e0add

#include<iostream>
using namespace std;
void Clockwise(int a[][100],int m,int n) {
	int sc=0,er=m-1,sr=0,ec=n-1;
	while(sc<=ec && sr<=er) {
		//1st sr
		for(int col=sc;col<=ec;col++) {
			cout<<a[sr][col]<<", ";
		}
		sr++;
		//2nd ec:
		for(int row=sr;row<=er;row++) {
			cout<<a[row][ec]<<", ";
		}
		ec--;
		//3rd er
		if(sc<=ec) {
			for(int col=ec;col>=sc;col--) {
			cout<<a[er][col]<<", ";
			}
			er--;
		}
		
		//4th sc
		if(sc<=ec) {
			for(int row=er;row>=sr;row--) {
			cout<<a[row][sc]<<", ";
			}
			sc++;
		}
		
	}
	cout<<"END";
}
int main() {
	int a[100][100];
	int m,n;
	cin>>m>>n;
	for(int i=0;i<m;i++) {
		for(int j=0;j<n;j++) {
			cin>>a[i][j];
		}
	}
	Clockwise(a,m,n);
	return 0;
}

this code is passing all test cases… and even u have submitted this code and gained full score…

this code is not passing the 2nd testcase. The code I used to pass both the testcases was wrong as I had to remove the if statement on the 3rd loop but it would not work when sr=er. It will duplicate the results.

oh okay…
try this–>

#include<iostream>
using namespace std;
void Clockwise(int a[][100],int m,int n) {
	int sc=0,er=m-1,sr=0,ec=n-1;
	while(sc<=ec && sr<=er) {
		//1st sr
		for(int col=sc;col<=ec;col++) {
			cout<<a[sr][col]<<", ";
		}
		sr++;
		//2nd ec:
		for(int row=sr;row<=er;row++) {
			cout<<a[row][ec]<<", ";
		}
		ec--;
		//3rd er
		if(sr<=er) {
			for(int col=ec;col>=sc;col--) {
			cout<<a[er][col]<<", ";
			}
			er--;
		}
		
		//4th sc
		if(sc<=ec) {
			for(int row=er;row>=sr;row--) {
			cout<<a[row][sc]<<", ";
			}
			sc++;
		}
		
	}
	cout<<"END";
}
int main() {
	int a[100][100];
	int m,n;
	cin>>m>>n;
	for(int i=0;i<m;i++) {
		for(int j=0;j<n;j++) {
			cin>>a[i][j];
		}
	}
	Clockwise(a,m,n);
	return 0;
}

i tried to run this on all edge cases… it is running fine… if still there is any issue do let me know…

the symbols are changed here to < . Can you please send the code on the ide?

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.