Learn C - C multidimensional array






A two-dimensional array can be declared as follows:

float value[25][50];

This declares the value array with 25 sets of 50 floating-point elements.

A three-dimensional array is declared as follows.

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

This declares an array with 800 elements.

Initializing Multidimensional Arrays

To initialize a two-dimensional array, put the initial values for each row between braces, {}, and then enclose all the rows between braces:

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
                    };

You can initialize the whole array to 0 by supplying just one value:

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

A three-dimensional array will have three levels of nested braces, with the inner level containing sets of initializing values for a row:

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 }
                          }
                       };

You need a nested loop to process all the elements in a multidimensional array.

The level of nesting will be the number of array dimensions.

Here's how you could sum the elements in the previous numbers array:


#include <stdio.h>
/*from   ww  w  .j a v a2s . c  o m*/
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;
}

The code above generates the following result.





Example

We can create multidimensional array, for instance two dimensional, we can use [][].


  #include <stdio.h> 
// w  w w.  j  a v a2 s  .  c o  m
  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; 
  } 

The code above generates the following result.





multidimensional array and sizeof

Here's the previous loop using the sizeof operator to compute the loop control limits:


#include <stdio.h>
//from  w  w w.  j a  v a  2  s .c  om
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;
}

The code above generates the following result.

Searching Two-Dimensional Arrays

The following program demonstrates how to search a two-dimensional array.


//w  w  w.j a v  a 2  s. c o  m
#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 

The code above generates the following result.