数组可以作为参数传递给函数。在此程序中,我们将通过将二维数组的元素传递给函数来执行其显示。
Begin The 2D array n[][] passed to the function show(). Call function show() function, the array n (n) is traversed using a nested for loop. End
#include <iostream>
using namespace std;
void show(int n[4][3]);
int main() {
int n[4][3] = {
{3, 4 ,2},
{9, 5 ,1},
{7, 6, 2},
{4, 8, 1}};
show(n);
return 0;
}
void show(int n[][3]) {
cout << "Printing Values: " << endl;
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 3; ++j) {
cout << n[i][j] << " ";
}
}
}输出结果
Printing Values: 3 4 2 9 5 1 7 6 2 4 8 1