Write a program that performs the transposition tA of a matrix A of dimensions N and M into a matrix of dimensions M and N.

The transposed matrix will be stored in a second matrix B which will then be displayed.

\(^{t}A = ^{t}\begin{pmatrix} a & b & c & d \\e & f & g & h \\ i & j & k & l \end{pmatrix}= \begin{pmatrix} a & e & i \\ b & f & j \\ c & g & k \\ d & h & l\end{pmatrix}\)


Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 10
void main()
{
	int M[SIZE][SIZE], T[SIZE][SIZE];
	int R, C;
	int i, j;
	do {
		printf("Enter number of rows: ");
		scanf("%d", &R);
	} while (R <= 0 || R > SIZE);

	do {
		printf("Enter number of columns: ");
		scanf("%d", &C);
	} while (C <= 0 || C > SIZE);

	for (i = 0; i < R; i++)
	{
		for (j = 0; j < C; j++)
		{
			printf("Enter M[%d][%d]=", i, j);
			scanf("%d", &M[i][j]);
		}
	}

	printf("\n\n*** Matrix ***\n");
	for (i = 0; i < R; i++)
	{
		for (j = 0; j < C; j++)
			printf("%4d", M[i][j]);
		printf("\n");
	}

	for (i = 0; i < R; i++)
	{
		for (j = 0; j < C; j++)
			T[j][i] = M[i][j];
	}

	printf("\n\n*** Transpose ***\n");
	for (i = 0; i < C; i++)
	{
		for (j = 0; j < R; j++)
			printf("%4d", T[i][j]);
		printf("\n");
	}

	getch();
}


Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Result of a division