Home c++ Populating a 2D dynamic array in a separate C++ function

Populating a 2D dynamic array in a separate C++ function

Author

Date

Category

I don’t understand how to fill a 2D dynamic array in a separate function, I need to pass it as a parameter and its dimension.
I create it in main

int N = 0;
cin & gt; & gt; N;
int ** dinamic_array = new int * [N];
  for (int i = 0; i & lt; N; i ++) {
    dinamic_array [i] = new int [N];

That is, its dimension is recognized during program execution. And I need to fill it with random numbers in a SEPARATE function. I know that this is done through the size_t type, but I just can’t find the answer anywhere.


Answer 1, authority 100%

So what’s the problem, create a function:

void generateArray (int ** arr, int n) {
//Body
}

And call it like this:

generateArray (dinamic_array, n);

UPD

And yes, you may be confusing size and dimension. Size – the number of elements in the array (rows * columns, if the matrix is ​​rectangular). Dimension – the number of its dimensions (two-dimensional, three-dimensional, etc.). The dimension is determined by pointers (* array is a one-dimensional array, ** array is a two-dimensional array). You don’t need to pass this on to you. Apparently, you need to pass the size


Answer 2, authority 100%

Function for entering two-dimensional array values:

void inputArray (int ** arr, int N)
{
  cout & lt; & lt; "Enter the elements of array A" & lt; & lt; endl;
  for (int i = 0; i & lt; N; i ++)
  {
    for (int j = 0; j & lt; N; j ++)
    {
      cout & lt; & lt; "A [" & lt; & lt; i & lt; & lt; "] [" & lt; & lt; j & lt; & lt; "] =";
      cin & gt; & gt; arr [i] [j];
    }
  }
}

Function call:

inputArray (dinamic_array, N);

Answer 3, authority 100%

To begin with, you are not creating a two-dimensional array. You create several one-dimensional arrays, the first of which contains elements in the form of pointers of type int * , and all other dynamically created arrays contain elements of type int .

Therefore, in the function you should pass a pointer to the first element of an array with elements of type int * , as well as the number of elements in this array and the number of elements in each created array with elements of type int . For example:

void fill (int ** dynamic_array, size_t rows, size_t cols);

And inside a function, the corresponding loop might look like this:

for (size_t i = 0; i & lt; rows; i ++)
{
  for (size_t j = 0; j & lt; cols; j ++)
  {
     dynamic_array [i] [j] = SomeValue;
  }
}

If the number of “rows” and “columns” are equal, then it is enough to transfer only one dimension of the array. For example:

void fill (int ** dynamic_array, size_t n);

And inside a function, the corresponding loop might look like this:

for (size_t i = 0; i & lt; n; i ++)
{
  for (size_t j = 0; j & lt; n; j ++)
  {
     dynamic_array [i] [j] = SomeValue;
  }
}

Answer 4

I think this should help:

# include & lt; iostream & gt;
#include & lt; cstdlib & gt;
#include & lt; ctime & gt;
template & lt; class T & gt;
auto make_2d_array (size_t n) {
  auto ar = new T * [n];
  for (size_t i = 0; i & lt; n; i ++) {
    ar [i] = new T [n];
  }
  return ar;
}
template & lt; class T & gt;
void fill_rand (T ** ar, size_t n) {
  for (size_t i = 0; i & lt; n; ++ i) {
    for (size_t j = 0; j & lt; n; ++ j) {
      ar [i] [j] = rand ();
    }
  }
}
template & lt; class T & gt;
void print (T ** ar, size_t n) {
  for (size_t i = 0; i & lt; n; ++ i) {
    for (size_t j = 0; j & lt; n; ++ j) {
      std :: cout & lt; & lt; ar [i] [j] & lt; & lt; "";
    }
    std :: cout & lt; & lt; "\ n";
  }
}
template & lt; class T & gt;
void clear_2d_array (T ** ar, size_t n) {
  for (size_t i = 0; i & lt; n; i ++) {
    delete [] ar [i];
  }
  delete [] ar;
}
int main () {
  srand (time (nullptr));
  size_t n = 0;
  std :: cin & gt; & gt; n;
  auto a = make_2d_array & lt; int & gt; (n);
  fill_rand (a, n);
  print (a, n);
  clear_2d_array (a, n);
  return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions