Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, March 22, 2015

What is an array ?

  • An array is the collection of similar data type which is used to store the information in a random way.
  •  An array is a derived data type i.e. it is derived from the inbuilt data type.
  • The size of the array is fixed i.e. the size of an array is static that can not be changed at the time of execution. The size of an array is defined at the time of compilation and it should be an positive integer value.
  • The indexing of an array is starting from the 0 (zero) and end with size – 1.
  • The index of an array is also called the subscript of an array.
  • The name of an array is similar as a variable name and it also follow the all rules that followed by the variable or identifier at the time of declaration.
  •  An array is a linear data structure.
  • The elements of an array are stored in successive (continuous) memory location.

The types of an array are as follows:

  • One dimensional array
  • Two dimensional array (Matrix)
  • Multi dimensional array
  • Character array (String)
Syntax:
                data_type  array_name[size];
Ex:
                int ar[10];
Here ar is an array which contains 10 integer elements.

One Dimensional Array or Linear Array:

  • The one dimensional array is a type of array.
  • An array that uses only one subscript is known as one dimensional array.
  • The number of the elements in a one dimensional array is equal to the size of array.
  • The subscript of one dimensional array is starting from 0 (zero) and end with size -1.

Syntax:
                data_type Array_name [size];
Here data_type is represents the type of array elements, it can be integer, float or character. The Array_name is the name of the array and the size is represents the number of elements stored in an array. The size of an array is always a positive integer value.
Ex:
                int marks[5];

 

Memory representation of one dimensional array:


The memory representation of 5 elements of array is as follows:
Index
Elements
Memory Address
[0]
10
2000
[1]
15
2002
[2]
12
2004
[3]
13
2006
[4]
16
2008






The elements are integer type so it occupies 2 bytes memory space. Here, the memory address is started from 2000 and increased by 2. The indexing of array is starting from 0 (zero) and end with 4, so the total number of elements are 5 (from 0 to 4). The memory address is always positive.

 

Initialization of one dimensional arrays:


There are two ways to initialization of an array:
  • Compile time initialization
  • Run time initialization

Compile time initialization of one dimensional array:


                The compile time initialization of an array is a best think to initialize the array because by default it initialized by the garbage values. In compile time initialization values are initializes in the array elements when they are declared. The values that initializes in array elements are separated by commas. The maximum values that initialized in the array elements are equal to the size of an array. If the total numbers of initialized values are greater than the size of an array, an error will generated.
Syntax:
                data_type array_name[size] = { List of values}
Ex:
                int marks[5] = {87, 67, 90, 78, 88, 92};
                float percentage[3] = {88.70, 67.98, 76.45};

 

Run time initialization of one dimensional array:


                An array can also be initialized at the time of execution. Generally, array elements are initialized at run time by using loops, mainly for loop is used to initialize the values in array elements.
Ex:
                for( i = 0; I < 10; i++)
                {
                                scanf(“%d”,&roll[i]);
                }

 

The length of one dimensional array can be evaluated as:


Length = ( upper limit – lower limit)+1

 

The size of one dimensional array can be evaluated as:


Size = Length of array * size of data type (in bytes)

Two dimensional (2-D) array (or Matrix):

  • Two dimensional arrays is also a part of array.
  • It is also called matrix because the representation of two dimensional arrays is similar as matrix that is it contains both rows and columns.
  • The size of row is represents the total number of 1 – D (One dimensional ) arrays in the matrix and the size of column represents the total number of elements in one row.
  • The elements of two dimensional array or matrix are consecutively store in memory.
  • Total number of elements in a matrix is equal to the multiplication of row and column.
Syntax:
                data_type array_name[row_size][column_size];
Ex:
                int class[12][50];

 

Memory representation of two dimension array or matrix:

 

                The simple representation of 3 * 3 matrix is as follow:

Column 0
Column 1
Column 2
Index
[0][0]
[0][1]
[0][2]
Row 0
10
12
11
Index
[1][0]
[1][1]
[1][2]
Row 1
18
17
16
Index
[2][0]
[2][1]
[2][2]
Row 2
15
14
16

Initialization of two dimensional arrays:

 

The initialization of two dimensional arrays or matrix is similar as the initialization of one dimensional array.
Compile time initialization:
Syntax:
                data_type array_name[row_size][column_size] = {element list};
ex:
int mat[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Run time initialization:
Syntax:
                for(i = 0; I < 3; i++)
                {
                                for( j = 0; j < 3; j++)
                                {
                                                scanf(“%d”,&mat[i][j]);
                                }
                }

Advantage of Arrays:

  • Array is stored continuously in memory so we can access the array randomly.
  • Searching and sorting is very simple in arrays.
  • The insertion and deletion can be done randomly at any location in a array.
  • It has static type memory allocation.
  • Reduce the declaration of more variables.

Disadvantage of Arrays:

  • Slow searching.
  • Fixed size i.e. static in nature.
  • Insertion and deletion operations required requires movement of large amount of data so it is time consuming and inefficient.
  • Wastage of memory if less elements are stored in array.

1 comment:

  1. Very informative, keep posting such good articles, it really helps to know about things.

    ReplyDelete