<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C 多維數組 > 原文: [https://www.programiz.com/c-programming/c-multi-dimensional-arrays](https://www.programiz.com/c-programming/c-multi-dimensional-arrays) #### 在本教程中,您將借助示例學習使用多維數組(二維和三維數組)。 在 C 編程中,您可以創建一個數組數組。 這些數組稱為多維數組。 例如, ```c float x[3][4]; ``` 在此,`x`是二維(2d)數組。 該數組可以容納 12 個元??素。 您可以將數組視為具有 3 行的表,每行有 4 列。 ![Two dimensional array in C programming](https://img.kancloud.cn/f7/24/f724205881937726e583b336badf3e12_395x215.png) 同樣,您可以聲明三維(3d)數組。 例如, ```c float y[2][4][3]; ``` 在這里,數組`y`可以容納 24 個元素。 * * * ## 初始化多維數組 這是初始化二維和三維數組的方法: * * * ### 二維數組的初始化 ```c // Different ways to initialize two-dimensional array int c[2][3] = {{1, 3, 0}, {-1, 5, 9}}; int c[][3] = {{1, 3, 0}, {-1, 5, 9}}; int c[2][3] = {1, 3, 0, -1, 5, 9}; ``` * * * ### 3D 數組的初始化 您可以用類似于二維數組的類似方式初始化三維數組。 這是一個例子 ```c int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}}; ``` * * * ### 示例 1:用于存儲和打印值的二維數組 ```c // C program to store temperature of two cities of a week and display it. #include <stdio.h> const int CITY = 2; const int WEEK = 7; int main() { int temperature[CITY][WEEK]; // Using nested loop to store values in a 2d array for (int i = 0; i < CITY; ++i) { for (int j = 0; j < WEEK; ++j) { printf("City %d, Day %d: ", i + 1, j + 1); scanf("%d", &temperature[i][j]); } } printf("\nDisplaying values: \n\n"); // Using nested loop to display vlues of a 2d array for (int i = 0; i < CITY; ++i) { for (int j = 0; j < WEEK; ++j) { printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]); } } return 0; } ``` **輸出** ```c City 1, Day 1: 33 City 1, Day 2: 34 City 1, Day 3: 35 City 1, Day 4: 33 City 1, Day 5: 32 City 1, Day 6: 31 City 1, Day 7: 30 City 2, Day 1: 23 City 2, Day 2: 22 City 2, Day 3: 21 City 2, Day 4: 24 City 2, Day 5: 22 City 2, Day 6: 25 City 2, Day 7: 26 Displaying values: City 1, Day 1 = 33 City 1, Day 2 = 34 City 1, Day 3 = 35 City 1, Day 4 = 33 City 1, Day 5 = 32 City 1, Day 6 = 31 City 1, Day 7 = 30 City 2, Day 1 = 23 City 2, Day 2 = 22 City 2, Day 3 = 21 City 2, Day 4 = 24 City 2, Day 5 = 22 City 2, Day 6 = 25 City 2, Day 7 = 26 ``` * * * ### 示例 2:兩個矩陣的總和 ```c // C program to find the sum of two matrices of order 2*2 #include <stdio.h> int main() { float a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrix\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter a%d%d: ", i + 1, j + 1); scanf("%f", &a[i][j]); } // Taking input using nested for loop printf("Enter elements of 2nd matrix\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter b%d%d: ", i + 1, j + 1); scanf("%f", &b[i][j]); } // adding corresponding elements of two arrays for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { result[i][j] = a[i][j] + b[i][j]; } // Displaying the sum printf("\nSum Of Matrix:"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("%.1f\t", result[i][j]); if (j == 1) printf("\n"); } return 0; } ``` **輸出** ```c Enter elements of 1st matrix Enter a11: 2; Enter a12: 0.5; Enter a21: -1.1; Enter a22: 2; Enter elements of 2nd matrix Enter b11: 0.2; Enter b12: 0; Enter b21: 0.23; Enter b22: 23; Sum Of Matrix: 2.2 0.5 -0.9 25.0 ``` * * * ### 示例 3:三維數組 ```c // C Program to store and print 12 values entered by the user #include <stdio.h> int main() { int test[2][3][2]; printf("Enter 12 values: \n"); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { scanf("%d", &test[i][j][k]); } } } // Printing values with proper index. printf("\nDisplaying values:\n"); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]); } } } return 0; } ``` **輸出** ```c Enter 12 values: 1 2 3 4 5 6 7 8 9 10 11 12 Displaying Values: test[0][0][0] = 1 test[0][0][1] = 2 test[0][1][0] = 3 test[0][1][1] = 4 test[0][2][0] = 5 test[0][2][1] = 6 test[1][0][0] = 7 test[1][0][1] = 8 test[1][1][0] = 9 test[1][1][1] = 10 test[1][2][0] = 11 test[1][2][1] = 12 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看