Transpose of a Matrix in C

Write a program to find the transpose of a matrix in the C programming language. Write a 

Transpose of a Matrix

Let A =[aij] be an m × n matrix. The transpose of A, denoted by At, is the n × m matrix obtained by interchanging the rows and columns of A.

In other words, if At =[bij], then bij = aji for i = 1,2,…,n and j = 1,2,…,m. If the original matrix is of size 2×4 then it’s transposed will be of size 4×2.

For 3×2 Matrix,

Original Matrix
a11 a12
a21 a22
a31 a32

Transpose Matrix
a11 a21 a31
a12 a22 a32

Example using 2×2 matrix:-

     1   2
A = 
     3   4

Then the transpose of matrix,

     1   3
At = 
     2   4

C Program to Find Transpose of a Matrix

This is the simple C Program without using any function. The values for the matrices are already given in the program itself.

#include <stdio.h>
// main function
int main()
{
  // matrix A 
  int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

  // declare matix C
  int transpose[3][3];
  
  // find transpose of a matrix
  // outer loop for row
  for (int i = 0; i < 3; i++) {
    // inner loop for column
    for (int j = 0; j < 3; j++) {
      // formula
      transpose[i][j] = a[j][i];
    }
  }

  // display matrix
  printf("Transpose of Matrix = \n");
  for(int i=0; i<3; i++)
  {
    for(int j=0; j<3; j++)
    {
      printf("%d ", transpose[i][j]);
    }
    printf("\n"); // new line
  }

  return 0;
}

Output:-

Transpose of Matrix =
1 4 7
2 5 8
3 6 9

C Function

C Function to find the transpose of a the given matrix.

// function to find transpose of a matrix
void transMatrix(int a[10][10], int b[10][10], int row, int column)
{
  // outer loop utpo column of original matrix
  for (int i = 0; i < column; i++) {
    // inner loop utpo row of original matrix
    for (int j = 0; j < row; j++) {
      // formula
      b[i][j] = a[j][i];
    }
  }
}

The transMatrix() function takes 4 parameters, the first two parameters for matrix a, b, and the remaining two parameters for row and column of the original matrix. Its return type is void, and it returns nothing. The arrays are implicit pointers and therefore internal values are stored to the original variables.

Time Complexity:- O(N2)

Assuming the matrix is a square matrix then the size of the row and column will be similar. Then the above Java method uses two loops (from 1 to n) to find the transpose of the matrix therefore the time complexity for the method is O(N2).

Transpose of a Matrix in C by taking Input from the User

In the above program both matrices A and B were initialized within the program, now let us see another C program for transpose of a matrix by taking input value from the end-user.

Below program can find Transpose of non-square or square matrix 

#include <stdio.h>
// function to find transpose of a matrix
void transMatrix(int a[10][10], int b[10][10], int row, int column)
{
  // outer loop utpo column of original matrix
  for (int i = 0; i < column; i++) {
    // inner loop utpo row of original matrix
    for (int j = 0; j < row; j++) {
      // formula
      b[i][j] = a[j][i];
    }
  }
}
// function to read matrix
void readMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      scanf("%d", &matrix[i][j]);
    }
  }
}
// function to display matrix
void displayMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      printf("%d ", matrix[i][j]);
    }
    printf("\n"); // new line
  }
}
// main function
int main()
{
  // declare matrix matrix A, B, & C
  int matrix[10][10]; // first matrix
  int transpose[10][10]; // second matrix

  // read the size of matrices
  int row, column;
  printf("Enter Row and Column of Matrix: ");
  scanf("%d %d", &row, &column);

  // read matrix A
  printf("Enter Matrix Elements: \n");
  readMatrix(matrix, row, column);

  // transpose of matrix
  // pass row and column of original matrix
  transMatrix(matrix, transpose, row, column);

  // to display new matrix, first find its row and
  // column size
  /** Note:- If original matrix is of size 2x4  
    * then it's transpose will be of size 4x2.
    */
  // assign row and column for transpose matrix
  int trow = column;
  int tcolumn = row;

  // display resultant matrix
  printf("Resultant Matrix: \n");
  displayMatrix(transpose, trow, tcolumn);

  return 0;
}

Output:-

Enter Row and Column of Matrix: 2 4
Enter Matrix Elements:
1 2 3 4
4 5 6 7
Resultant Matrix:
1 4
2 5
3 6
4 7

Enter Row and Column for Matrix: 3 3
Enter Matrix Elements:
9 8 7
6 5 4
3 2 1
Resultant Matrix:
9 6 3
8 5 2
7 4 1

Without using Second Matrix

The below program finds the transpose of a matrix in C without using the second matrix. Here we need to swap matrix elements at appropriate positions. And after finding the transpose, while displaying the matrix, swap the row and column of the original matrix.

Transpose of a Matrix in C Without using Second Matrix

#include <stdio.h>
// function to find transpose of a matrix
void transMatrix(int a[10][10], int column)
{
  // outer loop utpo row
  for (int i = 0; i < column; i++) {
    // inner loop utpo i
    for (int j = 0; j < i; j++) {
      // swap a[i][j] with a[j][i]
      int temp = a[i][j];
      a[i][j] = a[j][i];
      a[j][i] = temp;
    }
  }
}
// function to read matrix
void readMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      scanf("%d", &matrix[i][j]);
    }
  }
}
// function to display matrix
void displayMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      printf("%d ", matrix[i][j]);
    }
    printf("\n"); // new line
  }
}
// main function
int main()
{
  // declare matrix matrix A, B, & C
  int matrix[10][10]; // first matrix

  // read the size of matrices
  int row, column;
  printf("Enter Row and Column for Matrix: ");
  scanf("%d %d", &row, &column);

  // read matrix A
  printf("Enter Matrix Elements: \n");
  readMatrix(matrix, row, column);

  // transpose of matrix
  // pass column of original matrix
  transMatrix(matrix, column);

  // swap row and column
  int temp = row;
  row = column;
  column = temp;

  // display resultant matrix
  printf("Resultant Matrix: \n");
  displayMatrix(matrix, row, column);

  return 0;
}

Output:-

Enter Row and Column for Matrix: 2 3
Enter Matrix Elements:
1 2 3
4 5 6
Resultant Matrix:
1 4
2 5
3 6

Enter Row and Column for Matrix: 3 3
Enter Matrix Elements:
1 2 3
4 5 6
7 8 9
Resultant Matrix:
1 4 7
2 5 8
3 6 9

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *