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 matrix A will be transposed by permutation of the elements
\(^{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];
int R, C;
int i, j;
int max, aux;
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");
}
//
max = (R > C) ? R : C;
/*
if(R>C)
max=R;
else
max=C;
*/
for (i = 0; i < max; i++)
{
for (j = i + 1; j <max; j++)
{
aux = M[i][j];
M[i][j] = M[j][i];
M[j][i] = aux;
}
}
printf("\n\n*** Transpose ***\n");
for (i = 0; i < C; i++)
{
for (j = 0; j < R; j++)
printf("%4d", M[i][j]);
printf("\n");
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
