0

Tried using globally declaration, but that is not useful for me.

Pragya
  • 44
  • 1
  • 5
  • You should avoid global declarations for many reasons. If you want to use some properties from one function in another function, pass the property as a argument to that function. – Farbod Ahmadian Apr 03 '20 at 06:01
  • Does this answer your question? [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – Thomas Lang Apr 03 '20 at 07:13

2 Answers2

1
int connection(vector< vector<int>> &gridOfNodes,int m,int n)
{
//Your code goes here

//Accessing Vector sample
if(gridOfNodes[i][j] == 1)
{

}
}



int main() 
{ 


 int n,m;
 cin>>n>>m;
 // initialize Vector
 vector<vector<int> > vec( n , vector<int> (m, 0));

for (int i = 0; i < n; i++) 
        for (int j = 0; j < m; j++)
            cin>>vec[i][j]; 

 //Function calling 
 cout << connection(vec,m,n);

 return 0; 
}
  • 1
    Also worth adding some indentation and `const` to the argument to avoid accidental mutations. – tadman Apr 03 '20 at 05:32
1

2D Array:

If you don't want it (the second dimension or both of them as const) to be defined in global scope, then I assume your function is in the local scope of main() or some other function - If yes then just define the dimensions and use:

function(int array[dim1][dim2])

or

function(int array[][dim2], int dim1)

2D Vector:

function(std::vector<std::vector<int>>const &vec)

Vector Array:

function(std::array<std::vector<int>>const &vecarray)

Note that the use of const specifier is only required when you are passing the 2D vector by reference. If your passing it by value, copies will be made and your original 2D vector won't be modified. (For cases such as when you would return something from the function and not be bothered about its values changed inside the function body)

Anirban166
  • 3,162
  • 4
  • 11
  • 27