I am to print a matrix in C# like a snake, something like this. Any advice on how to proceed?
1 2 3 4 5 6
6
1 2 3 4 6
1 4 6
1 4 6
1 4 6
1 3 4 6
1 6
1 2 3 4 5 6
Here is the code I have so far. Basically i need to figure out how to skip rows and columns.
Console.WriteLine("Enter your number: ");
int n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[n, n];
int printVal = 1;
int c1 = 0, c2 = n - 1;
while(printVal <= n * n)
{
//dvizenje na desno
for (int i = c1; i <= c2; i++)
a[c1, i] = printVal++;
//dvizenje nadolu
for (int j = c1 + 1; j <= c2; j++)
a[j, c2] = printVal++;
// dvizenje na levo
for (int i = c2 - 1; i >= c1; i--)
a[c2, i] = printVal++;
//dvizenje nagore
for (int j = c2 - 1; j >= c1 + 1; j--)
a[j, c1] = printVal++;
c1++;
c2--;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
Something like that, it's not finished and can be more effective but it's the idea (Builder Design Pattern)