Transpose Matrix Program in C

Google Advertisements

Prev Tutorial Next Tutorial

C Program to Transpose Matrix

To transpose any matrix in C Programming language, you have to first ask to the user to enter the matrix and replace row by column and column by row to transpose that matrix, then display the transpose of the matrix on the screen as shown here in the following C program.

Examples to C Program to Transpose Matrix

The transpose of this matrix is shown below: Rows and columns are interchanged, rows of original matrix becomes column in transpose and columns of original matrix becomes rows in transpose.

Example

----------------
| 1  | 2  | 3  |
| 4  | 5  | 6  |
| 7  | 8  | 9  |
| 10 | 11 | 12 |
----------------

Example

---------------------
| 1  | 4  | 7  | 10 |
| 2  | 5  | 8  | 11 |
| 3  | 6  | 9  | 12 |
---------------------

To understand below example, you have must knowledge of following C programming topics; String in C, for looping statements we need know For Loop in C and Array in C.

C Program to Transpose Matrix

#include<stdio.h>
#include<conio.h>

void main()
{
  int a[10][10],b[10][10],i,j;
  clrscr();
  printf("\nEnter the elements of matrix: ");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  scanf("%d",&a[i][j]);
}
}
  printf("\nThe Transpose of matrix is:\n");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  b[i][j]=a[j][i];
  printf("\t%d\t",b[i][j]);
}
  printf("\n");
}
  getch();
}

Output

Enter the elements of matrix:
4
3
2
1
6
5
7
1
2

The Transpose of matrix is:
4  1  7
3  6  1
2  5  2
c program multiply 2X2 Matrices

C Program to Transpose Matrix

#include<stdio.h>
#include<conio.h>

void main()
{
int m, n, p, q, c, d, k, sum = 0;
int m1[10][10], m2[10][10], m3[10][10];
clrscr();
printf("Please enter the number of rows of 1st matrix: ");
scanf("%d", &m);
printf("Please enter number of columns of 1st matrix: ");
scanf("%d", &n);
printf("Please enter the elements of 1st matrix one by one\n");
for (  c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
scanf("%d", &m1[c][d]);
}
}
printf("Please enter the number of rows of 2nd matrix\n");
scanf("%d", &p);
printf("Please enter number of columns of 2nd matrix\n");
scanf("%d", &q);
if ( n != p )
{
printf("The entered matrices canít be multiplied each other.\n");
printf("To multiply two matrices of X and Y, the number of columns in X must be equal to the number of rows in Y");
}
else
{
printf("Please enter the elements of 2nd matrix one by one\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &m2[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + m1[c][k]*m2[k][d];
}
m3[c][d] = sum;
sum = 0;
}
}
printf("Multiplication of entered matrices:\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", m3[c][d]);
printf("\n");
}
}
getch();
}

Output

Please enter the number of rows of 1st matrix: 2
Please enter number of columns of 1st matrix: 2
Please enter the elements of 1st matrix one by one
1
2
3
4
Please enter the number of rows of 2nd matrix
2
Please enter number of columns of 2nd matrix
2
Please enter the elements of 2nd matrix one by one
3
1
2
3
Multiplication of entered matrices:
7   7
17  15
c program multiply 2X2 Matrices
Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483