1. Arrays
Definition:
An array is defined as a set of finite number of homogeneous
elements or same data items:
Declaration of array is as follows:
Syntax: Data type Array Name [Size];
Example: int arr[10];
Where int specifies the data type or type of elements arrays stores.
“arr” is the name of array & the number specified inside the
square brackets is the number of elements an array can store, this
is also called sized or length of array.
2. • Calculating the length of the array:
• The elements of array will always be stored in the consecutive
(continues) memory location.
• The number of elements that can be stored in an array, that is
the size of array or its length is
• given by the following equation:
• A[n] is the array size or length of n elements.
• The length of the array can be calculated by:
• L = UB – LB + 1
• Example:
If an array A has values 10, 20, 30, 40, 50, stored in location
0,1, 2, 3, 4 the UB = 4 and LB=0
Size of the array L = 4 – 0 + 1 = 5
3. Classification of linear arrays
• A linear array is a list of fixed size number of similar data type
elements.
Arrays are categorized as Single /one-dimensional array and
multidimensional arrays
• Single Dimension Array:
• Array with one subscript
– Ex: int A[i];
• Two Dimension Array
– Array with two subscripts (Rows and Column)
– Ex: int A[i][j];
• Multi Dimension Array:
– Array with Multiple subscripts
– Ex: int A[i][j]..[n];
5. • Representation of linear arrays in memory:
• The elements of linear array are stored in consecutive memory locations.
• It is shown below:
•
•
.
• Index starts with 0.
• Array length is 8 which means it can store 8 elements.
• Each element can be accessed via its index. For example, we can fetch element at
index 6 as 27.
• In linear array location of element A[i] can be calculated using following equation:
• LOC( A[i])=Base_Address+W*(i)
• Base_address is the adress of the frist element in the array
• W is the word size.It means number of bytes occupied by each element.
6. Operations on array:
• Traversing: means to visit all the elements of the array in an
operation is called traversing.
• Insertion: means to put values into an array
• Deletion / Remove: to delete a value from an array.
• Sorting: Re-arrangement of values in an array in a specific order
(Ascending or Descending) is called sorting.
• Searching: The process of finding the location of a particular
element in an array is called searching.
7. • Traversing:
It is used to access each data item exactly once so that it
can be processed:
• We have linear array A as below:
1 2 3 4 5
10 20 30 40 50
• Here we will start from beginning and will go till last
element and during this process we will access value of
each element exactly once as below:
• A [0] = 10
• A [1] = 20
• A [2] = 30
• A [3] = 40
• A [4] = 50
9. • Insertion into Array:
• Insertion:
• It is used to add a new data item in the given collection of data
items.
• it is quite easy to insert the element in the end of array
provided sufficient memory location are present to
accommodate the additional element.
• To insert the element at our desired locations like some
where in the middle of array, all the elements must be moved
to next locations to add new element and to keep their order.
• If we are inserting an element in the beginning of an
array, then all the elements should be moved to next locations
to accommodate the new element.
10. • Algorithm: Insert (A,N,LOC,ITEM)
• Here A is a linear array with N elements. LOC is the location. Where
ITEM is to inserted.
1. set I=N
[ initialize Counter ]
2. Repeat While (I >= LOC)
3. Set A [ I + 1 ] = A [ I ]
[Move elements to next locations]
4. Set I = I - 1
[Decrease counter by 1]
5. Set A [ LOC ] = ITEM
[ Insert element at LOC Position]
6. Set N = N + 1
[ Increment N by 1]
7. Exit
11. • Visual Representation :
• Example:
• Let's take an array of 5 integers.
• 1, 20, 5, 78, 30.
• If we need to insert an element 100 at position 2, the
execution will be,
• Insert element in array
• 1. We need to insert element 100 at position 2.
• 2. Move all the elements from the last index(4) to the
position(2) to one position right.
• arr[4] (30) will be placed in arr[5].
• arr[3] (78) will be placed in arr[4].
• arr[2] (5) will be placed in arr[3].
• 3. Finally, the element 100 is placed at the position 2.
13. • Deletion from Array:
• Deletion: It is used to delete an existing data item from the given collection
of data Items.
• It is quite easy to delete the element from the end of array. To delete the
element from our desired location like some where in the middle of the
array or beginning of the array then all the subsequent elements must be
moved one location left to keep the order of array.
• Deletion 30 from Array at Pos 3
• A [0] = 10
• A [1] = 20
• A [2] = 30
• A [3] = 40
• A [4] = 50
15. • Two Dimensional Array :
• Arrays which have elements with two subscripts are known as two
dimensional arrays. A two dimensional array consist of rows and columns.
• It is also known as double dimensional arrays .
• The general format of two dimensional array is :
• Data type arrayname[size-1][size-2];
• Here data type represents type of elements stored in the array such as int,
float,char,double.
• Array name is the name of the array.
• Size-1 and Size-2 specifies the maximum number of elements can be stored
in the array.
• Size-1 specifies row size, size-2 specifies column size.
16. • Example:
• int A[3][4];
• A is a two dimensional array which contains 3 Rows and 4 Columns.
• Multi Dimensional Array:
– Array with Multiple subscripts
– Ex: int A[i][j]..[n];
– EX: A[3][3][3]
– A is a 3D array is essentially an array of arrays of arrays .it is a
collection of 2D arrays.
18. • Memory Representation of Multi dimensional array:
• 1. Row – Major Representation
• 2. Column – Major Representation
Row – Major Representation:
In this method elements of an array are arranged sequentially Row by Row.
Ex: int A[3][3] = { 1,2,3,4,5,6,7,8,9}
And it will be represented in memory with row major representation as follows
A[0][0]
1
A[0][1]
2
A[0][2]
3
A[1][0]
4
A[1][1]
5
A[1][2]
6
A[2][0]
7
A[2][1]
8
A[2][2]
9
A[0][0]
1
A[0][1]
2
A[0][2]
3
A[1][0]
4
A[1][1]
5
A[1][2]
6
A[2][0]
7
A[2][1]
8
A[2][2]
9
19. • The location of element A[i,j] can be obtained by evaluating Expression.
• LOC(A[i,j])=Base_Address +W[M(i) +(j)]
• Base_Address is the address of the first element of the array
• W is the word size it means number of bytes occupied by each element
• N is number of rows in array
• M is number of columns in array
20. 2. Column – Major Representation
In this method elements of an array are arranged sequentially Column by Column.
Ex: int A[3][3] = { 1,2,3,4,5,6,7,8,9}
And it will be represented in memory with Column major representation as follows
A[0][0]
1
A[0][1]
2
A[0][2]
3
A[1][0]
4
A[1][1]
5
A[1][2]
6
A[2][0]
7
A[2][1]
8
A[2][2]
9
A[0][0]
1
A[1][0]
2
A[2][0]
3
A[0][1]
4
A[1][1]
5
A[2][1]
6
A[0][2]
7
A[1][2]
8
A[2][2]
9
21. • The location of element A[i,j] can be obtained by evaluating Expression.
• LOC(A[i,j])=Base_Address +W[N(j) +(i)]
• Base_Address is the address of the first element of the array
• W is the word size it means number of bytes occupied by each element
• N is number of rows in array
• M is number of columns in array