อาเรย์ 2 มิติ

Array หมายถึงลำดับของข้อมูลประเภทเดียวกันที่แต่ละข้อมูลสามารถเข้าถึงได้โดยใช้หมายเลขลำดับโดยเริ่มนับจากลำดับศูนย์

Warning

to be continued...

การประกาศ

รูปแบบคำสั่งประกาศ

ประเภท ตัวแปร[จำนวนแถว][จำนวนหลัก];

ตัวอย่างการประกาศตัวแปร x สำหรับเก็บข้อมูลประเภทจำนวนเต็ม 3 แถว 4 หลัก

int x[3][4];

ตัวอย่างคำสั่งในการประกาศอาเรย์ 2 มิติพร้อมกำหนดค่า

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  int x[3][4] = {
 7    { 0, 1,  2,  3 },
 8    { 4, 5,  6,  7 },
 9    { 8, 9, 10, 11 }
10  };
11  string names[2][3] = {
12    { "Abby",    "Annie", "Anthony" },
13    { "Barnaby", "Barney", "Bernie" },
14  };
15  return 0;
16}

ข้อมูลเพิ่มเติมเกี่ยวกับประเภทของข้อมูล ดูเพิ่มเติมได้ที่ ชนิดข้อมูลพื้นฐาน

การเข้าถึง

รูปแบบคำสั่งเพื่อเข้าถึงข้อมูลในอาเรย์ 2 มิติ

ตัวแปร[แถวที่][หลักที่];

โดย

แถวที่ เป็นตัวเลขจำนวนเต็มระบุแถวของข้อมูลที่ต้องการเข้าถึงโดยแถวแรกด้านบนเป็นแถวที่ 0 หลักที่ เป็นตัวเลขจำนวนเต็มระบุหลักของข้อมูลที่ต้องการเข้าถึงโดยหลักแรกจากซ้ายเป็นหลักที่ 0

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  int x[3][4] = {
 7    { 0, 1,  2,  3 },
 8    { 4, 5,  6,  7 },
 9    { 8, 9, 10, 11 }
10  };
11  cout << x[0][2] << endl;
12  cout << x[2][3] << endl;
13  cout << x[1][0]*x[2][1] << endl;
14
15  string names[2][3] = {
16    { "Abby",    "Annie", "Anthony" },
17    { "Barnaby", "Barney", "Bernie" },
18  };
19  cout << names[1][2] << endl;
20  return 0;
21}

การรับข้อมูล

รูปแบบที่ ๑

โจทย์ให้รับข้อมูลตามจำนวนที่ระบุไว้ชัดเจนในโจทย์ เช่น

จงเขียนโปรแกรมเพื่อรับตัวเลขจำนวนเต็ม 3 แถว แถวละ 4 จำนวนที่ผู้ใช้กรอก จากนั้นแสดงค่าของจำนวนเต็ม ณ ตำแหน่ง แถวที่และหลักที่ ที่ผู้ใช้ระบุ

Input

Output

11 22 33 44

55 66 77 88

55 66 77 88

1 2

77

คำตอบ

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  int x[3][4];
 7  for (int i=0; i<3; i++) {
 8    for (int j=0; j<4; j++) {
 9      cin >> x[i][j];
10    }
11  }
12  int a, b;
13  cin >> a >> b;
14  cout << x[a][b] << endl;
15  return 0;
16}

รูปแบบที่ ๒

โจทย์ให้รับข้อมูลตามจำนวนที่ระบุไว้ในแต่ละกรณี เช่น

จงเขียนโปรแกรมเพื่อรับตารางของตัวเลขตามจำนวนแถวและจำนวนหลักที่ผู้ใช้ระบุจากนั้นแสดงค่าของตัวเลข ณ ตำแหน่งแถวและหลักที่ผู้ใช้ต้องการ

Input

Output

3 4

11 22 33 44

55 66 77 88

44 33 22 11

1 2

77

คำตอบ

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6  int r, c;
 7  cin >> r >> c;
 8  int x[r][c];
 9  for (int i=0; i<r; i++) {
10    for (int j=0; j<c; j++) {
11      cin >> x[i][j];
12    }
13  }
14  int a, b;
15  cin >> a >> b;
16  cout << x[a][b] << endl;
17  return 0;
18}

ฟังก์ชันและอาเรย์ 2 มิติ

ตัวอย่างการประกาศฟังก์ชันเพื่อรับอาเรย์ 2 มิติ พร้อมระบุจำนวนสมาชิก

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5float sum(int x[3][4]) {
 6  int sum = 0;
 7  for (auto r=0; r<3; r++) {
 8    for (auto c=0; c<4; c++) {
 9      sum += x[r][c];
10    }
11  }
12  return sum;
13}
14
15void print(string names[2][3]) {
16  for (auto r=0; r<2; r++) {
17    for (auto c=0; c<3; c++) {
18      cout << names[r][c] << ", ";
19    }
20    cout << endl;
21  }
22}
23
24int main() {
25  int x[3][4] = {
26    { 0, 1,  2,  3 },
27    { 4, 5,  6,  7 },
28    { 8, 9, 10, 11 }
29  };
30  cout << sum(x) << endl;
31
32  string names[2][3] = {
33    { "Anna", "Andy", "Anthony" },
34    { "Barney", "Bernie", "Bucky" },
35  };
36  print(names);
37
38  return 0;
39}

vector

การใช้ vector แทนอาเรย์ 2 มิติ

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5float sum(vector<vector<int>> a) {
 6  float sum = 0;
 7  for (auto v : a ) {
 8    for (auto i=0; i<v.size(); i++) {
 9      sum += v[i];
10    }
11  }
12  return sum;
13}
14
15float sum2(vector<vector<int>> a) {
16  float sum = 0;
17  for (auto v : a) {
18    for (auto x : v) {
19      sum += x;
20    }
21  }
22  return sum;
23}
24
25float sum3(vector<vector<int>> a) {
26  float sum = 0;
27  for (auto v : a ) {
28    for (auto i=v.begin(); i != v.end(); i++) {
29      sum += *i;
30    }
31  }
32  return sum;
33}
34
35float sum4(vector<vector<int>> a) {
36  float sum = 0;
37  for (auto v : a ) {
38    auto i = v.begin();
39    while (i != v.end()) {
40      sum += *i;
41      i++;
42    }
43  }
44  return sum;
45}
46
47void print(vector<vector<string>> names) {
48  for (auto v : names ) {
49    for (auto name : v) {
50      cout << setw(20) << name ;
51    }
52    cout << endl;
53  }
54}
55
56int main() {
57
58   vector<vector<int>> b {
59     { 2, 4, 7, 9 },
60     { 3, 9, 5, 4, 10 },
61     { 3 },
62   };
63   cout << sum(b) << endl;
64   
65   for (auto i=1; i<=5; i+=2) {
66     b[2].push_back(i);
67   }
68   cout << sum2(b) << endl;
69   cout << sum3(b) << endl;
70   cout << sum4(b) << endl;
71
72   vector<vector<string>> names {
73     { "Andy", "Annie"},
74     { "Barney", "Bernie"},
75   };
76   print(names);
77
78  return 0;
79}