w3resource

C Exercises: Transpose of a Matrix

C Array: Exercise-22 with Solution

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

Pictorial Presentation:

C Exercises: Transpose of a Matrix

Sample Solution:

C Code:

#include <stdio.h>

void main()
  
  {
  int arr1[50][50],brr1[50][50],i,j,r,c;
  
       printf("\n\nTranspose of a Matrix :\n");
       printf("---------------------------\n"); 


       printf("\nInput the rows and columns of the matrix : ");
       scanf("%d %d",&r,&c);

       printf("Input elements in the first matrix :\n");
       for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&arr1[i][j]);
            }
        } 

 	 printf("\nThe matrix is :\n");
  		for(i=0;i<r;i++)
    		{
      		printf("\n");
      		for(j=0;j<c;j++)
          	printf("%d\t",arr1[i][j]);
    		}
  
  for(i=0;i<r;i++)
     {
      for(j=0;j<c;j++)
            {
                   brr1[j][i]=arr1[i][j];
            }
      }

      printf("\n\nThe transpose of a matrix is : ");
      for(i=0;i<c;i++){
      printf("\n");
      for(j=0;j<r;j++){
           printf("%d\t",brr1[i][j]);
      }
  }
      printf("\n\n");
}

Sample Output:

Transpose of a Matrix :                                                                                       
---------------------------                                                                                   
                                                                                                              
Input the rows and columns of the matrix : 2 2                                                                
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [1],[0] : 3                                                                                         
element - [1],[1] : 4                                                                                         
                                                                                                              
The matrix is :                                                                                               
                                                                                                              
1       2                                                                                                     
3       4                                                                                                     
                                                                                                              
The transpose of a matrix is :                                                                                
1       3                                                                                                     
2       4 

Flowchart:

Flowchart: Transpose of a Matrix.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C for multiplication of two square Matrices.
Next: Write a program in C to find sum of right diagonals of a matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP