Pass a 2D array to a C++ Function
To pass a 2D array in C++, a loop will be used so that the elements of the array could pass one by one. Now follow the below-written steps to pass a 2D array to a C++ function:
Step 1: In all C++ codes, the first step is to add the header files:
using namespace std;
Step 2: In a 2D array there are two dimensions: rows and columns. This is a very important step of the process where the user has to create a function to display/print the values of a 2D array; here I named this function as show() function:
}
Note: I have named the function show(), but the user can choose any other name too. The argument inside the function is a 2D array i.e. [rows] [columns]. While creating the function with an array argument, the row indices can be left empty but ensure that the column index has a value inside it otherwise your code may get an error.
Step 3: Inside the show function, two for() loops will be added, one that will run to display rows and the second for the column. Here, x represents the number of rows:
for (int x = 0; x < 4; ++x)
{
}
Note: Ensure that this for loop is enclosed inside the show function’s braces. The x is a variable which will be set equal to 0 at initializing and later will increment till the number of rows. I want an array with 4 rows so here x will run from 0-3 (4 times).
Now for columns, another for loop will be added. y shows the number of columns. But the second for loop should be enclosed inside the braces of the first for loop. A cout command will also be used to print the value of each location of the array one by one as the loop run:
{
cout<< "Location[" << x << "][" << y << "]: " << n[x][y] <<endl;
}
Step 4: Now finally start the main function, and pass the arguments/elements of the array:
int n[4][2] = {
{5, 4},
{2, 6},
{1, 9},
{3, 8},
};
Note: Ensure that you are following the correct syntax to pass the elements of an array.
Step 5: Now, the last step is to call the function, which was made in Step 2.
The entire code should look like as shown below:
using namespace std;
void show(int n[][2]) {
cout<< "Showing Values on Each Location: " <<endl;
for (int x = 0; x < 4; ++x) {
for (int y = 0; y < 2; ++y) {
cout<< "Location[" << x << "][" << y << "]: " << n[x][y] <<endl;
}
}
}
int main() {
int n[4][2] = {
{5, 4},
{2, 6},
{1, 9},
{3, 8},
};
show(n);
return 0;
}
Step 6: Run the code and see the output:
The real-life representation of our created array is as following:
Array | Column 0 | Column 1 |
---|---|---|
Row 0 | 5 | 4 |
Row 1 | 2 | 6 |
Row 2 | 1 | 9 |
Row 3 | 3 | 8 |
That’s it for the process, now by following this method, the users can create multiple 2D arrays with different values for rows and columns.
Conclusion
To pass a 2D array to a C++ function, the user needs to create a function() with an array argument in such a way that two indices of the array are defined as; [row index] [column index]. The row index can be left empty but ensure that the column index has some value in it. Then inside the braces of function, use for loops to pass and print the elements of the array. Then finally inside the main program assign the array elements and call the function.