SPIRAL PRINT question

one test case show error

#include
using namespace std;

void readmatrix(int arr[][10],int r,int c)
{
for(int i=0;i<r;++i)
{
for(int j=0;j<c;++j)
{
cin>>arr[i][j];
}
}
}

void sprialprint(int arr[][10],int r,int c)
{
int firstrow=0,lastrow=r-1,firstcol=0,lastcol=c-1;

while(firstrow<=lastrow && firstcol<=lastcol)
{
	//print first column
	for(int j=firstrow;j<=lastrow;++j)
	{
		cout<<arr[j][firstcol]<<", ";
	}
	++firstcol;

	//print last row
	for(int j=firstcol;j<=lastcol;++j)
	{
		cout<<arr[lastrow][j]<<", ";
	}
	--lastrow;

	//print last col
	if(firstcol<lastcol)
    {
        for(int j=lastrow;j>=firstrow;--j)
        {
            cout<<arr[j][lastcol]<<", ";
        }
        --lastcol;
    }

	//print first row
	if(firstrow<lastrow)
    {
        for(int j=lastcol;j>=firstcol;--j)
        {
            cout<<arr[firstrow][j]<<", ";
        }
        ++firstrow;
    }

}

}

int main() {
int m,n;
cin>>m>>n;

int arr[10][10];
readmatrix(arr,m,n);

sprialprint(arr,m,n);
cout<<"END";
return 0;

}

Hi @aattuull,
pls try sharing code using some other ide for d time being. ide.codingblocks.com is not working right now(its under repair).
sorry for the inconvenience caused.

i paste my code above

@aattuull use <= in if(firstcol<lastcol) and if(firstrow<lastrow).
if ‘=’ is not taken , it will fail on testcases like
2 6
1 2 3 4 5 6
7 8 9 10 11 12
Hope dis resolves ur doubt

1 Like