C 多維數(shù)組

2018-05-19 18:05 更新

學(xué)習(xí)C - C多維數(shù)組

二維數(shù)組可以聲明如下:

float value[25][50];

這聲明了具有25組50個浮點元素的值數(shù)組。

三維數(shù)組聲明如下。

double beans[4] [10][20]; // 4 fields, each with 10 rows of 20 beans

這將聲明一個包含800個元素的數(shù)組。

初始化多維數(shù)組

要初始化一個二維數(shù)組,將每行的初始值放在大括號{}之間,然后將括號中的所有行括起來:

int numbers[3][4] = {
                      { 10, 20, 30, 40 },          // Values for first row
                      { 15, 25, 35, 45 },          // Values for second row
                      {  7,  8,  9, 50 }           // Values for third row
                    };

您可以通過提供一個值將整個數(shù)組初始化為0:

int numbers[3][4] = {0};

三維數(shù)組將具有三級嵌套大括號,內(nèi)層包含一行初始化值集合:

int numbers[2][3][4] = {
                           {                       // First block of 3 rows
                             { 10, 20, 30, 40 },
                             { 15, 25, 35, 45 },
                             { 47, 48, 49, 50 }
                           },
                           {                       // Second block of 3 rows
                             { 10, 20, 30, 40 },
                             { 15, 25, 35, 45 },
                             { 47, 48, 49, 50 }
                          }
                       };

您需要一個嵌套循環(huán)來處理多維數(shù)組中的所有元素。

嵌套的級別將是數(shù)組維數(shù)。

這里是您如何對先前數(shù)字?jǐn)?shù)組中的元素進(jìn)行求和:


#include <stdio.h>

int main(void)
{
    int numbers[2][3][4] = {
                               {                       // First block of 3 rows
                                 { 10, 20, 30, 40 },
                                 { 15, 25, 35, 45 },
                                 { 47, 48, 49, 50 }
                               },
                               {                       // Second block of 3 rows
                                 { 10, 20, 30, 40 },
                                 { 15, 25, 35, 45 },
                                 { 47, 48, 49, 50 }
                              }
                           };
    int sum = 0;
    for(int i = 0 ; i < 2 ; ++i)
    {
      for(int j = 0 ; j < 3 ; ++j)
      {
        for(int k = 0 ; k < 4 ; ++k)
        {
          sum += numbers[i][j][k];
        }
      }
    }
    printf("The sum of the values in the numbers array is %d.", sum);
    return 0;
}

上面的代碼生成以下結(jié)果。



例子

我們可以創(chuàng)建多維數(shù)組,例如二維數(shù)組,我們可以使用[] []。


  #include <stdio.h> 

  int main() { 
      // define Multidimensional demenarray 
      int matrix[3][5]; 

      // insert data 
      int i,j; 
      for(i=0;i<3;i++){ 
          for(j=0;j<5;j++){ 
              matrix[i][j] = i+j; 
          } 
      } 

      // display data 
      for(i=0;i<3;i++){ 
          for(j=0;j<5;j++){ 
              printf("%d ",matrix[i][j]); 
          } 
          printf("\n"); 
      } 

      return 0; 
  } 

上面的代碼生成以下結(jié)果。



多維數(shù)組和sizeof

這里是使用 sizeof 的上一個循環(huán)運算符計算循環(huán)控制限制:


#include <stdio.h>

int main(void)
{
    int numbers[2][3][4] = {
                               {                       // First block of 3 rows
                                 { 10, 20, 30, 40 },
                                 { 15, 25, 35, 45 },
                                 { 47, 48, 49, 50 }
                               },
                               {                       // Second block of 3 rows
                                 { 10, 20, 30, 40 },
                                 { 15, 25, 35, 45 },
                                 { 47, 48, 49, 50 }
                              }
                           };
    int sum = 0;

    for(int i = 0 ; i < sizeof(numbers)/sizeof(numbers[0]) ; ++i)
    {
      for(int j = 0 ; j < sizeof(numbers[0])/sizeof(numbers[0][0]) ; ++j)
      {
        for(int k = 0 ; k < sizeof(numbers[0][0])/sizeof(numbers[0][0][0])  ; ++k)
        {
          sum += numbers[i][j][k];
        }
      }
    }

    return 0;
}

上面的代碼生成以下結(jié)果。

搜索二維數(shù)組

以下程序演示如何搜索二維數(shù)組。


#include <stdio.h> 
main() 
{ 
   int iTwoD[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 
   int iFoundAt[2] = {0, 0}; 
   int x, y; 

   int iValue = 0; 
   int iFound = 0; 

   printf("\nEnter your search value: "); 
   scanf("%d", &iValue);  

   //search the 2-D array 
   for ( x = 0; x <= 2; x++ ) { 
      for ( y = 0; y <= 2; y++ ) { 
         if ( iTwoD[x][y] == iValue ) { 
            iFound = 1; 
            iFoundAt[0] = x; 
            iFoundAt[1] = y; 
            break; 
         }  //end if 
      }  //end inner loop 
   } //end outer loop 
   if ( iFound == 1 ) 
      printf("\nFound value in iTwoD[%d][%d]\n", iFoundAt[0], iFoundAt[1]); 
   else 
      printf("\nValue not found\n"); 
} //end main 

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號