Qn-8.c (1112B)
1 /* Write a C program to find the transpose of a given square matrix using 2D array. 2 The transpose of a matrix is obtained by swapping the rows and columns of the matrix */ 3 4 #include <stdio.h> 5 6 void transpose(int row, int col, int[row][col]); 7 void print(int row, int col, int[row][col]); 8 9 int main() 10 { 11 int row = 3, col = 3, i, j, val = 1; 12 int arr[row][col]; 13 for (i = 0; i < row; i++) 14 { 15 for (j = 0; j < col; j++) 16 { 17 arr[i][j] = val++; 18 } 19 } 20 printf("Before Transpose: \n"); 21 print(row, col, arr); 22 transpose(row, col, arr); 23 return 0; 24 } 25 26 void transpose(int row, int col, int arr[row][col]) 27 { 28 int i, j; 29 int res[col][row]; 30 for (i = 0; i < row; i++) 31 { 32 for (j = 0; j < col; j++) 33 { 34 res[j][i] = arr[i][j]; 35 } 36 } 37 printf("\nAfter Transpose: \n"); 38 print(row, col, res); 39 } 40 41 void print(int row, int col, int arr[row][col]) 42 { 43 int i, j; 44 for (i = 0; i < row; i++) 45 { 46 for (j = 0; j < col; j++) 47 { 48 printf("%d ", arr[i][j]); 49 } 50 printf("\n"); 51 } 52 }