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

7
  • Don't use 2D arrays. Use 1D arrays, perhaps encapsulating them in some class which has member functions to access or modify them. Commented Mar 31, 2015 at 5:56
  • @Basile Starynkevitch: Do you have any simpe way? Thanks Commented Mar 31, 2015 at 5:57
  • Declare a 1D array or vector, and use a[i*width+j] instead of a[i][j] Commented Mar 31, 2015 at 5:58
  • 1
    You should avoid 2D arrays. It is very likely that you'll get confused. And there is no need for them. Commented Mar 31, 2015 at 6:01
  • 1
    You could always search the net for a proper answer. One I could easily found is here, on stackoverflow: stackoverflow.com/a/8767247/1432385 Commented Mar 31, 2015 at 6:02

5 Answers 5

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])
Sign up to request clarification or add additional context in comments.

7 Comments

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
@user8430 This has type safety, as mentioned in the answer. A pointer to pointer doesn't have that.
Good knowledge. Thank you.
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 <size_t N, size_t M> void set_P(int (&P)[N][M])
@user8430 There isn't a way to do that, because arrays are not copyable.
|
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.

1 Comment

This does not solve the OP's problem - you can't pass a 2D array of int as an int **.
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

Comments

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]

1 Comment

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.
-1
N=5;
L=10;    
void show_P( int ( &P )[N][L] )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.