Two dimensional array


Arrays with two sets of square brackets [][] are called 2- dimensional arrays. A two dimensional array is used when data items are arranged in row-wise and column wise in a tabular form.The array has two sets of square brackets [3][4] and hence it is a 2 dimensional array with 3 rows and 4 columns.This declaration informs the compiler to reserve 12 locations(3*4=12) contiguously one after the other.

for-loop-shineskill

The pictorial representation of array.


for-loop-shineskill

Initialization of two dimensional array.

Assigning required value to a variable before processing is initialization. As we initialize a variable to the required value we can initialize the individual elements of a 2 dimensional array during initialization.
syntax:-
Data type array_name[row][cols]={
                                      { a1, a2, a3…..an},
                                      { b1, b2, b3….bn}
                                                           };
Example:-           int a[2][3]= {
                             {33, 55, 66},
                             {23, 34, 12}
                                 };
#include<stdio.h>
#include<conio.h>
void main()
  {
   int arr[3][3], I, j;
   printf(“Enter element of array\n”);
   for(i=0;i<=2;i++)
  {
   for(j=0;j<=2;j++)
  {
   scanf(“%d”,&arr[i][j]);
  }
  }
   printf(“Enter Elements are:-\n”);
   for(i=0;i<=2;i++
  {
   for(j=0;j<=2;j++)
  {
   printf(“%d”,arr[i][j]);
  }
   printf(“\n”);
  }
  }

Output

Enter elements of array.
6 4 9
5 2 5
4 9 6