Help in array and function...

I am extremely new to this forum...help needed...
I know how to pass an array in function definition... but how to return an array from the function to the main()...
I basically want to create a function that will take no. of rows and columns as arguments and prompt the user to enter data in the 2d array...and RETURN the same array to the main()...so that I can further use it...

A quick help will be highly acknowledged and respected...
Regards and thank you in anticipation...
Arrays cannot be returned, because arrays are not copied to either direction.
Objects can be returned, because objects can copy themselves.

When do you know the "no. of rows and columns"?

* If before compiling the code, then a static array is possible.

* If only when the program is already running, then the memory has to be allocated dynamically.

For the latter, the std::vector is quite convenient.
For example, in
std::vector<std::vector<float>> foo;
The foo is essentially a 2D matrix of float values (except that each "row" might have different amount of "columns".

See http://www.cplusplus.com/reference/vector/vector/
In line with what keskiverto mentions, here is an example of returning and passing static arrays from and to functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

size_t nRows = 4;
size_t nCols = 5;

template <typename T>
T **return2DArray()
{
    T **twoDArray;
    twoDArray = new T*[nRows];
    for( int i = 0 ; i < nRows ; i++ )
    {
         twoDArray[i] = new T [nCols];
    }
    srand(time(NULL));
    for (size_t i = 0; i < nRows; i++)
    {
        for (size_t j = 0; j < nCols; j++)
        {
            twoDArray[i][j] =(rand() % 100 + 1);
        }
    }

    return twoDArray;
}
template <typename T>
void pass2DArray (T** twoDArray)
{
     for (size_t i = 0; i < nRows; i++)
      {
          for (size_t j = 0; j < nCols; j++)
          {
              cout << twoDArray[i][j] << "\t";
          }
            cout<<"\n";
      }
}

template <typename T>
void free2DArray(T** twoDArray)
{
      delete [] *twoDArray;
      delete [] twoDArray;
}

int main()
{
    int **my2dArr = return2DArray <int>();

    pass2DArray(my2dArr);//template already instantiated, hence template parameter optional

    free2DArray(my2dArr); // ditto as above
}


PS: using the 'old fashioned' randomiser, having some problems with the C++11 approach, giving same nos at every run!
new and delete are dynamic allocation, not static!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void pass2DArray ( float ** twoDArray, size_t R, size_t C )
{
  for ( size_t row = 0; row < R; ++row ) {
    for ( size_t col = 0; col < C; ++col ) {
      twoDArray[ row ][ col ] = row*C + col;
    }
  }
}

int main()
{
  constexpr size_t nRows = 4;
  constexpr size_t nCols = 5;

  float my2dArr[nRows][nCols]; // statically allocated array

  pass2DArray( my2dArr, nRows, nCols );

  for ( size_t row = 0; row < nRows; ++row ) {
    for ( size_t col = 0; col < nCols; ++col ) {
      std::cout << ' ' << twoDArray[ row ][ col ];
    }
    std::cout << '\n';
  }
}
new and delete are dynamic allocation, not static!

Agreed, but I'm not sure I could still claim that my program returns AND passes dynamic arrays. Returning a truly dynamic array is feasible and can be easily achieved by adding in a couple of user entry lines within return2DArray(). But passing 2D arrays ... I was wondering how to pass the row and col info to the pass2Darray() function and took the route of declaring the global variables nRows, nCols. Otherwise just with the pointer to pointer passed to the function it would not be possible to glean the row and col data from that. Alternatively, of course, I could have done a la keskiverto passing the pointer to pointer, row and col info all together
Thanx to all of you...i got it...yeah...
Topic archived. No new replies allowed.