You are on page 1of 2

2 D Arrays (Double Dimensional Array)

-------------------------------------

Double Dimensional
------------------
To store more than one value of similar type in a single variable using different
rows and columns

int n[3][3]; Total number of elements 3x3=9


int n[3][4]; Total number of elements 3x4=12
int n[2][3]; Total number of elements 2x3=6

int n[3][3]={{1,2,3},{4,5,6},{7,8,9}};

1 2 3
[0,0] [0,1] [0,2]

4 5 6
[1,0] [1,1] [1,2]

7 8 9
[2,0] [2,1] [2,2]

printf("\n%d",n[2][3]); error

printf("\n%d",n[2][1]); 8

printf("\n%d",n[1][2]); 6
--------------------------------------
int n[3][2]={{1,2},{3,4},{5,6}};

1 2
[0,0] [0,1]

3 4
[1,0] [1,1]

5 6
[2,0] [2,1]

printf("\n%d",n[0][0]); 1

printf("\n%d",n[2][1]); 6

printf("\n%d",n[1][0]); 3

int n[3][3]={1,2,3,4,5,6,7,8,9};
int n[3][3]={1,2,3,4,5,6};
--------------------------------------

1 2 3
4 5 6
7 8 9

printf("\n%d",n[0][1]+n[1][2]); 8
printf("\n%d",n[1][2]-n[2][2]); -3
printf("\n%d",n[0][0]*n[2][1]); 8
printf("\n%d",n[2][1]/n[1][0]); 2
------------------------------------------
void main()
{
int n[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
for(i=0;i<3;i++) loop to trace row
{
for(j=0;j<3;j++) loop to trace column
{
printf("%d\t",n[i][j]);
}
printf("\n");
}
}

i=0
j=0 to 2

n[0][0] n[0][1] n[0][2]


1 2 3

i=1
j=0 to 2

n[1][0] n[1][1] n[1][2]


4 5 6

i=2
j=0 to 2

n[2][0] n[2][1] n[2][2]


7 8 9
----------------------------------------------
void main()
{
int n[3][3]={{11,2,43},{14,55,61},{27,68,79}};
int i,j;
for(i=0;i<3;i++) loop to trace row
{
for(j=0;j<3;j++) loop to trace column
{
printf("%d\t",n[i][j]);
}
printf("\n");
}
}

You might also like