Home > Back-end >  Cannot transpose a 2D matrix in C
Cannot transpose a 2D matrix in C

Time:09-10

I am a beginner in programming. Learning 2D arrays in C now. I encountered a question asking to transpose a 2D matrix (i.e a 2D array). The book's suggested answer suggested using another array.

I was trying to find a way to do the same without the other array.

Here's my code:

#include <stdio.h>
#include <stdlib.h>
void printAr(int[3][3]);
int main()
{
    int A[3][3];
    printf("Enter the numbers: \n");
    for(int i=0;i<3;i  )
    {
        for(int j=0;j<3;j  )
        {
            scanf("%d",&A[i][j]);
        }
    }

    printf("\n-------------------------------\n");
    printAr(A);
    printf("Now transposing------------------\n");
    for(int f=0;f<3;f  )
    {
        for(int h=0;h<3;h  )
        {
            int t=A[f][h];
            A[f][h]=A[h][f];
            A[h][f]=t;
        }
    }
    printAr(A);
    return 0;
}

void printAr(int B[3][3])
{
    for(int k=0;k<3;k  )
    {
        for(int l=0;l<3;l  )
        {
            printf("%d ",B[k][l]);
        }
        printf("\n");
    }
}

Here is the output:

Enter the numbers:
1
2
3
4
5
6
7
8
9

-------------------------------
1 2 3
4 5 6
7 8 9
Now transposing------------------
1 2 3
4 5 6
7 8 9

What am i doing wrong? And why can't it transpose it?

CodePudding user response:

As the transposing starts the first element at (0,0) is swapped with the last element which is (2,2) because there is no condition when the loop reaches the end at (2,2) it again swaps it with the first element at (0,0) and that's happening with all the elements that the reason you are getting the same matrix to solve it just add the following condition.

for(int f=0;f<3;f  )
{
    for(int h=0;h<3;h  )
    {
        if(h<f){
            int t=A[f][h];
            A[f][h]=A[h][f];
            A[h][f]=t;
        }
    }
}

CodePudding user response:

In a transpose operation, every element of the upper-triangular submatrix gets swapped with one of the lower-triangular sub-matrix, and the diagonal elements don't move.

So a quick fix is

for(int f=0;f<3;f  )
{
    for(int h=0;h<3;h  )
    {
        if (h<f)
        {
            int t=A[f][h];
            A[f][h]=A[h][f];
            A[h][f]=t;
        }
    }
}

But you can do that in a slightly better way...

CodePudding user response:

Your transpose logic is wrong because your two loops will transpose the array twice resulting in the same array you have begin with. One of the solution is to copy the result in another array.

#include <stdio.h>
#include <stdlib.h>
void printAr(int[3][3]);
int main()
{
int A[3][3];
int B[3][3];
printf("Enter the numbers: \n");
for(int i=0;i<3;i  )
{
for(int j=0;j<3;j  )
{
scanf("%d",&A[i][j]);
}
}

printf("\n-------------------------------\n");
printAr(A);
printf("Now transposing------------------\n");
for(int f=0;f<3;f  )
{
for(int h=0;h<3;h  )
{
int t=A[f][h];
B[f][h]=A[h][f];

}
}
printAr(B);
return 0;
}
void printAr(int B[3][3])
{
for(int k=0;k<3;k  )
{
for(int l=0;l<3;l  )
{
printf("%d ",B[k][l]);
}
printf("\n");
}
}

If you want to transpose without using second array try this.

#include <stdio.h>
#include <stdlib.h>
void printAr(int[3][3]);
int main()
{
int A[3][3];
printf("Enter the numbers: \n");
for(int i=0;i<3;i  )
{
    for(int j=0;j<3;j  )
    {
        scanf("%d",&A[i][j]);
    }
}

printf("\n-------------------------------\n");
printAr(A);
printf("Now transposing------------------\n");
for(int f=0;f<3;f  )
{
    for(int h=f 1;h<3;h  )
    {
        int t=A[f][h];
        A[f][h]=A[h][f];
        A[h][f]=t;
    }
}
printAr(A);
return 0;
}

void printAr(int B[3][3])
{
for(int k=0;k<3;k  )
{
for(int l=0;l<3;l  )
{
printf("%d ",B[k][l]);
}
printf("\n");
}
}
  • Related