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

In this article you will learn how to create a Spiral Matrix by taking the number of rows and columns as input from the user using C#.

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:

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**

- Take the inputs from the user and store them, for example

row=3; column=3

- Create a
*row*column(3*3) 2-D array => int [,] A= new int[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”.

- 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:

- while(no<=ele){} ;

ele=row*column(total no of elements)

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

- Now use a for loop for each case to fill the matrix.
*i=row index, j=column index*

- 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.

- For(i=0,j=0;(j<column&&a[i,j]==’0’);j++)
- { A[i,j]=no;
- no++; }
- rinst++;cinst=--j;

- 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.

- For(i=rinst,j=jinst;(i<row&&a[i,j]==’0’);i++)
- { A[i,j]=no;
- no++; }
- rinst=--i;cinst--;

- 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.

- For(i=rinst,j=jinst;(j>=0 && a[i,j]==’0’);j--)
- { A[i,j]=no;
- no++; }
- rinst--;cinst=++j;

- 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.

- For(i=rinst,j=jinst;(i>=0 && a[i,j]==’0’);i--)
- { A[i,j]=no;
- no++; }
- cinst++;rinst=++i;

- 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 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.