Qn-8.c (1303B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 06 Mar 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a C program to find the transpose of a given square matrix using 2D array. 9 The transpose of a matrix is obtained by swapping the rows and columns of the matrix */ 10 11 #include <stdio.h> 12 13 void transpose(int row, int col, int[row][col]); 14 void print(int row, int col, int[row][col]); 15 16 int main() 17 { 18 int row = 3, col = 3, i, j, val = 1; 19 int arr[row][col]; 20 for (i = 0; i < row; i++) 21 { 22 for (j = 0; j < col; j++) 23 { 24 arr[i][j] = val++; 25 } 26 } 27 printf("Before Transpose: \n"); 28 print(row, col, arr); 29 transpose(row, col, arr); 30 return 0; 31 } 32 33 void transpose(int row, int col, int arr[row][col]) 34 { 35 int i, j; 36 int res[col][row]; 37 for (i = 0; i < row; i++) 38 { 39 for (j = 0; j < col; j++) 40 { 41 res[j][i] = arr[i][j]; 42 } 43 } 44 printf("\nAfter Transpose: \n"); 45 print(row, col, res); 46 } 47 48 void print(int row, int col, int arr[row][col]) 49 { 50 int i, j; 51 for (i = 0; i < row; i++) 52 { 53 for (j = 0; j < col; j++) 54 { 55 printf("%d ", arr[i][j]); 56 } 57 printf("\n"); 58 } 59 }