Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Friday, 21 September 2018

Initialization of array and its types with examples

Initialization of array and its types


The process of assigning the values to elements of an array is called an initialization of the array. It can be done by a various method which is explained briefly below with examples.

Element by element

In this case, each and every element of an array is assigned with a value individually.

E.g.  int a[3];
         a[0]=45;
         a[1]=7;
         a[2]=-4;

Declaration and initialization at the same time


The values are assigned to the array at the time of declaration. The values are assigned collectively within a single statement. The values are assigned by keeping them within curly braces{ } and are separated by commas (,).

E.g.

  • int a[3]={45,7,-4};

          which gives: 

          a[0]=45          a[1]=7        a[2]=-4




  • int a[3]={5};     

          which gives a[0]=5 and all the other elements will return a garbage value.

 
Initialization from the user


In this process, we assign different values to the element as per the choice of the user. A for loop is applied to individually call the elements of the array. The values are assigned as per the user and stored in respective subscripts. Then again a for loop is used and elements are displayed individually on the output screen.

Also, see Input and output of a one-dimensional array








Introduction to matrix and types of array

Matrix and array


Matrix

Matrix is a rectangular array of numbers arranged in rows (horizontal lines) and columns (vertical lines) enclosed between a pair of round or square brackets. E.g.


Example of matrices
EXAMPLES OF MATRIX











Order of matrix

Order or size of a matrix is given by the number of rows followed by the number of columns. In the example the first matrix has one row and two columns, so the order is 1*2 (read as 1 by 2). Similarly, the order of other matrices is 2*2, 3*3, 3*1.


Array

An array is a homogeneous (same type) collection of elements or data having a common property and sharing a common name. It can be declared normally like ordinary variables using [ ] for size specification.

For e.g. int a[5] is used to store 5 integer values in a[0],a[1],a[2],a[3],a[4].

a[0] is first element of array a. a[0],a[1],a[2],a[3],a[4] are called elements or subscripts.

All 5 variables are stored in different memory locations.



Types of array 


Single dimensional array
                                           The array having only a single pair of brackets with array name is called single dimensional array. They have single size specification.E.g. a[5].



Multidimensional array
                                         The array having more than one pair of brackets with the array name is called a multidimensional array. They have multiple size specification.
E.g. ex[5][5]g[2][4][2].

           
two-dimensional array is an array having a pair of square brackets(or having two dimensions). It is an example of multidimensional array. It has a double size specification. A two-dimensional array is used for the implementation of a matrix. When a two-dimensional array is implemented as a matrix, the first dimension represents the number of rows and the second dimension represents the number of columns. E.g. a[2][2]a[3][2]a[4][4].

An array a[2][2] will have four elements which are a[0][0]a[0][1]a[1][0]a[1][1].