Program
Time Complexity: O(mn)
Space Complexity: O(mn)
where m and n are number of rows and columns in matrix
#include <stdio.h>
int main()
{
printf("****************************************************");
printf("\n****************************************************");
printf("\n** WAP to transpose elements of a matrix/2D array **");
printf("\n** Created by Sheetal Garg **");
printf("\n** Assistant Professor **");
printf("\n** Phone No:9467863365 **");
printf("\n****************************************************");
printf("\n****************************************************\n");
int m, n, i, j, a[5][5], t[5][5];
printf("Enter no. of rows and columns in the array\n");
scanf("%d%d", &m, &n);
printf("enter array elements\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("\nThe array elements are\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
for (i = 0; i < n ; i++)
for (j = 0; j < m; j++)
t[i][j] = a[j][i];
printf("\nThe array elements after transpose are\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%d\t", t[i][j]);
printf("\n");
}
return 0;
}
Output 1
**************************************************** **************************************************** ** WAP to transpose elements of a matrix/2D array ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************** **************************************************** Enter no. of rows and columns in the array 2 3 enter array elements 2 4 6 1 3 5 The array elements are 2 4 6 1 3 5 The array elements after transpose are 2 1 4 3 6 5