6
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This doesn't work with matrices that are not X by X \$\endgroup\$ – Malachi Oct 15 '14 at 21:21
  • \$\begingroup\$ Your question is a target of rags-to-riches \$\endgroup\$ – Heslacher Oct 16 '14 at 9:11
3
\$\begingroup\$

To make it more clear that x and y must move together with i and j, move their declarations and manipulations inside the loops, right next to each other:

static int[,] ReverseMatrix(int[,] inputMatrix)
{
    var outputMatrix = new int[inputMatrix.GetLength(0), inputMatrix.GetLength(1)];
    for (int x = 0, i = inputMatrix.GetLength(0) - 1; i >= 0; i--, x++)
    {
        for (int y = 0, j = inputMatrix.GetLength(1) - 1; j >= 0; j--, y++)
        {
            outputMatrix[x, y] = inputMatrix[i, j];
        }
    }
    return outputMatrix;
}

This way it's less error-prone, easier to read, and more compact, all at the same time.

I would move ReverseMatrix method into a MatrixUtils class, and rename it to simply Reverse. Since it becomes perfectly clear that you're working with matrices, I would also simplify the parameter and local variable names, as inputMatrix and outputMatrix seem kind of tedious.

It would be better to replace the uninteresting Main method with unit tests on the example matrices:

  1. Easy way for you (and for code reviewers) to verify the implementation works
  2. The assertions in the tests would clarify the definition of "reverse matrix"
\$\endgroup\$
  • \$\begingroup\$ Next step on this project....Unit Tests! and a more OO Approach! \$\endgroup\$ – Malachi Oct 23 '14 at 16:48
6
\$\begingroup\$

is there anything that I can do to make it even better?

Yes, you should use a more OO approach, where you should

  • Define a class Matrix
  • Add a Reverse() method which returns a Matrix
  • Override the ToString() method for getting a String representation for printing

You can then easily extend the class, like adding the ability to multiply 2 matrixes etc.

\$\endgroup\$
  • \$\begingroup\$ Also he can represent the matrix as a regular 1D array. That will increase performance. \$\endgroup\$ – Raz Megrelidze Oct 16 '14 at 13:52
  • \$\begingroup\$ Implementation: pastebin.com/PRc22th4 \$\endgroup\$ – Raz Megrelidze Oct 16 '14 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.