Give a C++ code function that, given a n × n matrix M of type
float, replaces M with its transpose without the use of a temporary matrix.*/ #include <iostream> using namespace std; class Matrix { int **a; int row ,col; public: Matrix(int x=0,int y=0) { row=x; col=y; a = new int *[row]; for (int i=0;i<row;i++) { a[i]=new int [col]; } for (int i=0;i<row;i++) { for (int j=0;j<col;j++) { a[i][j]=0; } } } void getvalue() { for (int i=0;i<row;i++) { for (int j=0;j<col;j++) { cout<<"Enter Value for A["<<i<<"]["<<j<<"] : "; cin>>a[i][j]; } } } Matrix transpose() //transpose function {
for (int i=1;i<row;i++)
{ for (int j=0;j<col;j++) { int temp=a[j][i]; a[j][i]=a[i][j]; a[i][j]=temp; } } } void display() { for (int i=0;i<row;i++) { for (int j=0;j<col;j++) { cout<<a[i][j]<<" "; } cout<<endl; } } }; int main() { int r1,c1; cout<<"For Matrix 1:"<<endl<<"Enter No. of Rows:"; cin>>r1; cout<<"Enter No. of Columns:"; cin>>c1; Matrix mat1(r1,c1); mat1.getvalue(); cout<<"Matrix Formed:"<<endl; mat1.display(); mat1.transpose(); cout<<endl<<"Transpose of Given matrix is :"<<endl; mat1.display(); return 0; }