Spiralprintclockwise((()))

What is the problem in this code
#include
using namespace std;
void Spiralprint(int a[][10], int r, int c)
{
int sr = 0, sc = 0, er = r - 1, ec = c - 1;
while (sr <= er and sc <= ec)
{
// 1. Print sr from sc to sc,sr++.
for (int col = sc; col <= ec; col++)
{
cout << a[sr][col] << ", ";
}
sr++;

    // 2. Print ec from sr to er, ec--.
    for (int row = sr; row <= er; row++)
    {
        cout << a[row][ec] << ", ";
    }
    ec--;

    // 3. Print er from ec to sc, er--.
    if (er > sr)
    {

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

    // 4. Print sc from er to sr, sc++.
    if (ec > sc)
    {
        for (int row = er; row >= sr; row--)
        {
            cout << a[row][sc] << ", ";
        }
        sc++;
    }
}

}

int main()
{
int r, c;
cin >> r >> c;
int a[r][10];
// int val = 1;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin>>a[i][j];
}
cout<<endl;
}

Spiralprint(a, r, c);
cout<<"END";
return 0;

}

hi @abhinavssr2003_eab0717682969e5c, just write >= in if conditions

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.

whyyy is it so…like why can’t we just write>
???

@abhinavssr2003_eab0717682969e5c, it will print in different order
Try for
2 3
1 2 3
4 5 6

1 2 3 6 4 5 END…it is replacing 4 and 5

i will try to dry run this…if i will get a doubt then i will definitely ask

1 Like