C 語 言 的 陣 列 可 以 有 多 重 索 引 , 標 準 C 語 言 稱 為多 維 陣 列(multidimensional arrays),經常會用來表示表格(tables),其數值是依照列(rows)和行(columns)排列的資料所組成。 為了指出特定的表格元素,必須指定兩個索引:㇐般而言,第㇐個索引會指定元素的列數,第二個索引則會指定元素的行數。使用兩個索引來指定表格或陣列中某個特定元素的陣列,稱為二 維陣 列(doubledimensional arrays),多維陣列能夠有兩個以上的索引。



6.11.1 二維陣列的示範 圖6.20示範㇐個二維陣列 a,這個陣列包含三列以及四行,因此它稱為了3×4 的陣列,㇐個有 m 列和 n 行的陣列就稱為m ×n陣列(m-by-narray) 。 在圖6.20中,陣列 a 的每個元素都可以用 a[i][j] 的方式來加以指定元素名稱,其中 a 是陣列名稱,而 i 和 j 則會用來指出a 的每個元素的索引。第㇐列(Row 0)所有元素名稱的第㇐個索引都是 0;而第四行(Column3)所有元素名稱的第二個索引都是 3。 圖6.20 6.16.26.36.46.56.66.76.86.96.10第87頁 6.11摘要6.11.2 二維陣列的初始值 多維陣列可以和㇐維陣列㇐樣,在宣告時指定其初始值。例如,二維陣列int b[2][2]可以用以下的方式來宣告並且指定初始值: 這些數值會以列為單位,並且用大括號括起來。第㇐組大括號的值會初始化第 0 列,第二組大括號的值會初始化第 1 列。所以第㇐組的值1和值 2 分别會初始化陣列元素 b[0][0]和b[0][1]。第二組的值3 和值4則分別初始化陣列元素 b[1][0]和b[1][1]。如果給某㇐列的初始值個數不夠的話,則此列剩下的元素會將初始值設定為零。 以下敘述會將b[0][0]初始化成 1、b[1][0]初始化成3、b[1][1]初始化成4。
// Fig. 6.21: fig06_21.c // Initializing multidimensional arrays.
#include <stdio.h>
void printArray(int a[][3]); // function prototype
// function main begins program execution
int main(void)
{
int array1[2][3] = {{1, 2, 3}, {4, 5, 6}};
puts("Values in array1 by row are:");
printArray(array1);
int array2[2][3] = {{1, 2, 3}, {4, 5, 0}}; // Fixed initialization
puts("Values in array2 by row are:");
printArray(array2);
int array3[2][3] = {{1, 2, 0}, {4, 0, 0}}; // Fixed initialization
puts("Values in array3 by row are:");
printArray(array3);
return 0;
}
// function to output array with two rows and three columns
void printArray(int a[][3])
{
// loop through rows
for (size_t i = 0; i < 2; ++i)
{
// output column values
for (size_t j = 0; j < 3; ++j)
{
printf("%d ", a[i][j]);
}
printf("\\n"); // start a new line of output
}
}









// Fig. 6.22: fig06_22.c
// Two-dimensional array manipulations.
#include <stdio.h>
#define STUDENTS 3
#define EXAMS 4
// Function prototypes
int minimum(const int grades[][EXAMS], size_t pupils, size_t tests);
int maximum(const int grades[][EXAMS], size_t pupils, size_t tests);
double average(const int setOfGrades[], size_t tests);
void printArray(const int grades[][EXAMS], size_t pupils, size_t tests);
// Function main begins program execution
int main(void)
{
// Initialize student grades for three students (rows)
int studentGrades[STUDENTS][EXAMS] = {
{77, 68, 86, 73},
{96, 87, 89, 78},
{70, 90, 86, 81}};
// Output array studentGrades
puts("The array is:");
printArray(studentGrades, STUDENTS, EXAMS);
// Determine smallest and largest grade values
printf("\\n\\nLowest grade: %d\\nHighest grade: %d\\n",
minimum(studentGrades, STUDENTS, EXAMS),
maximum(studentGrades, STUDENTS, EXAMS));
// Calculate average grade for each student
for (size_t student = 0; student < STUDENTS; ++student)
{
printf("The average grade for student %zu is %.2f\\n", student, average(studentGrades[student], EXAMS));
}
return 0;
}
// Find the minimum grade
int minimum(const int grades[][EXAMS], size_t pupils, size_t tests)
{
int lowGrade = 100; // Initialize to highest possible grade
// Loop through rows of grades
for (size_t i = 0; i < pupils; ++i)
{
// Loop through columns of grades
for (size_t j = 0; j < tests; ++j)
{
if (grades[i][j] < lowGrade)
{
lowGrade = grades[i][j];
}
}
}
return lowGrade; // Return minimum grade
}
// Find the maximum grade
int maximum(const int grades[][EXAMS], size_t pupils, size_t tests)
{
int highGrade = 0; // Initialize to lowest possible grade
// Loop through rows of grades
for (size_t i = 0; i < pupils; ++i)
{
// Loop through columns of grades
for (size_t j = 0; j < tests; ++j)
{
if (grades[i][j] > highGrade)
{
highGrade = grades[i][j];
}
}
}
return highGrade; // Return maximum grade
}
// Determine the average grade for a particular student
double average(const int setOfGrades[], size_t tests)
{
int total = 0; // Sum of test grades
// Total all grades for one student
for (size_t i = 0; i < tests; ++i)
{
total += setOfGrades[i];
}
return (double)total / tests; // Average
}
// Print the array
void printArray(const int grades[][EXAMS], size_t pupils, size_t tests)
{
// Output column heads
printf("%s"," \\t[0]\\t [1]\\t [2]\\t [3]");
// Output grades in tabular format
for (size_t i = 0; i < pupils; ++i)
{
// Output label for row
printf("\\nstudentGrades[%u]",i);
// Output grades for one student
for (size_t j = 0; j < tests; ++j)
{
printf("%-5d\\t", grades[i][j]);
}
printf("\\n");
}
}



