+ Reply to Thread
Results 1 to 2 of 2

Thread: Print matrix spirally

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    Print matrix spirally

    Print a matrix in spiral order

    e.g:
    1 2 3
    4 5 6
    7 8 9

    should be printed like
    1 2 3 6 9 8 7 4 5

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    Code:
    void PrintSpiralMatrix(int **matrix, int m_size)
    { 
    	int j = 0;
    	for (int i = m_size - 1; i >= 0; i--, j++)
    	{
    		for (int k = j; k < i; k++)
    			cout<<matrix[j][k];
                    for (int k = j; k < i; k++)
                    	cout<<matrix[k][i];
                    for (int k = i; k > j; k--)
    	                cout<<matrix[i][k];
                    for (int k = i; k > j; k--)
                    	cout<<matrix[k][j];
                }
                //print middle element if N is odd
                if (m_size % 2 == 1)
                {
                    cout<<matrix[(m_size - 1) / 2][(m_size - 1) / 2] );
                }
    	}
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts