Types of arrays in C Programming

We have already seen about the one dimensional or 1D arrays. Now, let us see the other two types of arrays.

Two dimensional arrays

  • At times we need to store the data in form of tables or matrices. For this, we can use the two dimensional arrays.
  • This array is specified by using two subscripts where one subscript is denoted as the row and the other as the column.
  • It is also viewed as an array of arrays.
two dimentional array

Above figure shows how the rows and columns are addressed in a two- dimensional array. The first subscript denotes the row and the second subscript denotes the column.

i) Declaration of a two-dimensional array
The declaration of the array will tell the compiler as to which array is being used for the program.

Syntax:
data_type array_name [row_size] [column_size] ;

  • The declaration of the rows and columns is compulsory for a two-dimensional array.
  • We cannot replace the row size with the column size and the column size to row size. The proper sequence has to be maintained.
ii) Initialization of two-dimensional array
A two-dimensional array is initialized in the same way as the one-dimensional array.

Example: Initialization of 2D array
int score [3] [2] ={50, 60, 70, 95, 3, 36};

The initialization of the array is done row-by-row.

The above can also be written in the following manner:
int score [3] [2] ={{50, 60} , {70,95} ,{3,36}};

The elements will be seen in the following format once they are initialized.

score[0][0] = 50 score [0][1] = 60
score[1][0] = 70 score [1][1] = 95
score[2][0] = 3 score [2][1] = 36

The above example has been divided into three rows and two columns.

iii) Accessing the elements
  • The elements of this array are stored in a continuous memory location.
  • The last subscript varies rapidly as compared to the first one.
  • Two for loops required for scanning the elements of the two-dimensional array.  The first for will loop for each row and second for will loop for each column for every row.

Example: Simple program for printing the elements of 2D array.

#include <stdio.h>
void main()
{
     int score[3][2]= {10,20,30,40,50,60};
     int i,j;
     for(i=0;i<3;i++)
     {
           printf("\n");
           for(j=0;j<2;j++)
               printf("%d\t",score[i][j]);
     }
}


Output:
10      20      
30      40      
50      60

iv) Passing 2D array as parameters to functions
There are three ways of passing the parameters to the functions.

a) Passing individual elements
They are done in the same manner as that of the one-dimensional array.

b) Passing a row
  • A row can be passed by indexing the array name with the number of the row.
  • When a single row is sent to the called function, it is received as a one-dimensional array.

Example: Passing a row

void main()  // Calling function
{
      int score [2][3] = {{10,20,30} , {40, 50, 60}};
      func (score [10]);
}
void func (int score[ ])  // Called function
{
      int i;
      for (i=0;i<5;i++)
           printf ("%d", score [i] * 10);
}


c) Passing the entire 2D array
We use the array name as the actual parameter for passing a 2D array to a function.
But the parameter in the called function should denote that the array has two dimensions.

Multidimensional array

  • In simple terms it is called an array of arrays.
  • We have 'n' number of indexes in this array.
  • It is specified by using 'n' number of indices.
  • The requirement of the memory increases with the number of indices that it uses.
  • But, if we talk practically we would not use more than three indices.
  • These arrays are declared and initialized in the same manner as that of one and  two-dimensional arrays.
multi dimentional array

Example: Write a program to read and display a 2*2*2 array.

#include <stdio.h>
void main()
{
     int arr[3][3][3],i,j,k;
     printf("\n Enter the elements for the array:");
     for(i=0;i<2;i++)
     {
          for(j=0;j<2;j++)
          {
               for(k=0;k<2;k++)
               {
                    printf("\n array [%d][%d][%d] = ",i,j,k);
                    scanf("%d",&arr[i][j][k]);
               }
          }
     }
     printf("\n The matrix is:");
     for(i=0;i<2;i++)
     {
          printf("\n\n");
          for(j=0;j<2;j++)
          {
               printf("\n");
               for(k=0;k<2;k++)
                     printf("\t array[%d][%d][%d]=%d",i,j,k, arr[i][j][k]);
          }
     }
}


Output:
Enter the elements for the array: 10,20,30,40,50,60,70,80
array [0][0][0] = 10
array [0][0][1] = 20
array [0][1][0] = 30
array [0][1][1] = 40
array [1][0][0] = 50
array [1][0][1] = 60
array [1][1][0] = 70
array [1][1][1] = 80

The matrix is:

array[0][0][0]=10       array[0][0][1]=20
array[0][1][0]=30       array[0][1][1]=40
array[1][0][0]=50       array[1][0][1]=60
array[1][1][0]=70       array[1][1][1]=80