ภาษา C อนุญาตให้มีอาร์เรย์ 3 มิติ หรือมากกว่า เรียกว่าอาร์เรย์หลายมิติ
ขีดจำกัดที่แน่นอนขึ้นอยู่กับคอมไพเลอร์
ไวยากรณ์มีดังนี้:
datatype arrayname [size1] [size2] ----- [sizen];
ตัวอย่าง สำหรับอาร์เรย์ 3 มิติ:
int a[3] [3] [3];
จำนวนองค์ประกอบ = 3*3*3 = 27 องค์ประกอบ
ตัวอย่าง
โปรแกรม C คำนวณผลรวมแถวและคอลัมน์ของอาร์เรย์ 5x5 โดยใช้การคอมไพล์ระหว่างรัน:
void main() {
// ประกาศอาร์เรย์และตัวแปร //
int A[5][5], i, j, row = 0, column = 0;
// อ่านองค์ประกอบเข้าไปในอาร์เรย์ //
printf("Enter elements into the array : \n");
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
printf("A[%d][%d] : ", i, j);
scanf("%d", &A[i][j]);
}
}
// คำนวณผลรวมองค์ประกอบในทุกแถว //
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
row = row + A[i][j];
}
printf("The sum of elements in row number %d is : %d\n", i, row);
row = 0;
}
// คำนวณผลรวมองค์ประกอบในทุกคอลัมน์ //
for(j = 0; j < 5; j++) {
for(i = 0; i < 5; i++) {
column = column + A[i][j];
}
printf("The sum of elements in column number %d is : %d\n", i, column);
column = 0;
}
}ผลลัพธ์
เมื่อรันโปรแกรม จะได้ผลลัพธ์ดังนี้:
Enter elements into the array: A[0][0] : 1 A[0][1] : 2 A[0][2] : 4 A[0][3] : 3 A[0][4] : 5 A[1][0] : 2 A[1][1] : 5 A[1][2] : 6 A[1][3] : 7 A[1][4] : 2 A[2][0] : 3 A[2][1] : 6 A[2][2] : 2 A[2][3] : 6 A[2][4] : 7 A[3][0] : 2 A[3][1] : 7 A[3][2] : 4 A[3][3] : 3 A[3][4] : 1 A[4][0] : 4 A[4][1] : 5 A[4][2] : 6 A[4][3] : 7 A[4][4] : 8 The sum of elements in row number 0 is: 15 The sum of elements in row number 1 is: 22 The sum of elements in row number 2 is: 24 The sum of elements in row number 3 is: 17 The sum of elements in row number 4 is: 30 The sum of elements in column number 5 is: 12 The sum of elements in column number 5 is: 25 The sum of elements in column number 5 is: 22 The sum of elements in column number 5 is: 26 The sum of elements in column number 5 is: 23