What is an array in data structure?

Learn Tech 4U - What is an array in data structure?

- An array is the collection of the finite number of homogeneous data elements. We can say that it is a group of memory blocks or elements of the same nature of data that store data in a variable.

- Elements of the arrays are referenced respectively by an index set consisting of n consecutive numbers and are stored respectively in successive memory locations.

Example:

main() {
  
 int a[10];

}

Length of an array:- The number of elements is called the length or size of the array.

Accessing an array element:- The array elements can be accessed in constant time by using the index of the particular element.

To get the memory address of an array element:- To access an array element, address of element is computed as an offset from the base address of the array and one multiplication is required to compute, What is supposed to be added to the base address to get the memory of the element.

Example:-
                  
                int a[3] having an address of 106.

                formula to get the address of element:
                       => 100 + sizeOf(int) * 3 = 106.  [ Here: 100 is address of initial element, sizeOf(int) gives us = 2 and 3 is the 3rd element ].

- First, the size of an element of that data type is been calculated and then it will be multiplied with the index of the element to get the value to be added to the base address.

Conclusion:- The process takes one multiplication and one addition. Since these two operation takes constant time, We can say the array access can be performed in constant time.

Dimensions of an array:-

  1. One dimension array (1D): The list of data items that can be represented by one variable name using the only one subscript and such variable is called one dimension array. Example int a[3], int a[10] or int a[1000] and etc.
  2. Two-dimension array (2D): The list of data items that can be conceptualized as rows and columns, Which can be represented using one name and two subscripts. Example int a[4][3], int [3][3] and etc.
  3. Multidimensional Arrays: It can be defined as an array of arrays. Data in multidimensional arrays or 3D arrays are stored in tabular form (in row-major order). Example int a[2][3][3], int [3][3][3] and etc.