Multidimensional Arrays Arrays with more than one dimension are called multidimensional arrays. Human cannot easily visualize more than three dimension but representing multidimensional arrays presents no problem to computers. Declaration An array of two dimension can be declared as data_type array_name[size1][size2]; Where size1 and size2 are the sizes of the array’s first and second dimensions respectively.
Multidimensional Arrays Ex : The chessboard can be represented as a two-dimensional array of 64 elements like int chs[8][8]; Here the indices of each side of the chessboard array run from 0 to 7. Declaration of three dimensional Array A three dimensional array such a s a cube can be represented as data_type array_name [size1][size2][size3]; Three-dimensional arrays and higher, are stored as a linear sequence of variables and the last index is the the one that varies the fastest.
Multidimensional Arrays Initialization The number of subscripts determines the dimensionality of an array. For example x[i][j] refers to an element of a two-dimensional array. The initialization of a multidimensional array is done in the same way as a single-dimensional array. Ex : int y[3][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}}; or int y[3][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; The compiler assigns values beginning with the [0][0] element and proceeds row by row to fill in the remaining values.
Multidimensional Arrays Rules When initializing the arrays, the outermost pair of braces cannot be omitted. But if the initializer list includes all the initializers for the object being initialized, the inner braces can be omitted. Ex : int x[4][2] = {{1,2},{3,4},{5,6}}; Here the initialization ends before the fourth row is initialized, so the members of the fourth row default to zero or garbage depending on the compiler.
Multidimensional Arrays Unsized Array initialization If unsized arrays are declared, the c compiler automatically creates an array big enough to hold all the initializers. This called an unsized array. Ex : char str[]=“how are you?”; int x[][2]={1,2,3,4,5,6,7,8}; Accessing multidimensional Arrays The elements of a multidimensional array are stored contiguously in a block of computer memory. The last subscript of the array varies most rapidly where as the first varies least rapidly. That is for a two-dimensional array x[2][2] the elements are stored in the order x[0][0], x[0][1], x[1][0], x[1][1].
Multidimensional Arrays Ex : int i,j; int x[3][2] = {{1,2},{3,4},{5,6}}; for(i=0;i<=2;i++) { for(j=0;j<=1;j++) printf(“a[%d][%d]=%d”,i,j,a[i][j]); printf(“\n”); } Thus a multidimensional array can not be stored as a grid but the array is stored in rows.
Arrays of Strings A two-dimensional array of strings can be declared as data_type array_name [row_size][column_size]; Ex : char s[5][30]; Initialization A two-dimensional string array can be initialized as char s[5][30] = {“apple”,”banana”,”papaya”,”pine apple”,”orange”}; or char s[][] = {“apple”,”banana”,”papaya”,”pine apple”,”orange”}; where s[0]=“apple” s[2]=“papaya” and so on.