Array –array is a linear data structure which stores same data elements into contiguous way.

Let’s say,we have an array of 4 elements.The elements are 22, 34,-65, 8. Stored elements into array cells or block.

So,size/length of the array will = 4.

The graph of array will –

Declaring an array

An array can be declared in many ways. See below most common two ways –

int a[] = {22,34,-65,8};

Or,

int a[] = {22,34,-65,8};

Let’s make some programs about array declaration and displays array elements.

Remember: if you declare an array and initialize some values of it, then other block/cells of array by default initialize with 0(zero). See below code, to understand this.

Note: array can randomly accessed but a linked list can’t?

Ans – In array, data elements get stored in a contiguous memory location. So when we get first index of an array, we can easily calculate the address of anyelement, by using that element index in array.

In the case of linked list, element don’t get stored into contiguous memory location. So accessing any particular element using its index, from any particular element index can’t possible.

Array types

Generally array are two types – one dimensional array and multi-dimensional array. Multi-dimensional array can be many types like – 2D,3D,4D etc.

Generally on basics of most usage, array treated as 3 types –

1. 1D array – one dimension(like X)
2. 2D array – two dimension(like X , Y)
3. 3D array – three dimension(like X,Y,Z)

Calculating address of 1D array element.

B.A = base address/starting address, i = index of desired element, w = array block size (data-type size).

A[i] = i*w + B.A

A[i] = (i-1)*w + B.A

Problem 1: We assuming, we have an array of 5 integers arr = (10, 20, 44, 55, 65). Starting (base) address = 200. Calculate the address of element ‘55’.

Ans–each block of array size = 2 byte (assume integer size as 2 byte)

Base Address = 200. Starting index = 0(when in question starting index not mentioned)

Indexes of all elements will –

Desired element ‘44’, index is 2. So i = 2

So, according to formula:A[i] = i*w + B.A

arr = 2*2 + 200 [i=2,w=2,B.A=200]

arr = 204.

So, with index and address of elements array will look like –

Problem 2: We assuming, we have an array of 4 integers arr = (10, 20, 44, 55). Starting (base) address = 100. Calculate the address of element ‘55’. Starting index = 1.

Ans – each block of array size = 2 byte (assume integer size as 2 byte)

Base Address = 100. Starting index = 1

Indexes of all elements will –

Desired element ‘55’, index is 3. So i=4

So, according to formula: A[i] = (i-1)*w + B.A

arr = (4-1)*2 + 100 [i=4,w=2,B.A=100]

arr = 106.

So, with index and address of elements array will look like –

But wait a second, when your staring index with 3,4,5,6,7,8,…… n. then how you calculate address of any element?

i = index of element, l.b = lower bound of array(starting index) , w=array block size, B.A=base address

Universal formula: a[i] = (i – l.b)*w + B.A

Problem 3: We assuming, we have an array of 4 integers arr = (10, 20, 44, 55). Starting (base) address = 100. Calculate the address of element ‘55’. Starting index = 5.

Ans – each block of array size = 2 byte (assume integer size as 2 byte)

Base Address = 100. Starting index = 5,sol.b = 5.

Indexes of all elements will –

Desired element ‘55’, index is 8.i = 8

So, according to formula: A[i] = (I – l.b)*w + B.A

arr = (8-5)*2 + 100 [i=8,w=2,B.A=100]

arr = 106.

So, with index and address of elements array will look like –

In array, we have different type of operations to perform.

Some of them are –

1. Insert new element into array
2. Delete element from array
3. Search any element in array
4. Sort array by assessing or descending order

In array operations, you also get touch with traverses of elements.

But basically traverse basically an approach. There have a common difference between traverse and searching. Assume two cases, when your teacher says open a page in your book, but don’t follow the index page, so you need to traverse one by one page in book to follow the right content.

But if the teacher says, you to find the content from page’s index. So at first you see the index page, then you find the chapter; searching basically works these way.