-1

the output order

So, i have to do a program where the user input nXn matrix (like 3x3, 4x4, etc.) first. And then the matrix. But the output must follow the order on 1 image.

so if the input like this (first line the n for nXn, and second line is the matrix):

3

6 6 9 

1 5 1 

2 7 7 

the output will be like this:

6 6 9 1 7 7 2 1 5 

please help me if you have a spare time :D TYSM!

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • in programming terms , the problem is an example of printing the matrix in spiral form . – Giriteja Bille Oct 05 '21 at 08:42
  • There are a number of posts about this with answer on the site. Search `"[c] matrix spiral form"` in the search box at the top of the page. – David C. Rankin Oct 05 '21 at 08:43
  • There can't be one specific previous discussion, as you haven't asked a question or shown what the specific problem is, but I linked a well-read question. Please note that I found it by typing "[c] matrix spiral" in the search box. Welcome to StackOverflow! May I suggest you take the [tour](https://stackoverflow.com/tour) and read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Weather Vane Oct 05 '21 at 08:50

1 Answers1

-1

Try this code :

void order(int m, int n, int a[x][y])
{
    int i, k = 0, l = 0;

    /*  m - ending row index
        n - ending column index
    */

    while (k < m && l < n)
    {

        for (i = l; i < n; ++i)
        {
            printf("%d ", a[k][i]);
        }
        k++;

        for (i = k; i < m; ++i)
        {
            printf("%d ", a[i][n-1]);
        }
        n--;

        if ( k < m)
        {
            for (i = n-1; i >= l; --i)
            {
                printf("%d ", a[m-1][i]);
            }
            m--;
        }

        if (l < n)
        {
            for (i = m-1; i >= k; --i)
            {
                printf("%d ", a[i][l]);
            }
            l++;
        }
    }
}