How to make a transposed matrix of dimensions chosen by the user in c?

0

I need to make an algorithm that transposes an array whose size should be chosen by the user.

I have already written a code, but it does not work correctly for arrays whose number of rows is less than the number of columns.

Here is the code and an example of running the program:

void matrizTransposta(int tamanhoLinha, int tamanhoColuna, int transposta[tamanhoColuna][tamanhoLinha])
{
  int linha;
  int coluna;
  int aux;
  int indice = 1;
  int tamanho;

/*
Exemplo:

{11, 12}          {11, 21, 31, 41}
{21, 22}  ---->>  {12, 22, 32, 42}
{31, 32}
{41, 42}

*/

The size RowSize and ColumnSize are defined in the main function at the end of the code, and represent the number of rows and columns in the initial array.

In the following section a square matrix is created whose order is the size of the largest dimension chosen in the main function. For example, if the number of rows is greater than the number of columns, the order will equal the number of rows (Row size).

Then all elements of this array are zeroed.

  if (tamanhoLinha > tamanhoColuna)
  {
    tamanho = tamanhoLinha;
  }

  else
  {
    tamanho = tamanhoColuna;
  }

  int matriz[tamanho][tamanho];

  //*****ZERAR MATRIZ*****

  for (linha = 0; linha < tamanho; linha++)
  {
    for (coluna = 0; coluna < tamanho; coluna++)
    {
      matriz[linha][coluna] = 0;
    }
  }

The section below (PRINT MATRIX) prints the matrix zeroed. This is of no practical use to the program, but it helps you visualize step by step what happens in the program.

Next, the user fills in the array with the elements he wants, and finally prints out the values already filled in by the user.

A row or column will be zeroed because the square matrix exceeds a user-defined dimension. The purpose of this is to allow the creation of the transpose later.

  //*****IMPRIMIR MATRIZ*****

  printf("\n");

  for (linha = 0; linha < tamanho; linha++)
  {
    for (coluna = 0; coluna < tamanho; coluna++)
    {
      printf("%d\t", matriz[linha][coluna]);
    }
    printf("\n\n");
  }


  //*****PREENCHER MATRIZ*****

  for (linha = 0; linha < tamanhoLinha; linha++)
  {
    for (coluna = 0; coluna < tamanhoColuna; coluna++)
    {
      printf("\nPreencha o valor [%d][%d]\n", linha + 1, coluna + 1);

      scanf("%d", &matriz[linha][coluna]);
    }
  }

  //*****IMPRIMIR MATRIZ*****

  printf("\nMatriz inicial\n\n");

  for (linha = 0; linha < tamanho; linha++)
  {
    for (coluna = 0; coluna < tamanho; coluna++)
    {
      printf("%d\t", matriz[linha][coluna]);
    }
    printf("\n\n");
  }

Starts the transpose of the matrix below. First the initial array values are changed, then a new called transposed array (already declared in the function argument) is populated, which copies the modified initial array elements but has its dimensions corrected.

Your number of rows is the number of columns (sizeColumn) chosen by the user in the main function and their number of columns is the Row size.

So, it is the same as the modified start array, but without the column / line zeroed.

  //*****TRANSPOSTA*****

  for (linha = 0; linha < tamanhoLinha; linha++)
  {
    for (coluna = 0; coluna < tamanhoColuna; coluna++)
    {
      if (linha > coluna)
      {
        aux = matriz[linha][coluna];
        matriz[linha][coluna] = matriz[coluna][linha];
        matriz[coluna][linha] = aux;
      }
    }
  }

  for (linha = 0; linha < tamanhoColuna; linha++)
  {
    for (coluna = 0; coluna < tamanhoLinha; coluna++)
    {
      transposta[linha][coluna] = matriz[linha][coluna];
    }
  }

The changed initial matrix (under the name of the final matrix) and the transpose are printed respectively.

At this point you can see the problem. When the number of rows chosen by the user in the main function is less than the number of columns, the surplus columns are not changed by the "transposition function", and the zeros in the transpose are not replaced by other numbers.

I have already tested the program using several different sizes and the results were the same ones already reported here.

The images at the end of the post will allow better visualization of the problem.

  //*****IMPRIMIR MATRIZ*****

  printf("\nMatriz final\n\n");

  for (linha = 0; linha < tamanho; linha++)
  {
    for (coluna = 0; coluna < tamanho; coluna++)
    {
      printf("%d\t", matriz[linha][coluna]);
    }
    printf("\n\n");
  }

  //*****IMPRIMIR TRANSPOSTA*****

  printf("\nMatriz Transposta\n\n");

  for (linha = 0; linha < tamanhoColuna; linha++)
  {
    for (coluna = 0; coluna < tamanhoLinha; coluna++)
    {
      printf("%d\t", transposta[linha][coluna]);
    }
    printf("\n\n");
  }
}

//*****FUNÇÃO PRINCIPAL*****

int main()
{
  int tamanhoLinha;
  int tamanhoColuna;

  printf("\nDigite o número de linhas desejado\n");

  scanf("%d", &tamanhoLinha);

  printf("\nDigite o número de colunas desejado\n");

  scanf("%d", &tamanhoColuna);

  int transposta[tamanhoLinha][tamanhoColuna];

  matrizTransposta(tamanhoLinha, tamanhoColuna, transposta);

  return 0;
}

Image 1:

Image2:

    
asked by anonymous 20.05.2017 / 17:37

1 answer

0

The problem is that you are transposing the in-place array, so the order of operations will make a difference. When you transpose the first half of the array, you no longer have the first half of the original array. In this case, you have to change the two values [i][j] and [j][i] at the same time . The relevant part of the code is:

//*****TRANSPOSTA*****

for (linha = 0; linha < tamanho; linha++) {
    for (coluna = linha + 1; coluna < tamanho; coluna++) {
        aux = matriz[linha][coluna];
        matriz[linha][coluna] = matriz[coluna][linha];
        matriz[coluna][linha] = aux;
    }
}

for (linha = 0; linha < tamanhoColuna; linha++) {
    for (coluna = 0; coluna < tamanhoLinha; coluna++) {
        transposta[linha][coluna] = matriz[linha][coluna];
    }
}

Here we go through all cells to the right of the main diagonal (we do not need to cross the main diagonal, of course) and change each cell with the corresponding one on the other side of the diagonal. But we have to do this for the entire expanded array tamanho×tamanho , not only for the tamanholinha×tamanhoColuna part of the array.

    
22.05.2017 / 04:23