Data Structure Array

An array is a collection of similar data types. We can store more than one item in only one variable i.e. Array. So Array is used when there is requirement to add more items in a single variable.

To explain the concept of arrays, we will take an example of an employee. Suppose a company has 25 number of employees. So to define each employee in computer memory, we need 25 variables and to print value of variables and we need to write 25 print statements. This will be more hectic if the employee size increases to 200 or 400.

Array is used to solve such problem. It can process large volume of data. The elements of an array are referenced by an index and stored in consecutive memory locations.


Create an array

To use an array, it must be first declared. The syntax of an array declaration is -

type name[size];

type - name of the data type, like int, char, float.
name - name of an array.
size - maximum number of array values that an array can hold.

Example

int employee[25];

In the above example, employee is an array containing 25 elements.


Accessing elements of an array

Generally the array index starts with zero index. Like, first element stores at index 0, second element stores at index 1 and 25th element stores at index 24.

Element 1 Element 2 .... Element 25
employee[0] employee[1] .... employee[24]

The array index is used to access an array element, but to access all elements of an array, we must use a loop.

Example

int i, employee[10];
employee[5] = 10;
int i, employee[10];
for(i = 0; i<10; i++) {
	employee[i] = 30;
}




Getting address of an array

There is a simple formula to calculate the address of an array.

Formula

A[k] = BA(A) + w(k - lower_bound)

Here, A is the array, BA is the base address of an array, k is the index of an element and w is the size of one element in the memory.

Example

Element 1 Element 2 .... .... Element 25
employee[0] employee[1] .... .... employee[24]
1000 1002 .... .... 1048

To get address of second element, the calculation is -

employee[1] = 1000 + 2(1 - 0)
	= 1002




Operations on an array

These are the different operations perform on an array.

  • Traverse
  • Insertion
  • Deletion
  • Merge
  • Update
  • Search

Traverse

Traverse is a process of printing all array elements one by one.


Insertion

Insertion is a process of inserting an element in an existing array either at the end of an array or at the required position.

Algorithm
Start
Set x = len
Repeat for x = len down to pos
set a[x + 1] = a[x]
[End of loop]
Set a[pos] = num
Set len = len + 1
End

Deletion

Deletion is a process to delete an existing element from an array and organize the remaining elements.


Merge

Merging is a process of combining two arrays into a new array. The simplest process of array merging is to first create an empty array then copy the first array elements into empty array and then copy all the second array elements.

Example
a[0]	a[1]	a[2]	a[3]	a[4]
30	32	12	45	60

b[0]	b[1]	b[2]
45	20	21

c[0]	c[1]	c[2]	c[3]	c[4]	c[5]	c[6]	c[7]
30	32	12	45	60	45	20	21

Updatation

Updatation is the process of updating an existing element of an array at a given index.


Search

Search is the process of searching an array element either its value or index.


One Dimensional Array

One dimensional array is sequentially allocated data structure which is very simple. It requires a fixed number of memory allocation.


Two Dimensional Array

Two dimensional array is like a table or matrix, in which elements are stored in column and row wise.