1

I have a 2D array that defined as

int P[5][10];
for (int i=0;i<N;i++)
{
    for(int j=0;j<L;j++)
    {
        if(random()>0.5)
            P[i][j]=1;
        else
            P[i][j]=0;
    }
}

I want to make a function with input is P. The function allows us show the value of P. How to defined that function. I tried such as

void show_P(int P[][], int numcols,int numrows)

However, it is wrong. Could you help me fix it? Thanks

Jame
  • 3,354
  • 4
  • 39
  • 85

5 Answers5

3

If you want to restrict the arguments to 5 by 10 2D arrays, you can pass by reference like this:

void show_P(int (&P)[5][10])

This will fail for any other type of array. If you want the function to work for other sizes, you can make it a template.

template <size_t N, size_t M>
void show_P(int (&P)[N][M])
Paul R
  • 195,989
  • 32
  • 353
  • 519
juanchopanza
  • 210,243
  • 27
  • 363
  • 452
  • Is it safety way? That means we don't worry about memory problem. Someone suggest to me using 2D points array. However, I think your suggestion is very simple. Thanks – Jame Mar 31 '15 at 06:00
  • @user8430 This has type safety, as mentioned in the answer. A pointer to pointer doesn't have that. – juanchopanza Mar 31 '15 at 06:01
  • Good knowledge. Thank you. – Jame Mar 31 '15 at 06:06
  • Can your way apply for returning 2D array? That means if I want to write a function to return 2D array, can I apply template void set_P(int (&P)[N][M]) – Jame Mar 31 '15 at 06:31
  • @user8430 That isn't "returning a 2D array", but modifying an input parameter. Yes, you can kind of do that, but only by setting the elements of the array. Arrays are not assignable or copyable like, say, `int`s or `double`s are. – juanchopanza Mar 31 '15 at 06:34
  • Could you show to me one way to apply the work "return 2D array"?Thanks – Jame Mar 31 '15 at 06:35
  • 1
    @user8430 There isn't a way to do that, because arrays are not copyable. – juanchopanza Mar 31 '15 at 06:36
1

You could just change it to:

void show_P(int** P, int numcols, int numrows) 

Passing 2D array always using the pointer.

Hope this will help.

WindMemory
  • 480
  • 1
  • 4
  • 12
1

Or using the std::array

void printArray(array<array<int,2>,3>& arr)
{
  for (auto x : arr)
  {
    for (auto y : x)
    {
      cout << y << endl;
    }
  }
}

int main()
{
  array<array<int,2>,3> arr{{{1, 2}, {2, 3}, {3, 4}}};
  arr[0][1] = 5;
  printArray(arr);
}

would give you:

1

5

2

3

3

4

4386427
  • 33,845
  • 4
  • 32
  • 53
0
void print( int (&ref)[5][10]) {

for( auto &lm: ref) // get the first array from the multidimensional array and initialize lm with it 
    for( auto &elem: lm) // get the first element from lm 
        std::cout << elem << " "; // print the element 

}

This only works with an array with dimensions as P i.e, [5][10]

  • I have made 'lm' a reference because if we don't then lm will not be a reference to an array but rather have a type of int * which is not that we want it to be. – Cpp Programmer Mar 31 '15 at 06:22
-1
N=5;
L=10;    
void show_P( int ( &P )[N][L] )
John
  • 2,308
  • 7
  • 20
  • 55