Coders Packet

Clockwise/Anti-Clockwise Spiral Matrix in C++

By Timsal Zehra Rizvi

Hello Everyone! In this article, we will learn how to unwind and print spiral matrices in both clockwise and anti-clockwise direction using C++

About:

The program will ask the user to enter the number of rows, columns, and a 2D array. Our task would be to unwind the spirality of the 2D array and print the elements in a single-dimensional array. We will run four loops which will signify the direction to unwind spirality and will set conditions to print the remaining rows and columns.

A)To print in Clockwise Direction:

Code :

#include 
using namespace std;

int main() {
  int n,m,k=0,l=0,i,j;
  cin>>m>>n;
  int a[m][n];
  for(i=0;i<m;i++){
  for(j=0;j<n;j++){
      cin>>a[i][j];
  }
  }
  while(k=l;--i){
              cout<<a[m-1][i]<<" ";
          }
          m--;
      }
      if(l<n){
          for(i=m-1;i>=k;--i){
              cout<<a[i][l]<<" ";
          }
          l++;
      }
  }
  return 0;
}

 

Explanation:

Spiral Clockwise Explanation

To get the above output first declare variables as m(ending row index),n(ending columns column index) , k (starting row index) and l (starting column index). Variables k and l will be the starting row and column index respectively. Next print the first row and increase the starting row index similarly print the last column and decrease the ending column index, the increase and decrease of the indices signify that for the next round we have to print the remaining rows and columns. Now again print the last row in reverse order and the first column in reverse order adjusting the indexes so that the remaining rows and columns get printed. We will run a total of four loops to trace the clockwise path.

B)To print in Anti-Clockwise Direction:

Code :

#include 
using namespace std;

int main() {
  int r,c,k=0,l=0,i,j;
  cin>>r>>c;
  int a[r][c];
  for(i=0;i<r;i++){
  for(j=0;j<c;j++){
      cin>>a[i][j];
  }
  }
  while(k=k;--i){
              cout<<a[i][c-1]<<" ";
          }
          c--;
      }
      if(k<r){
          for(i=c-1;i>=l;--i){
              cout<<a[k][i]<<" ";
          }
          k++;
      }
  }
  return 0;
}

 

Explanation:

Spiral Anticlockwise explaination

 

Following the same approach as we did in the previous code, we will declare and initialize the variables but will follow a counter-clock path this time. First declare variables r (ending row index), c(ending column index), k (starting row index), l (starting column index) . Check if the starting indices are less than the ending row indices, now print the First column and then print the last row, increment the starting column index so that no element gets repeated. Similarly, we will run a total of four loops to follow the anti-clockwise direction.

 

This brings us to the end of the article.

Hope the code helps you.

Download project

Reviews Report

Submitted by Timsal Zehra Rizvi (Zehra)

Download packets of source code on Coders Packet