Jaypee University of Engineering and Technology
18B11CI311 – Data Structures
B.Tech -3rd Semester
Tutorial – 3
(Arrays)
1. Consider an array of 20 elements is stored in the memory of 40 bytes from 200 to 238.
Assume array index starts from 3. Find out the address of 10th index element.
Sol.
Step 1: Calculate the size of each element in the array
The total size of the array is 40 bytes, and it contains 20 elements. Therefore, the size of
each element can be calculated as:
Step 2: Determine the starting index
Since the array index starts from 3, the 10th index refers to the 7th physical element (since
10−3=7).
Step 3: Calculate the address of the 10th index element
The memory address of the first element (index 3) is 200. The address of the 10th index
element can be calculated by:
So, the address of the 10th index element is 214.
2. Consider an array of 30 elements is stored in the memory of 120 bytes from 3000 to
3116. Find out the address of 13th index element.
Sol.
Given:
The array has 30 elements.
The total memory used by the array is 120 bytes.
The array is stored from memory location 3000 to 3116.
We need to find the address of the 13th index element.
Step 1: Calculate the size of each element in the array
The total size of the array is 120 bytes, and it contains 30 elements. Therefore, the size of each
element is:
Size of each element= Total size120 bytes /Number of elements 30 elements =4 bytes
Step 2: Calculate the address of the 13th index element
The memory address of the first element (index 0) is 3000. The address of the 13th index
element can be calculated by:
Address of 13th index element=3000+(13×4)=3000+52=3052
So, the address of the 13th index element is 3052.
3. Consider a 2D array A of 30 (5x6) elements is stored in the memory of 120 bytes from
2100 to 2216. Find out the address of A[2][4] element in row-major and column-major
order.
Sol.
Given:
The 2D array A has dimensions 5×6 (5 rows and 6 columns), so it has 30 elements.
The array occupies 120 bytes of memory from address 2100 to 2216.
The size of each element = 120 bytes/30 elements=4 bytes.
We need to find the address of element A[2][4].
I. Row-Major Order
In row-major order, the address of an element A[i][j] can be calculated using the formula:
II. Column-Major Order
4. In a 2D integer array TD, assume that the row indices range from -3 to 7 and column
indices range from 6 to 14. An element TD [-3, 6] stored at address 3220. Find out the
dimension of TD and address of an element TD [2, 10], if TD stores the elements in
column major order.
Sol.
To solve this problem, we need to follow several steps to determine the dimensions of the
array and the address of the element TD[2,10].
Step 1: Determine the dimensions of the array TD
Given:
The row indices range from -3 to 7.
The column indices range from 6 to 14.
To determine the dimensions:
Number of rows: 7−(−3)+1=7+3+1=11
Number of columns: 14−6+1=9
Thus, the dimensions of TD are 11×9 (11 rows and 9 columns).
Step 2: Determine the size of each element
Since the address of the element TD[−3,6] is 3220,
and assuming integers are stored, we typically consider the size of an integer as 4 bytes
(this is standard in many systems).
Step 3: Calculate the address of TD[2,10] in column-major order
In column-major order, the elements are stored column by column. The formula for the
address of an element TD[i,j] in column-major order is:
Where:
jmin= 6
imin=−3
Number of rows = 11
Base address (address of TD[−3,6]TD[-3, 6]TD[−3,6]) = 3220
Size of each element = 4 bytes
For TD[2,10]TD[2, 10]TD[2,10]:
j=10, i=2
Substituting the values into the formula:
5. Assume you have given an array A[15][20]. Each element needs ‘W’ bytes of storage. If
the address of A[6][8] is 4440 and the base address at A[1][1] is 4000, find the width ‘W’
of each cell in the array A when the array is stored as Column Major Wise.
Solution:
Address of the element at ith row and jth column is given by,
loc(a[i] [j]) = base(a) + w * [m*(j - lbc) + (i - lbr)]
4440=4000+W*[15*7+5]
440/110=W
W=4 bytes
6. Consider a 2D array A[m][m], each element takes 4 bytes of storage. If the base address
at A[1][1] is 1500 and the address of A[4][5] is 1608, determine the order of the matrix
when it is stored in Column Major Wise.
Solution:
Address of the element at ith row and jth column is given by,
loc(a[i] [j]) = base(a) + w * [m*(j - lbc) + (i - lbr)]
1608=1500+4*[m*4+3]
108/4=m*4+3
27-3=m*5
24/4=m
m=6
7. Represent following matrix as triplet form (3-tuple form) using array. Also calculate the
amount of memory that can be saved or wasted. Assume each element occupies 4 bytes in
memory.
Solution:
6 6 8
1 1 15
1 4 22
1 6 -15
2 2 11
2 3 3
3 4 -6
5 1 91
6 3 28
The triplet matrix use 3x9=27
The original matrix takes 6x6=36
Profit=36-27=9memory space
The total memory save=9x4=36bytes.