C Program to find the transpose of a matrix

What is the transpose of a matrix?

In this c program what we need is to calculate the transpose of the matrix. so we need to accept a matrix and calculate its transpose. So First we have to know what it means by the transpose of a matrix. Transpose of a matrix means we have to change the columns of a matrix into the rows and vice versa.

Example: If the matrix is

2 5

6 7

Then the Transpose of the matrix is

2    6

5    7


The logic of this program is first to declare the variables as m,n, ma[][] as integers. Then read the order of the matrix into the variable m,n. Read the matrix from the user using nested for loop and save it into the array ma[i][j]. Then display the given matrix. To find out the transpose of the matrix, change each element's column to row and row to column. So we are using a nested for loop with the condition j

ALGORITHM

STEP 1:Include the header files to use the built-in header files into the C program.

STEP 2: Declare the variable i,j,m,n,ma[10][10] as integer .

STEP 3:Read the order of the matrix from the user into the variables m and n.

STEP 4:Read the coefficients of the matrix by using for loop into the matrix ma[i[j].

STEP 5:Display the given matrix as each element of ma[i]j] using for loop.

STEP 6: By using a for loop with condition j

STEP 7: By using another for with the condition i

STEP 8:Display ma[i][j] as transpose matrix 

 

C Source Code

                                          #include <stdio.h>

void main() {
  static int ma[10][10];
  int i, j, m, n;
  printf("Enter the order of the matrix \n"); /* enter the order of matrix */
  scanf("%d %d", & m, & n); /* accepting the values */
  printf("Enter the coefficients of the matrix\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      scanf("%d", & ma[i][j]);
    }
  }
  printf("The given matrix is \n"); /* just prints the matrix as such */
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      printf(" %d", ma[i][j]);
    }
    printf("\n");
  }
  printf("The transpose of the matrix \n"); /* finding the transpose of matrix using loop */
  for (j = 0; j < n; ++j) {
    for (i = 0; i < m; ++i) {
      printf(" %d", ma[i][j]);
    }
    printf("\n");
  }
} /* End of main() */
                                      

OUTPUT

Enter the order of the matrix
2*2

Enter the coefficients of the matrix
 3 -1
 6  0

The given matrix is
 3 -1
 6  0

The transpose of the matrix is
 3 6
-1 0