Create a Spiral Matrix by Taking Number of Rows and Columns as Input

This article provides an algorithm in a step-by-step approach to create a spiral matrix when the number of rows and columns in the matrix are given as input by the user. You are also provided with C# code snippets for easy reference when coding. I hope you will like it.

Background

The following is an example of a spiral matrix of a 3*3 matrix:

spiral matrix

So the entering of the numbers into the matrix takes place in the order 0->1->2->3 and repeat where:

    o: Represents left to right
    1: Represents up to down
    2: Represents right to left
    3: Represents down to up

     

This process repeats until all the elements in the matrix are filled.

Algorithm

  1. Take the inputs from the user and store them, for example

    row=3; column=3


  2. Create a row*column(3*3) 2-D array => int [,] A= new int[3,3];

  3. Now fill all the elements in the matrix with zero (this will help in determining whether the element has already been reached).

    Note: You can avoid the third step since all the elements in a matrix are by default “zero”.

  4. We need to create a condition for the loop to run until all the elements are filled. For that we will use the following conditions:
    1. while(no<=ele){} ;   
    Where:

    ele=row*column(total no of elements)

    no=number that is being filled into a element at the given instant.

  5. Now use a for loop for each case to fill the matrix.

    i=row index, j=column index

    1. From left to right, keep the row index fixed and increment the column index until the column index is less than the number of columns & the element is zero, if any of the conditions are not satisfied then break the loop.

      Now increment the row index by 1 to reach the next row and decrement the column index as when it comes out of the for loop it is one value greater.
      1. For(i=0,j=0;(j<column&&a[i,j]==’0’);j++)  
      2. { A[i,j]=no;  
      3. no++; }  
      4. rinst++;cinst=--j;  
    2. From top to bottom: Keep the column index fixed and increment the row index until the row index is less than the number of rows & the element is zero, if any of the conditions are not satisfied then break the loop.

      Now decrement the column index by 1 to reach the next element in spiral order and decrement the row index since when it comes out of the for loop it is one value greater.
      1. For(i=rinst,j=jinst;(i<row&&a[i,j]==’0’);i++)  
      2. { A[i,j]=no;  
      3. no++; }  
      4. rinst=--i;cinst--;  
    3. From right to left: Keep the row index fixed and decrement the column index until the row index is greater than zero and the element is zero, if any of the conditions are not satisfied then break the loop.

      Now decrement the row index by 1 to reach the next element in spiral order and increment the column index because when it comes out of the for loop it is one value greater.
      1. For(i=rinst,j=jinst;(j>=0 && a[i,j]==’0’);j--)  
      2. { A[i,j]=no;  
      3. no++; }  
      4. rinst--;cinst=++j;  
    4. From bottom to top: Keep the column index fixed and decrement the row index until the row index is greater than zero and the element is zero, if any of the conditions are not satisfied then break the loop.

      Now increment the column index by 1 to reach the next element in the spiral order and increment the row index since when it comes out of the for loop it is one value greater.
      1. For(i=rinst,j=jinst;(i>=0 && a[i,j]==’0’);i--)  
      2. { A[i,j]=no;  
      3. no++; }  
      4. cinst++;rinst=++i;  
  6. Now print the matrix.

    Note: this algorithm is defined for any number of rows and columns.

Conclusion

I hope someone finds this article helpful. Please share your valuable thoughts and comments. Your feedback is always welcomed.


Similar Articles