Double Dimensional Array in C programming with examples

 

1.Double dimensional array is called array of array.
2.It is organized as matrix that can be represented by the collection of rows and columns.
3.It is a collection of data of same data type.
4.It is used to store group of data simultaneously.
5.It can store data of same data type means an integer array can store only integer value ,character array can store only character value and so on.
6.We can not fetch data from array directly therefore we use index point.
7.The indexing of array always start with 0.
8.Index value is always an integer number.
9.Array may be of any data type like int, char, float etc.

Syntax


1.Here a is the name of array.
2.int is the data type of array.
3.Size of array is 3x3 means we can store maximum 9 values in this array.

Initialization of array method 1

int a[3][3]={ {40,50,60},{10,20,30},{70,80,90} };


Initialization of array method 2

int a[5];
a[0][0]=40;
a[0][1]=50;
a[0][2]=60;
a[1][0]=10;
a[1][1]=20;
a[1][2]=30;
a[2][0]=70;
a[2][1]=80;
a[2][2]=90;

Printing of array element method 1

#include<stdio.h>
int main()
{
int a[3][3]={{40,50,60},{10,20,30},{70,80,90}};//Initializing array
printf("value at a[0][0]=%d\n",a[0][0]);
printf("value at a[0][1]=%d\n",a[0][1]);
printf("value at a[0][2]=%d\n",a[0][2]);
printf("value at a[1][0]=%d\n",a[1][0]);
printf("value at a[1][1]=%d\n",a[1][1]);
printf("value at a[1][2]=%d\n",a[1][2]);
printf("value at a[2][0]=%d\n",a[2][0]);
printf("value at a[2][1]=%d\n",a[2][1]);
printf("value at a[2][2]=%d\n",a[2][2]);
}
/*
### Output ###
value at a[0][0]=40
value at a[0][1]=50
value at a[0][2]=60
value at a[1][0]=10
value at a[1][1]=20
value at a[1][2]=30
value at a[2][0]=70
value at a[2][1]=80
value at a[2][2]=90
*/

Here in the above program if the array elements increases then number of printf statement will also increase. Here we are using only 9 array elements so you will find that it is very simple to print all elements of array but this method of printing array elements is very time consuming and  is for beginners, if you want to print all array elements using very few line then go for second method which is below. 

Printing of array element method 2

Here i am going to use for loop to print all the elements of  array and this is the best method to access all the elements of array. Here in this case if the array elements increases then only we have to increase loop count and remain code will be same.

#include<stdio.h>
int main()
{
int a[3][3]={{40,50,60},{10,20,30},{70,80,90}};//Initializing array
for(int i=0;i<=2;i++)
{
  for(int j=0;j<=2;j++)
  {
   printf("%d ",a[i][j]);
  }
   printf("\n");
}
}
/*
### Output ###
40  50  60
10  20  30
70  80  90
*/

User input using loop

#include<stdio.h>
int main()
{
int a[3][3];//Declaring array
int i,j;
//Nested loop for user input
for(i=0;i<=2;i++)
{
  for(j=0;j<=2;j++)
  {
  printf("Enter element at a[%d][%d]=",i,j);
  scanf("%d",&a[i][j]);
  }
}
//Nested loop for printing output
for(i=0;i<=2;i++)
{
  for(j=0;j<=2;j++)
  {
   printf("%d ",a[i][j]);
  }
   printf("\n");
}
}
/*
### Output ###
Enter value at a[0][0]=4
Enter value at a[0][1]=5
Enter value at a[0][2]=6
Enter value at a[1][0]=1
Enter value at a[1][1]=2
Enter value at a[1][2]=3
Enter value at a[2][0]=7
Enter value at a[2][1]=8
Enter value at a[2][2]=9
4  5  6
1  2  3
7  8  9
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments