原文: https://www.programiz.com/c-programming/c-multi-dimensional-arrays

在本教程中,您将借助示例学习使用多维数组(二维和三维数组)。

在 C 编程中,您可以创建一个数组数组。 这些数组称为多维数组。 例如,

  1. float x[3][4];

在此,x是二维(2d)数组。 该数组可以容纳 12 个元素。 您可以将数组视为具有 3 行的表,每行有 4 列。

C 多维数组 - 图1

同样,您可以声明三维(3d)数组。 例如,

  1. float y[2][4][3];

在这里,数组y可以容纳 24 个元素。


初始化多维数组

这是初始化二维和三维数组的方法:


二维数组的初始化

  1. // Different ways to initialize two-dimensional array
  2. int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
  3. int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
  4. int c[2][3] = {1, 3, 0, -1, 5, 9};

3D 数组的初始化

您可以用类似于二维数组的类似方式初始化三维数组。 这是一个例子

  1. int test[2][3][4] = {
  2. {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
  3. {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};

示例 1:用于存储和打印值的二维数组

  1. // C program to store temperature of two cities of a week and display it.
  2. #include <stdio.h>
  3. const int CITY = 2;
  4. const int WEEK = 7;
  5. int main()
  6. {
  7. int temperature[CITY][WEEK];
  8. // Using nested loop to store values in a 2d array
  9. for (int i = 0; i < CITY; ++i)
  10. {
  11. for (int j = 0; j < WEEK; ++j)
  12. {
  13. printf("City %d, Day %d: ", i + 1, j + 1);
  14. scanf("%d", &temperature[i][j]);
  15. }
  16. }
  17. printf("\nDisplaying values: \n\n");
  18. // Using nested loop to display vlues of a 2d array
  19. for (int i = 0; i < CITY; ++i)
  20. {
  21. for (int j = 0; j < WEEK; ++j)
  22. {
  23. printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]);
  24. }
  25. }
  26. return 0;
  27. }

输出

  1. City 1, Day 1: 33
  2. City 1, Day 2: 34
  3. City 1, Day 3: 35
  4. City 1, Day 4: 33
  5. City 1, Day 5: 32
  6. City 1, Day 6: 31
  7. City 1, Day 7: 30
  8. City 2, Day 1: 23
  9. City 2, Day 2: 22
  10. City 2, Day 3: 21
  11. City 2, Day 4: 24
  12. City 2, Day 5: 22
  13. City 2, Day 6: 25
  14. City 2, Day 7: 26
  15. Displaying values:
  16. City 1, Day 1 = 33
  17. City 1, Day 2 = 34
  18. City 1, Day 3 = 35
  19. City 1, Day 4 = 33
  20. City 1, Day 5 = 32
  21. City 1, Day 6 = 31
  22. City 1, Day 7 = 30
  23. City 2, Day 1 = 23
  24. City 2, Day 2 = 22
  25. City 2, Day 3 = 21
  26. City 2, Day 4 = 24
  27. City 2, Day 5 = 22
  28. City 2, Day 6 = 25
  29. City 2, Day 7 = 26

示例 2:两个矩阵的总和

  1. // C program to find the sum of two matrices of order 2*2
  2. #include <stdio.h>
  3. int main()
  4. {
  5. float a[2][2], b[2][2], result[2][2];
  6. // Taking input using nested for loop
  7. printf("Enter elements of 1st matrix\n");
  8. for (int i = 0; i < 2; ++i)
  9. for (int j = 0; j < 2; ++j)
  10. {
  11. printf("Enter a%d%d: ", i + 1, j + 1);
  12. scanf("%f", &a[i][j]);
  13. }
  14. // Taking input using nested for loop
  15. printf("Enter elements of 2nd matrix\n");
  16. for (int i = 0; i < 2; ++i)
  17. for (int j = 0; j < 2; ++j)
  18. {
  19. printf("Enter b%d%d: ", i + 1, j + 1);
  20. scanf("%f", &b[i][j]);
  21. }
  22. // adding corresponding elements of two arrays
  23. for (int i = 0; i < 2; ++i)
  24. for (int j = 0; j < 2; ++j)
  25. {
  26. result[i][j] = a[i][j] + b[i][j];
  27. }
  28. // Displaying the sum
  29. printf("\nSum Of Matrix:");
  30. for (int i = 0; i < 2; ++i)
  31. for (int j = 0; j < 2; ++j)
  32. {
  33. printf("%.1f\t", result[i][j]);
  34. if (j == 1)
  35. printf("\n");
  36. }
  37. return 0;
  38. }

输出

  1. Enter elements of 1st matrix
  2. Enter a11: 2;
  3. Enter a12: 0.5;
  4. Enter a21: -1.1;
  5. Enter a22: 2;
  6. Enter elements of 2nd matrix
  7. Enter b11: 0.2;
  8. Enter b12: 0;
  9. Enter b21: 0.23;
  10. Enter b22: 23;
  11. Sum Of Matrix:
  12. 2.2 0.5
  13. -0.9 25.0

示例 3:三维数组

  1. // C Program to store and print 12 values entered by the user
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int test[2][3][2];
  6. printf("Enter 12 values: \n");
  7. for (int i = 0; i < 2; ++i)
  8. {
  9. for (int j = 0; j < 3; ++j)
  10. {
  11. for (int k = 0; k < 2; ++k)
  12. {
  13. scanf("%d", &test[i][j][k]);
  14. }
  15. }
  16. }
  17. // Printing values with proper index.
  18. printf("\nDisplaying values:\n");
  19. for (int i = 0; i < 2; ++i)
  20. {
  21. for (int j = 0; j < 3; ++j)
  22. {
  23. for (int k = 0; k < 2; ++k)
  24. {
  25. printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
  26. }
  27. }
  28. }
  29. return 0;
  30. }

输出

  1. Enter 12 values:
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11
  13. 12
  14. Displaying Values:
  15. test[0][0][0] = 1
  16. test[0][0][1] = 2
  17. test[0][1][0] = 3
  18. test[0][1][1] = 4
  19. test[0][2][0] = 5
  20. test[0][2][1] = 6
  21. test[1][0][0] = 7
  22. test[1][0][1] = 8
  23. test[1][1][0] = 9
  24. test[1][1][1] = 10
  25. test[1][2][0] = 11
  26. test[1][2][1] = 12