How to change position of all matrix elements by changing the row number by the column?

Asked

Viewed 1,578 times

1

I tried this algorithm but the result is the same after this exchange

  int[,] array = new int[10,10];

  for (int l = 0; l <  10; l++)
    for (int c = 0; c < 10; c++)
     {               
       int temp = array[l, c];
       array[l, c] = array[c, l];
       array[c, l] = temp;
    }

Output before exchange

 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

depois da troca

 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
  • If the code is just that, all are worth the same, then there is no way to notice difference. If you put a real code, a [mcve], it is easier to help.

  • But I don’t really understand why it doesn’t work

  • I liked the two solutions can not mark the two? is that work the two xD

  • You can only vote for all of them, accept only one, you have to choose the one that is closest to what you want. It works, one generates a new object. I made one that saves memory, is faster and goes in line than you posted, I found a better solution, so I posted, but the decision is yours.

2 answers

2

Your algorithm doesn’t seem to make any sense to me.

Transpose an array, will reverse the size of it (rows turn columns and columns turn rows).

The right thing would be to do something like:

public int[,] Transpor(int[,] matriz)
{
    int w = matriz.GetLength(0);
    int h = matriz.GetLength(1);

    int[,] novaMatriz = new int[h, w];

    for (int i = 0; i < w; i++)
    {
        for (int j = 0; j < h; j++)
        {
            novaMatriz[j, i] = matriz[i, j];
        }
    }

    return novaMatriz;
}

See working on . NET Fiddle.

2


If you want to do inline just start checking the column from the stopped row. The problem is that if you start from 0 it reverses what has already been reversed, then back to the original place. So:

using static System.Console;

public class Program {
    public static void Main() {
        int[,] array = new int[10,10];
        for (int l = 0; l < 10; l++) {
            for (int c = 0; c < 10; c++) array[l, c] = c;
        }
        for (int l = 0; l < 10; l++) {
            for (int c = 0; c < 10; c++) Write($"{array[l, c]} ");
            WriteLine();
        }
        WriteLine();
        for (int l = 0; l < 10; l++) {
            for (int c = l; c < 10; c++) { // <========== mudei aqui, veja o l
                int temp = array[l, c];
                array[l, c] = array[c, l];
                array[c, l] = temp;
            }
        }
        for (int l = 0; l < 10; l++) {
            for (int c = 0; c < 10; c++) Write($"{array[l, c]} ");
            WriteLine();
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.