Cannot transpose a 2D matrix in C

Viewed 56

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?

2 Answers

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...

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