Arrays


An array holds a fixed number of similar elements that are stored under one name. These elements are stored in contagious memory locations. It is one of the simplest data structures. Most modern programming languages have arrays built-in by default.


Declaring a One Dimensional Array


An array has to be declared before it can be used. In C, declaring an array means specifying the following:


Data Type: This is the kind of values that the array will store. This can be characters, integers, floating points or any legal data type.

Name: The variable name used to identify the array and interact with it.

Size: The size of the array, which specifies the maximum number of values that the array will store.

Syntax Used


An array can be declared in C by using the following syntax:


type name[size];

Assigning values after initialization


By default, an array is created whenever memory is available at any random location. We do not know what information that random location of memory will contain, as any other program could have used that memory previously.


If array elements are not initialized while creation, then accessing them directly they would result in such garbage values.


Therefore, it is always recommended to empty the elements or assign values to it if a calculation is to be performed on the array.


int ages[10];
    // accessing array without assigning elements first
for(int i = 0; i < 10; i++)
  printf("\n arr[%d] = %d", i, ages[i]);
        

Traversing the array


Inserting an element at the end of the array is easy provided the array has enough space for the new element. The index of the last element of the array is found out and the new element is inserted at the index + 1 position.


At any other position


An element can be inserted in between at any position by shifting all elements from that position to the back of the array. The element to be inserted is then inserted at the required position.


void insert_position(int arr[]) {
          int i = 0, pos, num;
          printf("Enter the number to be inserted : ");
          scanf("%d", &num);
          printf("Enter position at which the number is to be added :");
          scanf("%d", &pos);
          for (i = n-1; i>= pos; i--)
              arr[i+1] = arr[i];
          arr[pos] = num;
          n = n + 1;  // increase total number of used positions
          display_array(arr);
      }
      

Multi-Dimensional Arrays


An array may have more than one dimension to represent data. These are known as multidimensional arrays. The elements in these arrays are accessed using multiple indices.


Two Dimensional Array


A two dimensional array can be considered as an array within an array. It can be visualised as a table, having a row and a column. Each item in the table can be accessed using 2 indices corresponding to the row and column.


A 2D array is declared using 2 parameters:


type name[max_size_x][max_size_y]

The max_size_x and max_size_y are the max values each dimension can store.


Three Dimensional Array


A three dimensional array similarly can be visualised as a cube. Each item can be accessed using 3 indices corresponding to the 3D position.


Example: Every block of a Rubik’s Cube can be represented by a three dimensional array of size 3 x 3 x 3.


A 3D array is declared using 2 parameters:


type name[max_size_x][max_size_y][max_size_z];

The max_size_x, max_size_y and max_size_z are the max values each dimension can store.


Memory Allocation in arrays


Since an array stores all its data elements in consecutive memory locations, storing just the base address, that is, the address of the first element is sufficient. The address of other elements can be then calculated using the base address.


For one dimensional array


A simple formula consisting of the size of the element and the lower bound is used.


A[i] = base_address(A) + size_of_element(i – lower_bound)

The lower bound is the smallest index in the array. Similarly, an upper bound is the largest index in the array. In C programming, the lower bound value may be omitted as it is generally 0.


For two dimensional array


The elements in a two dimensional array can be stored using 2 representations and their addresses can be calculated using the respective formulae.


Column Major Representation


In this form, the elements are stored column by column. m elements of the first column are stored in the first m locations, elements of the second column element are stored in the next m locations, and so on.


Address(A[I][J]) = base_address + width {number_of_rows (J – 1) + (I – 1)}

Row Major Formula


In this form, the elements are stored row by row. n elements of the first row are stored in the first n locations, elements of the second row elements are stored in the next n locations, and so on.


Address(A[I][J]) = base_address + width {number_of_cols (I – 1) + (J – 1)}

Time Complexity of Operations


Access


Any array element could be accessed directly through its index. Hence the access time is constant O(1).

Search


Searching for a given value through the array requires iterating through each element in the array until the element is found. This is assuming that linear search is used (which is the most basic type of search to find any element). This makes the search time O(n).
The other more efficient search algorithm, binary search could be used to search in O(log n) time but it requires the array to be sorted beforehand.

Insertions


Inserting an element in between 2 elements in an array involves shifting all the elements to the right by 1. This means that at most all the elements have to be shifted right (insertion at the beginning of the array), hence the complexity of the insert operation in O(n).

Deletions


Deleting an element in between 2 elements in an array involves shifting all the elements to the left by 1. This means that at most all the elements have to be shifted left (deletion at the beginning of the array), hence the complexity of the delete operation in O(n).

Space Required


An array only takes the space used to store the elements of the data type specified. This means that for storing n elements the space required is O(n).