Multidimensional Arrays Ajay
Multidimensional Arrays Ajay
Arrays
1
• Multidimensional Arrays are array of arrays. They can be two
dimensional, three dimensional and even more.
5
• First Method:
• int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
• The above array have 3 rows and 4 columns. The elements in
the braces from left to right are stored in the table also from
left to right. The elements will be filled in the array in the
order, first 4 elements from the left in first row, next 4
elements in second row and so on.
• Second Method:
• int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
• This type of initialization make use of nested braces. Each set
of inner braces represents one row. In the above example
there are total three rows so there are three sets of inner 6
braces.
• Accessing Elements of Two-Dimensional Arrays: Elements in
Two-Dimensional arrays are accessed using the row indexes
and column indexes.
Example:
int x[2][1];
The above example represents the element present in third row
and second column.
7
• Three Dimensional Arrays
8
• Initializing Three-Dimensional Array:
• Initialization in Three-Dimensional array is same as that of Two-
dimensional arrays. The difference is as the number of
dimension increases so the number of nested braces will also
increase.
• First Method:
• int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23};
• Second Method:
• int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }, {
{12,13,14,15}, {16,17,18,19}, {20,21,22,23} } };
9
Program to store and print values in array
#include<stdio.h>
int main(){
int array[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++){
printf("Enter value for array[%d][%d]:", i, j);
scanf("%d",array[i][j]);
}
}
10
printf("array elements:\n");
for(i=0; i<2; i++){
for(j=0;j<3;j++) {
printf("%d ", array[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
11
Output:
Enter value for array[0][0]:1
Enter value for array[0][1]:2
Enter value for array[0][2]:3
Enter value for array[1][0]:4
Enter value for array[1][1]:5
Enter value for array[1][2]:6
array elements:
123
456
12