# Reversing a matrix in C#

Implementation:

static void ReverseMatrix<T>(T[,] matrix)
{
int width = matrix.GetLength(1);
int height = matrix.GetLength(0);
int startRow = 0;
int startCol = 0;
int endRow = height-1;
int endCol = width-1;

while (true)
{
if (startRow == height-1 || endRow == 0) return;

// swap
var temp = matrix[startRow, startCol];
matrix[startRow, startCol] = matrix[endRow, endCol];
matrix[endRow, endCol] = temp;

startCol++;
endCol--;
if (startCol == width)
{
startRow++;
startCol = width - 1;
}
if (endCol == -1)
{
endRow--;
endCol = 0;
}
}
}


Test program:

static void Main()
{
var matrix = new[,]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

ReverseMatrix(matrix);

PrintMatrix(matrix);
}

static void PrintMatrix(int[,] matrix)
{
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write("{0}\t",matrix[row,col]);
}
Console.WriteLine();
}
}


Input:

1 2 3
4 5 6
7 8 9


Output:

9 8 7
6 5 4
3 2 1


How can I improve this implementation?

• You can post your corrections as a self-answer. – Jamal Oct 15 '14 at 20:56
• It's not obvious from the name what the method does, but it seems most likely to be either reversing the columns or reversing the rows, not doing both. Rotate180 might be a better name. – Peter Taylor Oct 16 '14 at 16:39

I also changed the Reversal code and got rid of all the variables. from the looks of it you did what I did only you did it with out the use of for loops

I used one loop to go through the rows(or the first dimension of the multi-dimensional array) and then I nested a second loop inside the first to loop through the elements of each array. now in doing this I needed some independent variables to be incremented for the output matrix so I made sure that I declared them in the right places and incremented them in the right places. the output matrix was declared using the input matrix so that I didn't have to declare any magic numbers and so that I could minimize the number of variables that I had to declare as well.

enough chit chat here is the code.

    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;
}


Posted as a question also Reversing a Matrix

one thing that I did differently was that I used an input and an output for my method, I personally don't like the way that your code changes a variable outside of the scope like that, if the purpose were to change the original then I wouldn't change my code at all, I would just change how I call the code, I would do it like this

matrix = ReverseMatrix(matrix);


I don't like the way that you did it, I am sure there is a reason for it and a good and bad way to do it, but I don't think this is a good place to use code like this.

My implementation was not correct. I've made some corrections and now it works.

static void ReverseMatrix<T>(T[,] matrix)
{
int width = matrix.GetLength(1);
int height = matrix.GetLength(0);
int startRow = 0;
int startCol = 0;
int endRow = height-1;
int endCol = width-1;

while (true)
{
if (startRow > endRow) return;

// swap
var temp = matrix[startRow, startCol];
matrix[startRow, startCol] = matrix[endRow, endCol];
matrix[endRow, endCol] = temp;

startCol++;
endCol--;

if (startCol == width)
{
startRow++;
startCol = 0;
}
if(startCol == -1)
{
startRow++;
startCol = width - 1;
}
if (endCol == -1)
{
endRow--;
endCol = width - 1;
}
if (endCol == width)
{
endRow--;
endCol = 0;
}
}
}


How can I improve this implementation?

Use a Stack, which is a LIFO (Last In First Out) collection.

• Yes that works. I've converted the matrix to a IEnumerable<T> using matrix.Cast<T> and filled the stack with the sequence. Then i just Filled the matrix with that sequence, which i accomplished using sequence.GetEnumerator(). I've pasted the implementation here: pastebin.com/9VXA1FuF – Raz Megrelidze Oct 15 '14 at 20:05