ALGORITHM
- Declare and initialize a 2-D array p[a][b] of order axb.
- Read the matrix p[a][b] from the user and store in a memory location.
- declare another 2D array t store the transpose of the matrix. This array will have the reversed dimensions as of the original matrix.
- next step is to loop through the array and convert the rows into column and column into rows:
- declare 2 variables i and j
- set both to 0
- repeat until i < b
- repeat untill j<a
- t[ i ][ j ] = p[ j ][ i ]
- j = j + 1
- i = i + 1
- print the final matrix t.
SOURCE CODE
#include <stdio.h>
#define M 3
#define N 4
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < M; j++)
B[i][j] = A[j][i];
}
int main()
{
int A[M][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3}};
// Note dimensions of B[][]
int B[N][M], i, j;
transpose(A, B);
printf("Result matrix is \\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
printf("%d ", B[i][j]);
printf("\\n");
}
return 0;
}