Home > OS >  How can I transpose my Matrix to reverse columns and rows?
How can I transpose my Matrix to reverse columns and rows?

Time:10-11

I managed to do enter M, N, and accept its condition (0<N and M<=10), then create and apply desirable number into 2 matrices, which is called Matrix A and Matrix B, then the Matrix C is the plus of 2 Matrix A B.

The last one is the transpose, I have looked upon some sites, but since the ways they do are not like mine so I don't like to just "straightly" copy and paste it.

Here is my code:

#include <stdio.h>

int main()
{
int M, N, i, j;

printf ("Enter number M: ");
scanf("%d", &M);
printf ("Enter number N: ");
scanf("%d", &N);

if (0 < N && M <= 10) {
    printf ("Accepted number M: %d\n", M);
    printf("Accepted number N: %d\n", N);
}
else printf("Please enter valid number, which is 0 < N and M <= 10!");

int matrixA[M][N], matrixB[M][N], matrixC[M][N];

printf ("Enter matrix A: \n");

for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         scanf("%d", &matrixA[i][j]);
                        }
}

printf ("Enter matrix B: \n");

for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         scanf("%d", &matrixB[i][j]);
                        }
}

printf("Matrix A: \n");
for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         printf("%d ", matrixA[i][j]);
                        }
printf("\n");
}

printf("Matrix B: \n");
for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         printf("%d ", matrixB[i][j]);
                        }
printf("\n");
}

for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         matrixC[i][j] = matrixA[i][j]   matrixB[i][j];
                        }
}

printf("Sum of matrix A and matrix B => Matrix C is: \n");
for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         printf("%d ", matrixC[i][j]);
                        }
printf("\n");
}

int reverseMatrixC[N][M];
for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         reverseMatrixC[j][i] = matrixC[i][j];
                        }
}
printf("Reverse columns and rows in matrix C: \n");
for (i=0; i<M; i  ){
   for (j=0; j<N; j  ){
         printf("%d ", reverseMatrixC[i][j]);
                        }
printf("\n");
}
}

The last one "Reverse columns and rows" are wrong and I don't know why, from my way of thinking, I'm switching M and N, then apply the previous Matrix C into a new variable which is reverseMatrixC, and then reverse i and j.

For example, I choose M = 2, N = 3. and then I choose matrixA and matrixB these value:

1 2 3
4 5 6

The matrixC will display:

2 4 6
8 10 12

But when I transpose it, it'll display:

2 8 4
4 10 6

What I want is something like this:

2 8
4 10
6 12

CodePudding user response:

You're very close. In fact, you're transposing matrixC correctly, the only problem is with how you're printing it out:

printf("Reverse columns and rows in matrix C: \n");
// reverseMatrixC has its rows and columns dimensions swapped, so N specifies its number of rows
for (i=0; i<N; i  ){
    // and M specifies its number of columns
    for (j=0; j<M; j  ){
            printf("%d ", reverseMatrixC[i][j]);
    }
    printf("\n");
}

Also highly recommend to fix your indentation and use an IDE that formats for you, it makes reading code much easier.

Demo

  •  Tags:  
  • c
  • Related