#include<iostream>
using namespace std;
void transpose(const int input[2][3], int (&output)[3][2]){
for(int i=0; i<2; ++i) {
for(int j=0; j<3 ; ++j) {
output [j][i] = input [i][j];
}
}
}
void printMultiArray(const int multi[2][3], const int len){
for (int row=0; row<2; row++){
for (int col=0; col<3; col++){
cout << multi[row][col] << " " ;
}
cout << endl;
}
}
int main(){
int multi[2][3] = {{1, 2, 3}, {7, 8, 9}};
int empty[3][2];
printMultiArray(multi, 6);
cout << "... space line ..." << endl;
transpose(multi, empty);
printMultiArray(empty, 6);
return 0;
}
I have the above code to tranpose a 2x3 array... but it does not compile and fails with:
6-3-transposeArray.cpp: In function ‘int main()’:
6-3-transposeArray.cpp:33: error: cannot convert ‘int (*)[2]’ to ‘const int (*)[3]’ for argument ‘1’ to ‘void printMultiArray(const int (*)[3], int)’
I am not sure what the problem is though. It seems to be complaining about the 1st arg to transpose() but printMultiArray() seems to have no problem with the array being passed in the same manner.
Secondly is there a more generic way to implement this? (e.g. a generic func that could take in 2x3, 2x4 and 2x5 arrays and return the transpose of each)
Bit of a basic question but Any help appreciated :)