อาร์เรย์สามารถส่งผ่านไปยังฟังก์ชันเป็นอาร์กิวเมนต์ได้ ในโปรแกรมนี้ เราจะดำเนินการเพื่อแสดงองค์ประกอบของอาร์เรย์ 2 มิติโดยส่งผ่านไปยังฟังก์ชัน
อัลกอริทึม
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