Two – dimensional array

A two - dimensional array of one – dimensional arrays. A single dimensional array can store a list of values, whereas a two – dimensional array can store a table of values. The general format for declaration of two – dimensional array is:

data – type array – name [row – size] [column – size];

We have already seen that an n – element, one – dimensional array can be thought of a list of values, as illustrated in figure similarly, an mXn, two – dimensional array can be thought of as a table of values having m rows an n column, as illustrated in figure. Extending this idea, a three – dimensional array can be visualized as a set of tables (e.g., a book in which each page is a table), and so on.


img

In 2 – D array also, element declaration (both in row and column) is done with zero origin subscripting. Thus an array int a [3] [3] will have elements as:

a [0][0] a [0][1] a [0] [2]
a [1][0] a [1][1] a [1] [2]
a [2][0] a [2][1] a [2] [2]

A two – dimensional array can be initialized with a list of values

int a [3][3] = {9,9,9,3,3,3,1,1,1};

Figure shows the pictorial representation.


img

Here the first row elements are initialized to 9, second row to third row to 1.

The initialization is done row by row. This can also be written as
int a [3] [3] = {{9,9,9}, {3,3,3}, {1,1,1}};

In the syntax, the commas are required after each brace that closes off a row except for the last row.


Multi – dimensional Array

C allows multidimensional array. The exact limit is determined by the compiler. The general form of a multidimensional array is

data – type array – name [s1] [s2] [s3] . . . [sn] where s [i] is the size of the i – th dimension.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext