Fill matrix spiral C#
- User
Fill matrix spiral C#
- Assistant
- User
Rewrite this code for matrix with difference rows and columns count
- Assistant
Fill matrix spiral C#
Rewrite this code for matrix with difference rows and columns count
Filling a matrix in a spiral pattern is a common programming challenge. In C#, you can achieve this by using a 2D array and carefully managing the boundaries as you traverse the matrix in a spiral order. Below is a complete example of how you can fill a matrix in a spiral pattern:
using System;
class Program
{
static void Main()
{
int n = 4; // Size of the matrix (nxn)
int[,] matrix = new int[n, n];
FillSpiral(matrix, n);
PrintMatrix(matrix, n);
}
static void FillSpiral(int[,] matrix, int n)
{
int value = 1;
int top = 0;
int bottom = n - 1;
int left = 0;
int right = n - 1;
while (top <= bottom && left <= right)
{
// Traverse from left to right
for (int i = left; i <= right; i++)
{
matrix[top, i] = value++;
}
top++;
// Traverse from top to bottom
for (int i = top; i <= bottom; i++)
{
matrix[i, right] = value++;
}
right--;
// Traverse from right to left
if (top <= bottom)
{
for (int i = right; i >= left; i--)
{
matrix[bottom, i] = value++;
}
bottom--;
}
// Traverse from bottom to top
if (left <= right)
{
for (int i = bottom; i >= top; i--)
{
matrix[i, left] = value++;
}
left++;
}
}
}
static void PrintMatrix(int[,] matrix, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(matrix[i, j].ToString("D2") + " ");
}
Console.WriteLine();
}
}
}
Initialization:
n.n x n integer matrix.top, bottom, left, right) and the starting value.Filling the Matrix:
top boundary is less than or equal to bottom and the left boundary is less than or equal to right.top boundary, then incrementing top.right boundary, then decrementing right.bottom boundary (if top <= bottom), then decrementing bottom.left boundary (if left <= right), then incrementing left.Printing the Matrix:
When you run this code, it will fill a 4x4 matrix in a spiral order and print it:
01 02 03 04
12 13 14 05
11 16 15 06
10 09 08 07
You can change the size of the matrix by modifying the value of n.
Certainly! To handle matrices where the number of rows (m) is different from the number of columns (n), we need to adjust the code to accommodate these different dimensions. Below is the updated code:
using System;
class Program
{
static void Main()
{
int m = 4; // Number of rows
int n = 5; // Number of columns
int[,] matrix = new int[m, n];
FillSpiral(matrix, m, n);
PrintMatrix(matrix, m, n);
}
static void FillSpiral(int[,] matrix, int rows, int cols)
{
int value = 1;
int top = 0;
int bottom = rows - 1;
int left = 0;
int right = cols - 1;
while (top <= bottom && left <= right)
{
// Traverse from left to right
for (int i = left; i <= right; i++)
{
matrix[top, i] = value++;
}
top++;
// Traverse from top to bottom
for (int i = top; i <= bottom; i++)
{
matrix[i, right] = value++;
}
right--;
// Traverse from right to left
if (top <= bottom)
{
for (int i = right; i >= left; i--)
{
matrix[bottom, i] = value++;
}
bottom--;
}
// Traverse from bottom to top
if (left <= right)
{
for (int i = bottom; i >= top; i--)
{
matrix[i, left] = value++;
}
left++;
}
}
}
static void PrintMatrix(int[,] matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j].ToString("D2") + " ");
}
Console.WriteLine();
}
}
}
Setting Matrix Dimensions:
m for the number of rows and n for the number of columns.int[,] matrix with dimensions [m, n].FillSpiral Method Adjustments:
top, bottom, left, right) according to the rows and columns.Traversal Logic:
PrintMatrix Method Adjustments:
When you run this code, it will fill a 4x5 matrix in a spiral order and print it:
01 02 03 04 05
14 15 16 17 06
13 20 19 18 07
12 11 10 09 08
You can adjust the values of m and n to test with different matrix sizes.
Not yet included in a published release.