Computing

Arrays Multi-Dimensional

We have seen the basic concept behind arrays in general and also one-dimensional arrays. Now we see the handling of more than one-dimensional arrays.

Two-Dimensional Arrays

The discussion so far has been in listing of values stored under a single name. However, sometimes it becomes to store a table of values. Now we all know that a table comprises of rows and columns. Consider the following data table

 ----------------------------------------

                 x1         x2

----------------------------------------

y1            100       200

y2             300      400

 ---------------------------------------

The above table consists of  2 rows and 2 columns. Now it is necessary to store this kind of data in programs, where the column represents variation of one quantity and corresponding to each column we have the row values varying  as above we have x1 and varying of y from y1 to y2.

C allows us to define such table of items by using two-dimensional arrays. The table discussed above can be defined in C as

array[2][2]

Here the first bracket value indicates the number of rows which is 2 in this case, and the second bracket is the column size (2 in this case).

 In general the declaration of two-dimensional arrays is as follows

 type array_name [row_size][column_size];

The type can be any data type inbuilt or user defined. C & C++ provides the easiest techniques to to declare arrays.

The representation of memory storage of two-dimensional arrays can be visualized as follows

                             column0             column1

                                [0][0]                [0][1]

                          ______________________

Row0----             |___100_____|____200 ___|    

                           

                               [1][0]            [1][1]

                        ______________________

Row1----           |____300____ |____400___|           

 

Initialization of two-dimensional arrays

Similar to the one-dimensional the two-dimensional arrays are initialized. For example

int array[2][3] = {1,2,3,4,5,6};

In the above case elements of the first row are initialized to 1,2,3 & second row elements are initialized to 4,5,6.

The initialization can be done row wise also, for the above example it is

int array[2][3] = {{1,2,3},{4,5,6}};

If the initialization has some missing values then they are automatically initialized to 0.

For example

int array[2][3] = {{3.4},{5}}

In this case the first two elements of the first  row are initialized to 3,4 ,while the first element of the second row is initialized to 5 & rest all elements are initialized to 0.

 Multi-Dimensional Arrays

 C allows arrays with more than two dimension, to the limit depending on the compiler.

 The general form of a multidimensional array is

 type array_name[s1][s2][s3]....[sm];

The total number of elements in any dimension of arrays is the product of all sizes included in the declaration. So it will be s1*s2*s3*....*sm.

For most applications, two dimension arrays suffice. Three dimension is maximum that you can expect to go in normal applications. Still higher dimension arrays are required in the field of scientific computing, weather forecasting, time-space analysis, etc.   

19-Mar-2001

More by :  Sachin Mehta

Top | Computing

Views: 3396      Comments: 0





Name *

Email ID

Comment *
 
 Characters
Verification Code*

Can't read? Reload

Please fill the above code for verification.