Two Dimensional(2D) Array

Two dimensional array is an array of array. It consists of the multiple rows and multiple columns. 2D array is represented in matrix form.

 
The Two-dimensional array is initialized as, 

int a[5][5]={{ 10,20,30,40,50},                 

                 {12,22,32,42,52},                                                 

                       {14,24,34,44,54},

                       {16,26,36,46,56},

                       {18,28,38,48,58 }}

We can also write as, 

int a[ ][5]={ { 10,20,30,40,50},

                       {12,22,32,42,52},

                       {14,24,34,44,54},

                       {16,26,36,46,56},

                       {18,28,38,48,58 }}

 But we cannot write as, int a[ ][ ]   because size is not mentioned.

The following fig shows the matrix of an array.

Two-dimensional-array-c
Let us perform some operations::

1.           For displaying only one value in an array

printf(“%d “,a[3][2]);

Output::  36 

printf(“%u “,a);

Output::  1000 //base address

printf(“%u “,a[3]);

Output::   1030 // base address of the third row

printf(“%u “,&a[3][2]);

Output::  1034  // address of the Third row second column

 

2.       For displaying all values in the constant row

for(i=0;i<5;i++)

               {

 printf(“%d  “,a[3][i];

}

Output:: 16 26 36 46 56


3.        For displaying all value in an array

for(i=0;i<5;i++)

{

 printf(“\n”);

  for(j=0;j<5;j++)

 printf(“%d “,a[i][j]);

}

Output::       10 20 30 40 50

                      12 22 32 42 52

                       14 24 34 44 54

                       16 26 36 46 56

                       18 28 38 48 58


Taking Runtime Input

#include<stdio.h>
int main()
{
 int num[3][3],i,j;
 printf(" Enter the values::\n");
 for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
     {
       scanf("%d ",&num[i][j]);
     }
    printf("\n");
  }

// printing the input values
 for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
     {
       printf("%d ",num[i][j]);
     }
   printf("\n");
  }
 return 0;
}

output:
Enter the values::
1 2 3 4 5 6 7 8 9
 
1 2 3 
4 5 6
7 8 9
 


  Refer Also :

1. One-dimensional array

2. Three-dimensional array

3. array in c