Previous | Home | Next |
Multidimensional arrays are often known as array of the arrays. In multidimensional arrays the array is divided into rows and columns, mainly while considering multidimensional arrays we will be discussing mainly about two dimensional arrays and a bit about three dimensional arrays. In 2-D array we can declare an array as :
Syntax
data_type array_name[][][];
Example
int arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
where first index value shows the number of the rows and second index value shows the no. of the columns in the array. To access the various elements in 2-D we can access it like this:
#include<stdio.h> #include<conio.h> void main() { int i, j; int num[3][3]={ {20, 48, 65}, {89, 34, 70}, {90, 75, 49} }; clrscr(); printf(" Elements of 2D array\n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf ("%d\t", num[i][j]); } printf("\n"); } getch(); }
Elements of 2D array
20 48 65
89 34 70
90 75 49
# Three dimensional we can declare the array in the following manner :
int arr[3][3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 }; /* here we have divided array into grid for sake of convenience as in above declaration we have created 3 different grids, each have rows and columns */
If we want to access the element the in 3-D array we can do it as follows :
Its output will be 26, as a[2][2][2] means first value in [] corresponds to the grid no. i.e. 3 and the second value in [] means third row in the corresponding grid and last [] means third column
Previous | Home | Next |