0

I am developing a function to transpose an array (i.e. a[i][j] => a[j][i]). To test the function, the following array is created: array[4][3] = {...} But a compiler returns the error:

E:\CodeBlock\2_7.cpp||In function 'int main()':|
E:\CodeBlock\2_7.cpp|13|error: cannot convert 'int (*)[3]' to 'const int* const*' for argument '1' to 'int** transpose(const int* const*, unsigned int, unsigned int)'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|

As I understand the type of array is int**. So, how can transposefunction take up this array?

#include <iostream>
#include <cstdlib>

using namespace std;

int ** transpose(const int * const * m, unsigned rows, unsigned cols);

int main()
{
    int array[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
    int ** arrayT = {};
    arrayT = transpose(array, 4, 3);
    return 0;
}

int ** transpose(const int * const * m, unsigned rows, unsigned cols)
{

  int **tr = new int *[cols];
  for (unsigned i = 0; i<cols; i++){
    tr[i] = new int[rows];
    for (unsigned j = 0; j < rows; j++) {
      tr[i][j] = m[j][i];
    }
  }
  return tr;
}
A user
  • 9
  • 5
  • 2
    [An array of arrays is not the same as a pointer to pointer](http://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456). Try using `std::vector` or `std::array` together with references instead. – Some programmer dude Nov 23 '16 at 13:23
  • In C++, using a 2D array to represent a matrix is usually the wrong choice. Use a class that stores the data in a contiguous single array and index by multiplication. Or choose one of the bazillion such libraries that already exist. It's difficult and time-consuming to do it right. – Peter Nov 23 '16 at 14:09

2 Answers2

0

You could either reinterpret_cast array to (int *) or use template function.

... = transpose(reinterpret_cast<int *>(array),...);

and in transpose, you must use m as a 1D array, calculate the actul index yourself: array[i][j] becomes m[i*cols+j].

or declare transpose as

template <size_t kRows, size_t kCols>
... transpose(const int (&m)[kRows][kCols]) {
  //you may use kCols and kRows as const variables, and use m as a 2D array
}

calling :

... = transpose(array);
felix
  • 1,919
  • 5
  • 15
0

If you have pass an 2d array to function and use it like arr[i][j]. You should then create all arrays using dynamic allocation and pass double pointers to function.Your code then should look like

#include <iostream>
#include <cstdlib>

using namespace std;

int** create_array( int r, int c ){
    int** arr = new int*[r];
    for( int i=0; i<r; i++ ){
        arr[i] = new int[c];
    }
    return arr;
}

void delete_array( int** arr, int r){
    for( int i=0; i<r; i++ ) delete [] arr[i];
    delete [] arr;
}

int** transpose(int** arr, int r, int c)
{

  int **tr = create_array( c, r );
  for (int i = 0; i<c; i++){
    for (int j = 0; j < r; j++) {
      tr[i][j] = arr[j][i];
    }
  }
  return tr;
}


int main()
{   int r = 4, c = 3, cnt=0;
    int** m = create_array( r, c );
    for( int i=0; i<r; i++ )
        for( int j=0; j<c; j++ )
            m[i][j] = ++cnt;
    int** tr = transpose(m,r,c);

    for( int i=0;i<c; i++){
        for( int j=0;j <r; j++ )
            cout << tr[i][j] << " ";
        cout << endl;
    }

    delete_array(m,r);   // dont forget to delete dynamic allocated mem
    delete_array(tr,c);  // dont forget to delete dynamic allocated mem
    return 0;
}
Ankit Vallecha
  • 624
  • 3
  • 16
  • OP asked about 2D arrays and you are saying use 1D array of 1D arrays. How does that answer the question? – stark Nov 23 '16 at 15:14
  • 2D arrays are also 1D array of 1D array in c, thats what the defination of 2D array, secondly i wrote the code just to show whenever you want to pass 2D array to a function, it will be problem in some way like col has to constant or you have to use single pointer and use *(*(arr+i)+j) for arr[i][j] so with dynamic allocation these can be overcome – Ankit Vallecha Nov 23 '16 at 15:21
  • Thanks for you explanation. Now I see that the to pass a 2D array to a function is to create the array as dynamic. – A user Nov 24 '16 at 11:10