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

1

I tried this algorithm but the result stays 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 switching

 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 |
    
asked by anonymous 14.07.2017 / 16:09

2 answers

1

If you want to do inline just start checking the column from the line that has stopped. The problem is that if you start from 0 it reverses what has already been inverted, then it returns 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();
        }
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I put it in GitHub for future reference .

    
14.07.2017 / 16:33
2

Your algorithm does not seem to make any sense to me.

Transposing an array will invert its size (rows have seen columns and columns have seen 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.

    
14.07.2017 / 16:12