Two dimensional array in C | Initialization of Two dimensional array in C

Preview
You must first complete Introduction to Array in C | One dimensional array in C before viewing this Lesson

Two dimensional array in C

Array having two dimensions is known as two dimensional array. We use two subscripts to specify the size of a two dimensional array or to refer to any element of the two dimensional array.

A two dimensional array is also known as a matrix and elements of two dimensional array are represented in the form of rows and columns.

Before using a two dimensional array in a program, array must be declared.

The syntax for declaring a two dimensional array is:

<Data_Type> Arr_name[R][C];

Here, Data_Type refers to any valid data type of C language. It can be any primary data type like int, float, char or any secondary data type like structure and union.

Arr_name  specifies the name of array as defined by the programmer. The name of array must follow all the rules of a valid identifier of C language.

[ ] Square brackets are known as subscript, they are used to declare size of array within them.

R specifies the maximum number of rows in the two dimensional array. It should be an integer value.

C specifies the maximum number of columns in the two dimensional array. It should also be an integer value.

For Example, we can declare a two dimensional array as:

int Arr[3][3];

Two dimensional array Arr can be represented in memory as follows:





Two dimensional array Arr  has been declared as Arr[3][3] which specifies that this array can   contain 9 elements stored in the form of rows and columns referred as Arr[0][0], Arr[0][1], Arr[0][2], Arr[1][0], Arr[1][1], Arr[1][2], Arr[2][03], Arr[2][1] and Arr[2][2]. Here 0,1,2 are known as indexes of array elements. Index of very first element of two dimensional array is [0][0] i.e. Row 0 and column 0, Next element is referred as [0][1] i.e. row 0 and column 1 and so on.

Elements  of a two dimensional array are also stored in consecutive memory locations.

So if first element of array i.e. A[0][0] is stored at memory location 1002, next array element Arr[0][1] should be stored at location 1004, Arr[0][2] should be stored at location 1006, Arr[1][0] should be stored at location 1006 and so on.

There is a gap of 2 between two consecutive addresses because Size of int data type is 2.

In case of float type array, this gap will be of 4 as float data type is of 4 bytes.

In case of char data type, this gap would be of 1 byte as char data type is of 1 byte.


Program to demonstrate the use of two dimensional array.
#include<stdio.h>
int main()
{
int a[2][2];
a[0][0]=10;
a[0][1]=20;
a[1][0]=30;
a[1][1]=40;
printf(“\na[0][0]=%d”,a[0][0]);
printf(“\na[0][1]=%d”,a[0][1]);
printf(“\na[1][0]=%d”,a[1][0]);
printf(“\na[1][1]=%d”,a[1][1]);
return(0);
}
Output
a[0][0]=10
a[0][1]=20
a[1][0]=30
a[1][1]=40
Description

In the above program, we have declared a two dimensional array a having number of rows 2 and number of columns 2. This array can contain four values.

First element of array is named as a[0][0].

Second value is named as a[0][1].

Third value is named as a[1][0].

Fourth value is named as a[1][1].



Initialization of a Two dimensional array in C

We can initialize a two dimensional array just as we declare and initialize a one dimensional array. Syntax for initializing a two dimensional array is:

<Data_Type> Arr_name[R][C]={List_of_values};

Data_Type refers to any valid data type of C language. It can be any primary data type like int, float, char etc or can be any secondary data type like structure or union.

Arr_name  specifies the name of array as defined by the programmer. The name of array must follow all the rules of a valid identifier of C language.

[ ] Square brackets are known as subscript, they are used to declare number of rows and columns within them. While initializing an array, first subscript can be left empty but second subscript must be provided some value specifying the number of columns in the array.

R specifies the maximum number of rows in a two dimensional array. It is optional while initializing a two dimensional array.

C specifies the maximum number of columns in a two dimensional array. It must be specified while initializing a two dimensional array.

List_of_values specifies the values which are stored in a two dimensional array. These values should be separated from each other with the help of commas. There are various ways to initialize a two dimensional array. They are as follows:

int a[2][2]={3,4,5,6};

int a[3][2]={3,4,

                 5,6,

                 8,9

                 };

int b[3][3]={{6,9,8},

                 {2,6,8},

                 {4,9,3}

                 };


Program to initialize a two dimensional array.
#include<stdio.h>
int main()
{
int a[2][2]={3,4,5,6};
printf(“\na[0][0]=%d”,a[0][0]);
printf(“\na[0][1]=%d”,a[0][1]);
printf(“\na[1][0]=%d”,a[1][0]);
printf(“\na[1][1]=%d”,a[1][1]);
return(0);
}
Output
a[0][0]=3
a[0][1]=4
a[1][0]=5
a[1][1]=6
Description

In the above program, we have initialized a two dimensional array a having number of rows 2 and number of columns 2.

This array contains four values.

First element of array a[0][0] contains 3 , a[0][1] contains 5,  a[1][0] contains 5 and a[1][1] contains 6




Write C program for following:

  1. Program to print the sum of diagonal elements of a two dimensional array.
  2. Program to find the smallest number from a two dimensional array.
  3. Program to find the sum of all elements of a two dimensional array.

Write the output of following code:

1.

main( )
{
char a[3][3]={3,4,5,
6,3,1,
9,8,2};
int i,j;
for(i=0;i<3;i++)
   {

   for(j=0;j<3;j++)
   {
      if(a[i][j]%2==0)
      printf(“\n%d”a[i][j]);
   }
}

}

Best Books of C





Lesson tags: initialization of two dimensional array in c, matrix in c, reading two dimensional array in c, two dimensional array in c
Back to: C Programming Language