E
E
Evgeniy V.2020-02-12 18:48:31
C++ / C#
Evgeniy V., 2020-02-12 18:48:31

Why is the matrix not transposed?

public static int[,] getTransposeMatrix(int[,] matrix)
            {
                int[,] resMatrix = matrix;

                for (int i = 0; i < resMatrix.GetLength(0); i++)
                {
                    for (int j = 0; j < resMatrix.GetLength(1); j++)
                    {
                        int tmp = resMatrix[i, j];
                        resMatrix[i, j] = resMatrix[j, i];
                        resMatrix[j, i] = tmp;
                    }
                }

                return resMatrix;
            }


Obviously a silly mistake, but I can't find it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-02-12
@volkovecgenei

because you completely enumerate it, as a result, the exchange occurs twice
ps and if you try to transpose a non-square matrix with this code, you will catch a crash in general. meditate on it ;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question