Multi Dimensional Arrays

Definition

The multidimensional array will have multiple indexes, like a 2-Dimensional array will have 2 indexes and a 3-Dimensional array will have 3 indexes. A 2-D array is used when we want to take data in the form of rows and columns. In the 2-D array, the 1st index shows the number of rows and the 2nd shows the number of columns. So total number of values we can store in 2-D array = <1st index> * <2nd index>

Initialization

The syntax of initializing a 2-D, 3-D array is as shown below.

<data type>   <array name>[<index 1/ row>][<index 2/column>]; //2-D array
<data type>   <array name>[<index 1>][<index 2>][<index 3>]; //3-D array

Ex1. int arr[2][3];

This shows that arr is a 2-D array having 2 rows (index starting from 0 to 1) and 3 columns(index starting from 0 to 2).

Ex2. Int arr[2][4][7];

This shows that arr is a 3-D array with 3 indexes. The 1st index is a string from 0 to 1, the 2nd index is starting from 0 to 3, and the 3rd index is starting from 0 to 6.

Size :

Size of 2-D array : <size of data type> * <index 1> * <index 2>

Size of 3-D array : <size of data type> * <index 1> * <index 2> * <index 3>

Ex1.  int arr[2][4];    // 2 row, 4 column

Size of above arr array is = size of int * index1 * index2   = 2*2*4 = 16 bits

Fig. 1: Structure of Multidimensional Array

Ex2.  int arr[2][4][5];   

Size of above arr array is = size of int * index1 * index2 * index3  = 2*2*4*5 = 80 bits

Methods to give value to an array

There are two methods to give value to 2-D or any multidimensional arrays. Which are as listed below. Which is the same as 1-D array methods.

  • Method to give value at compile time 

In this method, we give values to the array at the time of declaration. For this, we need an outer curly bracket and inner curly brackets. Each inner curly bracket is used to give value to each element of an array. In the below example indexes 2 and 4 show that ‘a’ can store 2 groups of int values and each group has 4 int members. Those 4 members of each group are separated by other curly brackets.

Ex. int a[2][4]={ {10,20,30,40} , {50,60,70,80} } ;

     // which means   :        a[0][0] = 10  , a[0][1] = 20 , a[0][2] = 30 , a[0][3] = 40 ,

                   a[1][0] = 50  , a[1][1] = 60 , a[1][2] = 70 , a[1][3] = 80 
  • Method to give value at run time

In this method, the user has to give array elements values, whatever he/she wants at run time. In the below example, ‘a’ is having indexes 2 and 4. So 1st for loop represents 1st index and 2nd for loop represents 2nd index. At the start of the 1st loop, i is 0. And then it enters the 2nd for loop. So the 2nd for loop takes the value of a[0][0] to a[0][3]. After that again 1st for loop is starting from i with value 1. So now the 2nd for loop takes the value of a[1][0] to a[1][3] and so on.

Ex. int a[2][4];

      for(int i=0;i<2;i++)

for(int j=0;j<4;j++)

     scanf(“%d”,&a[i][j]);

The complete example of taking 2X3 matrix values and display them

#include<stdio.h>
#include<conio.h>
void main()
{
int mat[2][3],i,j;
clrscr();

for( i=0 ; i<2 ; i++ ) // this for loop takes value of array elements
{	
for( j=0 ; j<3 ; j++ )
    {
printf(“enter value of mat[%d][%d] : ”,i,j);
scanf(“%d”,&mat[i][j]);
}
}

printf(“ displaying whole matrix \n“);
for( i=0 ; i<2 ; i++ ) // this for loop prints value of array elements
{	for( j=0 ; j<3 ; j++ )
    {
printf(“%d  ”,mat[i][j]);
}
printf(“\n”);
}
getch();
}

Output:

 

 

 

 

 

In this example code “Method to give value at run time” is used to get values of the array from a user. mat is the array of indexes 2 and 3 so it can store total 2*3 = 6 values. The 1st for loop is used to take the value of an array from the user and the 2nd for loop is used to print the value of the array.

Things to be noted while passing an array to function:

When we are passing an array to function argument then in the function definition, we can write a 2-D array with 1st index blank, but 2nd index is necessary to write.

Ex. func_def(arr[][3])  // here arr array have total 3 column , but row can be of any numbers

     {

.  .  .

     }

In below example,arr is array and we are passing array to function func_def. And we are not passing 1st argument while calling. At output it is printing values which we gave.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,arr[2][3]={{1,2,3},{4,5,6}};
clrscr();
func_def(arr[][3]);
getch();
}
void func_def(int arr[2][3])
{	int i,j;
    for(i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
        {
        printf("arr[%d][%d] = %d  ",i,j,arr[i][j]);
        }
        printf("\n");
    }
}

Output:

 

 

 

Learning from above blog:
1) Syntax of 2-D array
2) Methods to give / take values of 2-D array
3) Index in 2-D array
4) Program of how to get the value of array and display it
5) Usage of 2-D array 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments