Arrays
Arrays
Properties of an Array
o Each element in an array is of the same data type and carries the
same size.
o Elements in the array are stored at contiguous memory locations
from which the first element is stored at the smallest memory
location.
o Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
Base Address and the Size of the data element.
Representation of an Array
We can represent an array in various ways in different programming
languages.
As per the above illustration, there are some of the following important
points:
Here, the memory allocation of an array arr of size 5. The Base Address
of the array is 100. It is the address of arr[0].
Here, the size of the data type used is of 4 bytes; therefore, each
element will take 4 bytes in the memory.
OR
Question 1:
An Array, A[-10 ..... +2] having Base Address (BA) = 999 and Size of an
element = 2 bytes, find the location of A[-1].
Solution:
Given the Base Address of an array A[1300 …… 1900] as 1020 and the
size of each element is 2 bytes in the memory, find the address
of A[1700].
Solution:
Given:
Base Address, BA = 1020
Lower Limit/Lower Bound of subscript, LB = 1300
Storage Size of one element store in any Array, W = 2 Byte
Subset of element whose address to be found, I = 1700
Formula used:
Address of A[I] = BA + W * (I – LB)
Calculation:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Types of Arrays:
Question:
Solution:
Given:
Base Address, BA = 100
Storage Size of one element store in any Array, W = 1 Bytes
Row Subset of an element whose address to be found, I = 8
Column Subset of an element whose address to be found, J = 6
Lower Limit of Row/Start Row Index of Matrix, LR = 1
Lower Limit of Column/Start Column Index of Matrix, LC = 1
Number of Column(s) given in the Matrix, N = [(Upper Bound – Lower Bound) + 1]
= [(15 – 1) + 1]
= 15
Formula:
Address of A[I][J] = BA + W * ((I – LR) * N + (J – LC))
Calculation:
Solution:
Given:
Base address, BA = 100
Storage size of one element store in any array, W = 1 Bytes
Row Subset of an element whose address to be found, I = 8
Column Subset of an element whose address to be found, J = 6
Lower Limit of Row/Start Row Index of matrix, LR = 1
Lower Limit of Column/Start Column Index of matrix, = 1
Number of Row(s) given in the matrix, M = [(Upper Bound – Lower Bound) + 1]
= [(10 – 1) + 1]
= 10
Formula used:
Address of A[I][J] = BA + W * ((I – LR) + (J – LC) * M)
Calculation:
For the same position two different address locations are obtained
that’s because in Row-Major Order, movement is done across the Rows
and then down to the next Rows. Whereas in the Column-Major Order,
movement is done across the Columns and then move to the next
Columns.
Advantages of Arrays:
Disadvantages of Arrays:
Programs in Arrays: