I took the idea from Reversing a matrix in C# and decided to write my own matrix-reversing code.
Is there anything that I can do to make it even better?
static void Main(string[] args)
{
var matrix = new[,]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
PrintMatrix(ReverseMatrix(matrix));
Console.ReadLine();
var matrix2 = new[,]
{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
PrintMatrix(ReverseMatrix(matrix2));
Console.ReadLine();
}
static int[,] ReverseMatrix(int[,] inputMatrix)
{
var outputMatrix = new int[inputMatrix.GetLength(0), inputMatrix.GetLength(1)];
var x = 0;
for (int i = inputMatrix.GetLength(0) - 1; i >= 0; i--)
{
var y = 0;
for (int j = inputMatrix.GetLength(1) - 1; j >= 0; j--)
{
outputMatrix[x, y] = inputMatrix[i, j];
y++;
}
x++;
}
return outputMatrix;
}
I stole the Print
method from the other question. I imagine that I could place it inside the original loop if I wanted to write fewer lines of code, but I think that might violate the SRP.
X by X
\$\endgroup\$