Inverse spiral matrix clockwise and counter-clockwise route

Viewed 20

I would like to achieve clockwise and counter clockwise inverse spiral route of a 12*12 matrix. I have obtained the counter clockwise route but I have problem to do it clockwise.

Here is the code for CCW inverse spiral in c++, how can I do the spiral in CW direction? I would like to know how to modify the for loops to get my result?

Thanks!

    // This is a modified code of
    // https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
    #include <iostream>
    #define R 12
    #define C 12
    using namespace std;
    
    // Function that print matrix in reverse spiral form.
    void ReversespiralPrint(int m, int n, int a[R][C])
    {
    // Large array to initialize it
    // with elements of matrix
    long int b[144];
    
    // k - starting row index
    // l - starting column index
    int i, k = 0, l = 0;
    
    // Counter for single dimension array
    // in which elements will be stored
    int z = 0;
    
    // Total elements in matrix
    int size = m*n;
    
    while (k < m && l < n)
    {
    // Variable to store value of matrix.
    int val;
    
    /* Print the first row from the remaining rows */
    for (i = l; i < n; ++i)
    {
        // printf("%d ", a[k][i]);
        val = a[k][i];
        b[z] = val;
        ++z;
    }
    k++;

    /* Print the last column from the remaining columns */
    for (i = k; i < m; ++i)
    {
        // printf("%d ", a[i][n-1]);
        val = a[i][n-1];
        b[z] = val;
        ++z;
    }
    n--;

    /* Print the last row from the remaining rows */
    if ( k < m)
    {
        for (i = n-1; i >= l; --i)
        {
            // printf("%d ", a[m-1][i]);
            val = a[m-1][i];
            b[z] = val;
            ++z;
        }
        m--;
    }

    /* Print the first column from the remaining columns */
    if (l < n)
    {
        for (i = m-1; i >= k; --i)
        {
            // printf("%d ", a[i][l]);
            val = a[i][l];
            b[z] = val;
            ++z;
        }
        l++;
    }
    }
    for (int i=size-1 ; i>=0 ; --i)
    {
    cout << b[i] << "  ";
    }
    }

    /* Driver program to test above functions */
    int main()
    {
    int a[R][C] = { {  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 , 10 , 11 , 12 } , 
                { 24 , 23 , 22 , 21 , 20 , 19 , 18 , 17 , 16 , 15 , 14 , 13 } , 
                { 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 } , 
                { 48 , 47 , 46 , 45 , 44 , 43 , 42 , 41 , 40 , 39 , 38 , 37 } , 
                { 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 } , 
                { 72 , 71 , 70 , 69 , 68 , 67 , 66 , 65 , 64 , 63 , 62 , 61 } , 
                { 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 } ,
                { 96 , 95 , 94 , 93 , 92 , 91 , 90 , 89 , 88 , 87 , 86 , 85 } ,
                { 97 , 98 , 99 , 100, 101, 102, 103, 104, 105, 106, 107, 108} ,
                { 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109} ,
                { 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132} ,
                { 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133} };

    ReversespiralPrint(R, C, a);
    return 0;        
    }
0 Answers
Related