Arrays in C

     A normal variable holds only one value. There are many applications which requires to process a group of data items that are of same type. Such as int, float, char,... etc

    An array is a collection of values of similar data type is collectively accessed using one name.

Eg: List of marks obtained in an examination by a group of students. In such situations, it is necessary to have an unique variable which  holds a group of elements.

Defn: An array can be defined as, an ordered list of homogeneous elements. 

  1. It is collection of values.
  2. All values of same data types.
  3. All values are accessed using same name.
Classification of Arrays :     Generally, arrays are classified into two types. 

  1. One (single)-Dimensional Array
  2. Multi-Dimensional Array
    1. Two-dimensional Array
    2. Three-dimensional Array    
Single-dimensional Array : It is a linear list of fixed number of data types (items) of the same type. All these data items are accessed using the same name and a single subscript. It is similar to a row or a column matrix. It is also called as one-dimensional array of a one subscripted value.

Syntax:  datatype array_name [size];

    Here datatype is a basic datatypes such as int, float, char....., array_name is a variable/array name and size is number of elements specified within pair of square brackets.

Eg:  1. int list[10];

            It creates an array named 'list' of '10' 'integer' constants.

        2. float xyz[25];

            Floating point declaration creates an array named as 'xyz' of '25' numbers.

Initialization of one-dimensional array: Initializing is nothing but, assigning some value to the variable  that undergoes processing.

Syntax:  datatype array_name [size] = {ele1, ele2, ele3,.......... elen};

              datatype is a basic datatype, array_name is name of the array, size is no. of elements and ele1. ele2, ele3,.... elen are initial values enclosed within a pair of curly braces and they must be seperated by a comma.

Eg: 1. int evnum [4] = {2, 4, 6, 8};

          evnum [0] = 2,

          evnum [1] = 4,

          evnum [2] = 6,

         evnum [3] = 8,

Pictorial as follows ;    

2

4

6

8

                                               evnum[0]   evnum[1]  evnum[2]  evnum[3]


Multi-dimensional Array : If the number of subscripts is more than one, then such an array are called Multi-Dimensional Array. Thus, we can say two-dimensional array (2-D), three-dimensional array (3-D), and so on. Thus the dimensionality is understood from number of pairs of square brackets ahead of array name.  

Eg: 1. Array[ ] ------- One-dimensional array (1-D)

       2. Array[ ] [ ] ------- Two-dimensional array (2-D)

       3. Array[ ] [ ] [ ] ------- Three-dimensional array (3-D)


Two-dimensional Array:  It is an ordered table of homogeneous elements. It is generally referred to as matrix of some rows and columns. It is also  called as a two subscripted variable.

Syntax:   datatype array_name [rows] [columns];

            Where, datatype is a basic datatype. Array_name is name of the array, rows is  no. of elements to be processed under the subscript one. column is  no. of elements to be processed under the subscript two.

Eg: 1. int marks[5][3];  

            marks in 2-D array of 5 rows and 3 columns of integer type of data.

       2. float matrix[3][3]; 

            matrix in 2-D array of 3 rows and 3 columns of float type of data.


Initialization 2-D array:

    datatype array_name[size1][size2]={e1, e2, e3, ...................... , en};

    datatype --- basic data type, array_name --- name of the array, [size1]--- no. of rows, [size2] --- no. of columns, e1,e2,... en ---- are the initial values to be assigned to 'n elements' of an array.

Eg: int matrix[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

      matrix[0][0] = 1;  matrix[0][1] = 2;  matrix[0][2] = 3;  matrix[1][0] = 4;  matrix[1][1] = 5;                        matrix[1][2] = 6;  matrix[2][0] = 7;  matrix[2][1] = 8;  matrix[2][2] = 9;

Pictorial as follows ;    

1

2

3

4

5

6

7

8

9


or

 

Col 0

Col 1

Col 2

Row 0

1

2

3

Row 1

4

5

6

Row 2

7

8

9


Rules for governing subscript :

Some of the rules that govern the subscript are as follows;

  • Each subscript must be unsigned positive integer constant or an expression.
  • subscript of subscript is not allowed.
  • In C, the subscript values arranges from zero (0) to one less than the maximum size.
  • If the size of an array is 10, then the first subscript is zero(0), the second element is one(1)m and so on. The last subscript is at Ninth(9) place. In general  the 9th element has the subscrip (i-1).