Function that transposes a matrix, using passage by reference

Asked

Viewed 209 times

0

I need to do a function that by passing a matrizA by reference, the matrizA would turn the transpose of the matrizA. I tried using an auxiliary matrix, only it’s not working. It has to be a 5x5 matrix.

#include <stdio.h>
#include <stdlib.h>
void Transposta(int *MatrizA[][5]){
    int MatrizAux[5][5];
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            MatrizAux[i][j] = *MatrizA[i][j];
        }
    }

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            *MatrizA[j][i] = MatrizAux[i][j];
        }
    }
}
int main(){
    int Matriz[5][5];
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            Matriz[i][j] = i+j;
        }
    }
    Transposta(&Matriz);
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            printf("| %d |", Matriz[i][j]);
        }
        printf("\n");
    }
    return 0;
}
  • 1

    What is happening is that you are swapping out cda element twice, which makes it the same as the original. Try: for (int i = 0; i < 5; i++) {&#xA; for (int j = i+1; j < 5; j++) {&#xA; int aux = MatrizA[i][j];&#xA; MatrizA[i][j] = MatrizA[j][i];&#xA; MatrizA[j][i] = aux;&#xA; }&#xA; }.

1 answer

0


The matrix you created is equal to:

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

I mean, it’s a symmetrical matrix, which means it is equal to your transpose. Therefore, it is not a good example for you to test if the function transposta is working.

So first let’s modify the first one a little bit loop:

for (int i = 0, c = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++, c++) {
        matriz[i][j] = c;
    }
}

This creates the following matrix:

|  0 |  1 |  2 |  3 |  4 |
|  5 |  6 |  7 |  8 |  9 |
| 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 |

Now yes we can get the transpose and know if it worked.


Since the matrix is square, you don’t need to go through all the elements. The diagonal remains the same, and you only need to traverse one of the "halves" (or the "right" or "left" part of the diagonal) and switch to the corresponding "opposite" position:

void transposta(int matriz[][5]) {
    for (int i = 1; i < 5; i++) { // começa em 1, pois ignora a diagonal
        for (int j = 0; j < i; j++) { // só vai até `i` (até a diagonal), não precisa ir até o fim
            // troca os elementos de posição
            int aux = matriz[i][j];
            matriz[i][j] = matriz[j][i];
            matriz[j][i] = aux;
        }
    }
}

int main(void) {
    int matriz[5][5];
    for (int i = 0, c = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++, c++) {
            matriz[i][j] = c;
        }
    }

    printf("original:\n");
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            printf("| %2d ", matriz[i][j]);
        }
        printf("|\n");
    }

    transposta(matriz);
    printf("transposta:\n");
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            printf("| %2d ", matriz[i][j]);
        }
        printf("|\n");
    }
}

I also changed the output a little bit: you were printing || between elements, changed to print only one. The output is:

original:
|  0 |  1 |  2 |  3 |  4 |
|  5 |  6 |  7 |  8 |  9 |
| 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 |
transposta:
|  0 |  5 | 10 | 15 | 20 |
|  1 |  6 | 11 | 16 | 21 |
|  2 |  7 | 12 | 17 | 22 |
|  3 |  8 | 13 | 18 | 23 |
|  4 |  9 | 14 | 19 | 24 |

Browser other questions tagged

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