Home » C Programs » Transpose of a Matrix in C

Transpose of a Matrix in C

You may get the transpose of a matrix by turning rows into columns or columns into rows. The letter “T” signifies the matrix’s transposition in the superscript of the given matrix. For example, if “A” is the supplied matrix, then the transposition of the matrix is represented as A’ or AT. Matrix “flipping” across its diagonal. Exchanged are the rows and columns. The value in the first row and third column, for instance, ends up in the third row and first column.

What is Matrix?

A group of integers built up in a rectangular array with rows and columns. The elements, or entries, of the matrix are the integers. In addition to several mathematical disciplines, matrices find extensive use in the fields of engineering, physics, economics, and statistics.

What is Array?

The definition of an array in C is a way to bring together many items of the same type. These things or things can have data types like int, float, char, double, or user-defined data types like structures.

Transpose of a Matrix in C

#include <stdio.h>
int main() {
  int a[10][10], transpose[10][10], r, c;
  printf("Enter The Rows And Colums");
  scanf("%d %d", &r, &c);
  printf("\nEnter Matrix Elements\n");
  for (int i = 0; i < r; ++i)
  for (int j = 0; j < c; ++j) {
    printf("Enter Element A%d%d", i + 1, j + 1);
    scanf("%d", &a[i][j]);
  }
  printf("\nEntered Matrix\n");
  for (int i = 0; i < r; ++i)
  for (int j = 0; j < c; ++j) {
    printf("%d  ", a[i][j]);
    if (j == c - 1)
    printf("\n");
  }
  for (int i = 0; i < r; ++i)
  for (int j = 0; j < c; ++j) {
    transpose[j][i] = a[i][j];
  }
  printf("\nTranspose of the Matrix\n");
  for (int i = 0; i < c; ++i)
  for (int j = 0; j < r; ++j) {
    printf("%d  ", transpose[i][j]);
    if (j == r - 1)
    printf("\n");
  }
  return 0;
}

Output:

Enter The Rows And Colums3
3

Enter Matrix Elements
Enter Element A1112
Enter Element A1223
Enter Element A1345
Enter Element A2156
Enter Element A2267
Enter Element A2378
Enter Element A3112
Enter Element A3234
Enter Element A3356

Entered Matrix
12 23 45
56 67 78
12 34 56

Transpose of the Matrix
12 56 12
23 67 34
45 78 56