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

Asked

Viewed 12,103 times

0

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

I’ve written a code, but it doesn’t work properly for matrices whose number of rows is less than the number of columns.

Follow the code and an example of the execution of 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 sizes Small size and sizeColumn are defined in the main function at the end of the code and represent the number of rows and columns of the initial matrix.

In the section below is created a square matrix 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 be equal to the number of rows (sizeLine).

Then all elements of this matrix are equal to zero.

  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 zeroed matrix. This has no practical use for the program, but helps to visualize step by step what happens in the program.

Then the user fills the matrix with the elements he wants and finally is printed, with the values already filled by the user.

A row or column will be zeroed as 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");
  }

Start below the transposition of the matrix. First the values of the initial matrix are exchanged, then a new matrix called transposed is filled (already stated in the function argument), which copies the elements of the modified initial matrix, but has its dimensions corrected.

Its number of rows is the number of columns (sizeColumn) chosen by the user in the main function and its number of columns is the sizeLine.

Thus, it is equal to the modified initial matrix, but without the column/row 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 altered initial matrix (under the name of the final matrix) and the transpose are printed respectively.

At this point it is possible to visualize the problem. When the number of rows chosen by the user in the main function is less than the number of columns, the excess columns are not changed by the "transposition function", and the zeros in the transpose are not replaced by other numbers.

I have tested the program using several different sizes and the results were the same 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:

Quando tamanhoLinha > tamanhoColuna, o programa funciona corretamente

Image 2:

Quando tamanhoLinha < tamanhoColuna, o problema aparece

1 answer

1

The problem is that you are transposing the matrix in-place, Then the order of operations makes a difference. When you transpose the first half of the matrix, you no longer have the first half of the original matrix. In this case, you have to exchange 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 the cells to right of the main diagonal (we do not need to cross the main diagonal, of course) and exchange each cell with the corresponding one on the other side of the diagonal. But we have to do this to the entire expanded matrix tamanho×tamanho, not only for the part tamanholinha×tamanhoColuna of the matrix.

Browser other questions tagged

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