| 6 | #define mod 1000000007 |
| 7 | #define endl '\n' |
| 8 | void spiral(int arr[][10],int n,int m){ |
| 9 | int startRow=0; |
| 10 | int endRow=n-1; |
| 11 | int startCol=0; |
| 12 | int endCol=m-1; |
| 13 | |
| 14 | //outer loop |
| 15 | while(startCol<=endCol and startRow<=endRow){ |
| 16 | //start row |
| 17 | for(int col=startCol;col<=endCol;col++){ |
| 18 | cout<<arr[startRow][col]<<" "; |
| 19 | } |
| 20 | //end col |
| 21 | for(int row=startRow+1;row<=endRow;row++){ |
| 22 | cout<<arr[row][endCol]<<" "; |
| 23 | } |
| 24 | //end row |
| 25 | for (int col=endCol-1;col>=startCol;col--) { |
| 26 | if(startRow==endRow){ |
| 27 | break; |
| 28 | } |
| 29 | cout<<arr[endRow][col]<<" "; |
| 30 | |
| 31 | } |
| 32 | for(int row=endRow-1;row>=startRow+1;row--){ |
| 33 | if(startCol==endCol){ |
| 34 | break; |
| 35 | } |
| 36 | cout<<arr[row][startCol]<<" "; |
| 37 | |
| 38 | } |
| 39 | startRow++; |
| 40 | startCol++; |
| 41 | endRow--; |
| 42 | endCol--; |
| 43 | } |
| 44 | } |
| 45 | int main(){ |
| 46 | int n=4; |
| 47 | int m=4; |