+ 1

program in C++ to find the transpose of a given matrix

20th Nov 2016, 2:48 PM
Soutik
Soutik - avatar
3 Antworten
+ 1
Scott is right it is simple store it in multi dimensional array and give rows into column and column into rows. #include <iostream. h> int main() { int I,j, r,c; int a[10][10], trans [10][10]; cout<<"Enter the no of rows and columns to be made"; cin>>r>>c; // let's make the array for(i=0;i<r;i++) for (j=0;j<c;j++) { cout<<"Enter elements of <<i+1<<j+1; cin>>a[i][j]; cout<<endl; } } cout<<endl<<"Transpose of matrix"; // this will print the matrix cout<<":"<<endl for(i=0; i<r;++i) for(j=0;j<c;j++) { cout<<" "<<a[i][j]; if (j==c-i) cout<<endl; } // now transposing matrix for(i=0;i<r;++i) for(j=0;j<c;++j) { trans[i][j]=a[j][i] } cout<<endl<<"Transpose is this" for(i=0;i<c;++i) for(j=0;j<r;++j) { cout<< ""<<trans[i][j]; if(j==r-1) cout<<endl; } return 0 } I have learnt this program somewhere
20th Nov 2016, 3:50 PM
Sandeep Chatterjee
0
I made this available on my profile so you can run it and try it out. #include <iostream> using namespace std; int main () { const int N = 3; // Size of the N by N matrix int Matrix [N][N]; // Create matrix of N*N int Transpose [N][N]; // Elements will be moved into the transpose matrix /* Example matrix for demonstration*/ for (int i = 0; i < N; i++) Matrix[0][i] = i + 1; for (int i = 0; i < N; i++) Matrix[1][i] = i + 4; for (int i = 0; i < N; i++) Matrix[2][i] = i + 7; // Print out the matrix for (int i = 0; i < N; i++) { cout << endl; for (int j = 0; j < N; j++) cout << Matrix[i][j] << ' '; } cout << endl; /* */ // Transpose the N by N matrix we set up above for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { Transpose[j][i] = Matrix[i][j]; } // Print the transposed matrix for (int i = 0; i < N; i++) { cout << endl; for (int j = 0; j < N; j++) cout << Transpose[i][j] << ' '; } cout << endl; return 0; }
20th Nov 2016, 3:34 PM
scott johnson
scott johnson - avatar
0
Here is the code snippet to find transpose of a matrix : /* transpose[i][j] = inputMatrix[j][i] */ for(rowCounter = 0; rowCounter < rows; rowCounter++){ for(colCounter = 0; colCounter < cols; colCounter++){ transposeMatrix[colCounter][rowCounter] = inputMatrix[rowCounter][colCounter]; } } Source : http://www.techcrashcourse.com/2015/03/c-program-to-find-transpose-of-matrix.html http://www.techcrashcourse.com/2016/04/cpp-program-find-transpose-matrix.html
15th Apr 2017, 1:27 PM
Arun Kumar
Arun Kumar - avatar