Printing matrix (2D array) in spiral order

Given a matrix (2D array) of m x n elements (m rows, n columns), write a function that prints the elements in the array in a spiral manner.

Sponsored: has published a chatbot, who can chat with you and help you find apartments in San Francisco and Bay area. Check it out:

01 void spiralPrint(vector<int *> A,int left, int right, int low, int high)
02 {
03     if((left>right)||(low>high))
04         return;
05     for(int i=left;i<=right;i++)
06         cout<<A[low][i]<<” “;
07     for(int i=low+1;i<=high;i++)
08         cout<<A[i][right]<<” “;
09     if(low!=high){
10         for(int i=right1;i>=left;i)
11             cout<<A[high][i]<<” “;
12     }
13     if(left!=right){
14         for(int i=high1;i>=low+1;i)
15             cout<<A[i][left]<<” “;
16     }
17     spiralPrint(A,left+1,right1,low+1,high1);
18     return;
19 }
This entry was posted in Array and linked list, Recursive. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s