F Program To Transpose Of a Matrix (C++) | CodeTheta

Program To Transpose Of a Matrix (C++)

January 14, 2016

Here mat is original matrix variable and tmat is transpose of mat matrix variable and i and j are the loop variable which count the row and column respectively.

/* http://native-code.blogspot.com */
#include <iostream>
using namespace std;
int main()
{
	int mat[2][2],tmat[2][2],i,j; //mat=original matrix , tmat=transpose matrix
	cout<<"/* http://native-code.blogspot.com */";
	cout<<"Enter the elements in array: ";
	//Now insert the element into mat[2][2]
	for(i=0;i<2;i++) //for row
	{
		for(j=0;j<2;j++)  //for column
		{
			cin>>mat[i][j];
		}
	}
	//print the inserted element
	cout<<"Elements are: ";
	for(i=0;i<2;i++)
	{
		for(j=0;j<2;j++)
		{
			cout<<" "<<mat[i][j];
		}
		cout<<endl;
	}
	//transpose rule start
	for(i=0;i<2;i++)
	{
		for(j=0;j<2;j++)
		{
			tmat[i][j]=mat[j][i];
		}
	}
	//print the transpose matrix element
	cout<<"Transpose elements are: ";
	for(i=0;i<2;i++)
	{
		for(j=0;j<2;j++)
		{
			cout<<" "<<tmat[i][j];
		}
		cout<<endl;
	}
	return 0;
}

IDE Used To Test This Code : Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment