Is it possible to create a transpose of a matrix by using only one 2D array(input array)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it possible to create a transpose of a matrix by using only one 2D array(input array)?

I was thinking of making either changes in the input matrix or using another temporary matrix to copy data and to delete the input matrix (if this is possible). I am not really sure what we should do I'm just curious.

12th Sep 2016, 1:43 PM
Infinity
Infinity - avatar
6 Answers
+ 1
http://www.sololearn.com/app/cplusplus/playground/cZ3g6qhH7Yf8/ #include <iostream> using namespace std; template <int _size> void print2dim(int matrix[][_size]) { for (int i=0; i<_size; ++i) { for (int j=0; j<_size; ++j) cout << matrix[i][j]; cout << endl; } } template <int _size> void transpose(int matrix[][_size]) { for (int i=1; i<_size; ++i) for (int j=0; j<i; ++j) { int tmp=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=tmp; } } int main() { int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; print2dim(matrix); cout << endl; transpose(matrix); print2dim(matrix); return 0; }
16th Sep 2016, 3:23 PM
kiwiyou
kiwiyou - avatar
0
of course you can if the matrix is a square matrix.
12th Sep 2016, 1:55 PM
kiwiyou
kiwiyou - avatar
0
but I want use just one matrix
12th Sep 2016, 1:57 PM
Infinity
Infinity - avatar
0
you can.
13th Sep 2016, 1:22 PM
kiwiyou
kiwiyou - avatar
0
can you write a code I mean the sample
16th Sep 2016, 2:57 PM
Infinity
Infinity - avatar
0
thanks you very much
17th Sep 2016, 2:37 PM
Infinity
Infinity - avatar