# One Dimensional Array

In the previous post we introduce the array data structure. In this post we introduce one dimensional array. If only one subscript is required to reference all the element of the array, then the array is termed as one dimensional array.

**Memory Allocation for One Dimensional Array**

Memory representation of an array is very simple. Suppose an array A[100] is to be stored in a memory as shown in the below figure.

Let the memory location where the first element is to be stored be **M**. If each element requires one word, then the location(or address) of any element say **A[i]** in an array can be obtained by

$$loc(A[i]) = M + (i – 1)$$

Likewise, in general, an array can be written as **A[L…U]**, where **L** and **U** denote the lower and upper bound for the index. If the array is stored starting from the memory location (Base address) **M**, and for each element it requires w number of words, then the address of **A[i]** will be:

$$loc(A[i] = M + (i – L) * w$$

The above formula is known as indexing formula, which is used to map the logical presentation of an array to physical presentation. By knowing the starting address of an array M, the location of the \(i^{th}\) element can be calculated directly. Therefore the access time of any element in the array is constant i.e. \(\mathcal O(1)\).

Suppose an array **A[-15 … 64] **is stored in a memory whose staring address is **459**. Assume that the word size for each element is **2**. Then obtain the following:

- How many number of elements are in the array
**A**? - If one word of the memory is equal to
**2**bytes, then how much memory is required to store the entire array? - What is the location of
**A[50]**? - What is the location of the \(10^{th}\) element?
- Which element is located at
**589**?

In the indexing formula \(loc(A[i] = M + (i – L)*w\), we have taken that the array is stored from the lower region of the memory to the higher region of the memory. But in some computers the convention is just reversed, that is, array is stored from the higher region to lower region i.e. \(loc(A[i]) \gt loc(A[i+1])\) for all \(L \le i \le U\). Modify the above formula for this case?

**Basic operations on Array**

Various operations that can be performed on an array are: traversing, sorting, searching, insertion, deletion and merging.

**Application of Array**

There are numerous applications of arrays in computation. This is why almost every programming language provide native support to array (as a built-in data type).

**Array Can be Used for Sorting Elements**As widely known arrays play an important role in sorting algorithms!**Array Can Perform Matrix Operation**Matrix multiplication is greatly done using arrays.**Array Can be Used in CPU Scheduling**Arrays can be used in various CPU scheduling algorithms. CPU Scheduling is generally managed by Queue. Queue can be managed and implemented using the array. Array may be allocated dynamically i.e at run time.**Array Can be Used in Recursive Function**When the function calls another function or the same function again then the current values are stores onto the stack and those values will be retrieve when control comes back. This is similar operation like stack.

This article is contributed by Ram Kripal. If you like eLgo Academy and would like to contribute, you can mail your article to admin@elgoacademy.org. See your article appearing on the eLgo Academy page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.