Array spiral print anti clockwise, what is wrong with this code

#include
#include
using namespace std;
int main() {
int a[10][10],M,N;
cin>>M>>N;
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
cin>>a[i][j];
}
}
/*
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
cout<<a[i][j];
}
cout<<endl;
}
*/
int fr,lr,fc,lc;
fr=0,fc=0;
lr=M-1,lc=N-1;
while(fr<=lr && fc<=lc){
for(int i=fr;i<=lr;i++){
cout<<a[i][fc]<<",";
}
fc++;
for(int j=fc;j<=lc;j++){
cout<<a[lr][j]<<",";
}
lr–;
for(int x=lr;x>=fr;x–){
cout<<a[x][lc]<<",";
}
lc–;
for(int y=lc;y>=fc;y–){
cout<<a[fr][y]<<",";
}
fr++;
}
cout<<“END”;

return 0;

}

1 Like

Hey Surya, can you share the link to your code.(copy your whole code on online ide save it and then share that link here)

https://ide.codingblocks.com/s/51750

Hey Surya, your code is correct but you output should exactly match the given output format, so print a space after “,” as given in the output format of the problem.

1 Like