将二维数组传递给 C++ 函数


数组作为参数传递给一个函数。在本程序中,我们将通过将一个二维数组传递给一个函数来显示它的元素。

算法

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

更新于: 30-07-2019

2K+ 浏览量

开启你的 职业生涯

完成课程即可获得认证

开始学习
广告
© . All rights reserved.