Multidimensional Arrays

In the previous tutorials on Arrays, we covered, well, arrays and how they work. The arrays we focused at were all one-dimensional, but C can form and use multi-dimensional arrays. Here is the common form of a multidimensional array declaration:

type name[size1][size2]...[sizeN];   

For example, here’s a necessary one for you to look at –

int foo[1][2][3];   

or maybe this one –

char vowels[1][5] = {
    {'a', 'e', 'i', 'o', 'u'}
};   

Two-dimensional Arrays

The straightforward form of multidimensional array is the two-dimensional array. A two-dimensional array is pretty much a list of one-dimensional arrays. To publish a two-dimensional integer array of size [ x ][ y ], you would write something like this −

type arrayName [x][y];
   

Where type can be any C data type (int, char, long, long long, double, etc.) and arrayName will be a valid C identifier, or variable. A two-dimensional array can be examine as a table which will have [ x ] number of rows and [ y ] number of columns. A two-dimensional array a, which carries three rows and four columns can be present and thought about like this −

int a = 0,b = 1,c = 2,d = 3, e = 4;
a = b - c + d * e;
printf("%d", a); /* will print 1-2+3*4 = 11 */   

In this perception, every component in the array a is recognized by an component name in the form a[i][j], where ‘a’ is the name of the array, and ‘i’ and ‘j’ are the indexes that uniquely recognize, or show, each component in ‘a’.

And honestly, you really don’t have to put in a [ x ] value really, because if you did something like this –

char vowels[][5] = {
    {'A', 'E', 'I', 'O', 'U'},
    {'a', 'e', 'i', 'o', 'u'}
};   

the compiler would already recognize that there are two “dimensions” you could say, but, you need NEED a [ y ] value!! The compiler may be smart, but it will not know how many integers, characters, floats, whatever you’re using you have in the dimensions. Keep that in mind.

Initializing Two-Dimensional Arrays

Multidimensional arrays may be used by identifying bracketes[] values for each row. Below is an array with 3 rows and each row has 4 columns. To make it easier, you can neglect the 3 and keep it blank, it’ll still work.

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};   

The inside braces, which designated the wanted row, are voluntary. The following initialization is the same to the previous example −

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};   

Accessing Two-Dimensional Array Elements

A component in a two-dimensional array is retrieved by using the subscripts, i.e., row index and column index of the array. For example −

int val = a[2][3];   

The above statement will take the 4th element from the 3rd row of the array.

Exercise

Let us try to find out the average marks of a group of five students for two subjects, Mathematics and Physics. To do this, we use a two-dimensional array called grades. The marks relates to Mathematics would be stored in the first row (grades[0]), whereas those relates to Physics would be stored in the second row (grades[1]). Complete the following steps so that you can execute this program.

  • Publish grades as a two-dimensional array of integers
  • Complete the for loops by define their terminating states
  • Compute the average marks achieved in each subject