ALGORITHM

  1. Declare and initialize a 2-D array p[a][b] of order axb.
  2. Read the matrix p[a][b] from the user and store in a memory location.
  3. declare another 2D array t store the transpose of the matrix. This array will have the reversed dimensions as of the original matrix.
  4. next step is to loop through the array and convert the rows into column and column into rows:
    1. declare 2 variables i and j
    2. set both to 0
    3. repeat until i < b
      1. repeat untill j<a
      2. t[ i ][ j ] = p[ j ][ i ]
      3. j = j + 1
    4. i = i + 1
  5. 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;
}