patterncsharpMinor
Reversing a matrix in C#
Viewed 0 times
matrixreversingstackoverflow
Problem
Implementation:
Test program:
Input:
Output:
How can I improve this implementation?
static void ReverseMatrix(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 9Output:
9 8 7
6 5 4
3 2 1How can I improve this implementation?
Solution
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.
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
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.
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.
Code Snippets
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;
}matrix = ReverseMatrix(matrix);Context
StackExchange Code Review Q#66791, answer score: 2
Revisions (0)
No revisions yet.