using System;
class Program
{
static void PrintMatrix(int[,] matrix) {
int rows = matrix.GetLength(0); // number of rows
int cols = matrix.GetLength(1); // number of columns
for (int i = 0; i < rows; i++) {
string row = "";
for (int j = 0; j < cols; j++) {
// Pad each number to width 4
row += matrix[i, j].ToString().PadLeft(4);
}
Console.WriteLine(row);
}
}
static void InverseMatrix(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int counter = 0;
for (int i = 0, r = rows - 1; i < rows; i++, r--) {
for (int j = 0, c = cols - 1; j < cols; j++, c--) {
// swap values
int temp = matrix[i, j];
matrix[i, j] = matrix[r, c];
matrix[r, c] = temp;
counter++;
if (counter > (rows * cols) / 2 - 1)
return;
}
}
}
static void Main()
{
int[,] matrix = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
Console.WriteLine("matrix:");
PrintMatrix(matrix);
InverseMatrix(matrix);
Console.WriteLine("\ninverse matrix:");
PrintMatrix(matrix);
}
}
/*
run:
matrix:
1 2 3 4
5 6 7 8
9 10 11 12
inverse matrix:
12 11 10 9
8 7 6 5
4 3 2 1
*/