In c programming language, arrays are classified into two types. They are as follows...
- Single Dimensional Array / One Dimensional Array
- Multi Dimensional Array
In c programming language, single dimensional arrays are used to store list of values of same datatype. In other words, single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as one-dimensional arrays, Linear Arrays or simply 1-D Arrays.
Declaration of Single Dimensional Array
We use the following general syntax for declaring a single dimensional array...
datatype arrayName [ size ] ;
int rollNumbers [40];
Initialization of Single Dimensional Array
We use the following general syntax for declaring and initializing a single dimensional array with size and initial values.
datatype arrayName [ size ] = {value1, value2, ...} ;
int marks [6] = { 89, 90, 76, 78, 98, 86 } ;
The above declaration of single dimensional array reserves 6 contiguous memory locations of 2 bytes each with the name marks and initializes with value 89 in first memory location, 90 in second memory location, 76 in third memory location, 78 in fourth memory location, 98 in fifth memory location and 86 in sixth memory location.
Accessing Elements of Single Dimensional Array
We use the following general syntax to access individual elements of single dimensional array...
arrayName [ indexValue ] = value;
marks [2] = 99 ;
Most popular and commonly used multi dimensional array is two dimensional array. The 2-D arrays are used to store data in the form of table. We also use 2-D arrays to create mathematical matrices.
Declaration of Two Dimensional Array
We use the following general syntax for declaring a two dimensional array...
datatype arrayName [ rowSize ] [ columnSize ] ;
int matrix_A [2][3] ;
The above declaration of two dimensional array reserves 6 continuous memory locations of 2 bytes each in the form of 2 rows and 3 columns.
Initialization of Two Dimensional Array
We use the following general syntax for declaring and initializing a two dimensional array with specific number of rows and coloumns with initial values.
datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ;
int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
Accessing Individual Elements of Two Dimensional Array
We use the following general syntax to access the individual elements of a two-dimensional array...
arrayName [ rowIndex ] [ columnIndex ] = value;
matrix_A [0][1] = 10 ;
In the above statement, the element with row index 0 and column index 1 of matrix_A array is assinged with value 10.
No comments:
Post a Comment