Doubt doubt doubt

#include
#include
using namespace std;

// Spiral print:

void spiralprint(int arr[100][100], int r, int c)
{
int scol = 0;
int ecol = c - 1;
int srow = 0;
int erow = r - 1;

// Below while condition will work perfectly square matrices but not when number no. rows are odd.
while (erow >= srow and ecol >= scol)
{
    // first row
    for (int i = scol; i <= ecol; i++)
    {
        cout << arr[srow][i] << ", ";
    }
    srow++;

    // last column
    for (int i = srow; i <= erow; i++)
    {
        cout << arr[i][ecol] << ", ";
    }
    ecol--;

    // last row
    if (ecol > scol)
    {
        for (int i = ecol; i >= scol; i--)
        {
            cout << arr[erow][i] << ", ";
        }
    erow--;
    }

    // first column
    if (erow > srow)
    {
        for (int i = erow; i >= srow; i--)
        {
            cout << arr[i][scol] << ", ";
        }
        scol++;
    }
}

}

int main()
{
int arr[100][100];
int r, c;
int val = 1;
cout << “Enter the no. of rows and columns:” << endl;
cin >> r >> c;
cout << “Your 2D-array:” << endl;
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
arr[row][col] = val;
cout << setw(3) << arr[row][col] << " ";
val++;
}
cout << endl;
}

cout << "Spiral Print:" << endl;
spiralprint(arr, r, c);

return 0;

}


not working for 3 and 5.
Reason???

hello @parth_tyagi

use >= in place of >

if i use >= in place of >, then what’s the point of adding the if statement???

ur first two loop are modifying
srow and ecol.
so when u come to third or fourth row then u need to make sure that the condition between row and col holds true

Still not working…bhaiya can you modify my code and send me the screen me the screen shot of the part where to make changes.

pls save ur code in cb ide and share its link with me .

check now->

bhaiya problem ye nahi thi ki mene “>=” use nahi kiya…instead…mene jo if statements likhi h unhe ek dusre se replace karna tha. Aur agr hum “>” ko isse “>=” replace karenge to if satement lagane ka koi logic he nahi banega. Humne if

check my last response, already shared the updated code, and mentioned the changes in comments.

do a dry run >= condition is necessary

Achha…samajh gaya. “>=” nahi lagaenge to kuch elements print nahi honge.

Achha bhaiya…mujhe kitna padna chaiye course me se so that ki august tak complete ho jae couse. Or kya august tak ho sakta h?? Kyuki me chahta hu first year me DSA complete ho jae.

pura course hi padho , sab important hai

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.