Professional Documents
Culture Documents
Lec 2.1 (Arrays)
Lec 2.1 (Arrays)
5
Merging Algorithm
Algorithm: Merging (A, R,B,S,C)
Here A and B be sorted arrays with R and S elements respectively. This algorithm
merges A and B into an array C with N=R+ S elements.
7
Important points
• Arrays can be declared as: AUTO(1932: 1984);
where AUTO[K] = value at K.
• Length = UB – LB + 1
Otherwise,
LOC(A[J,K]) = Base(A) + w[N(J - LB1) + (K - LB2)]
Q. 1) Consider a 2D array X whose index set of
first and second dimensions ranges from -3 to
3 and 2 to 5 respectively (i.e X[-3..3 , 2..5]
a) Find the length of each dimension and size of
this 2D array
b) If this array is stored in row major order then
find the address of X[1,3].Given
Base(X)=5000 and w=2 bytes
Q. 2) Given an array A[10,7] is stored in row
major order. An element A[3,3] is stored at
address 1064 and element A[5,4] is stored at
address 1124. Find the address of A[4,6] and
A[1,2]?
Representation of 2D array in memory
(Column-major)
• Similarly, for Column-major (with starting index LB1=LB2=1):
Otherwise,
LOC(A[J,K]) = Base(A) + w[M(K – LB2) + (J – LB1)]
Q.1) Consider a 2D array X whose index set of
first and second dimensions ranges from -3 to
3 and 2 to 5 respectively (ie X[-3..3 , 2..5]
a) Find the length of each dimension and size of
this 2D array
b) If this array is stored in column major order
then find the address of X[1,3].Given
Base(X)=5000 and w=2 bytes
Q. 2) Given an array A[10,7] is stored in column
major order. An element A[3,3] is stored at
address 1064 and element A[5,4] is stored at
address 1124. Find the address of A[4,6] and
A[1,2]?
Sparse Matrices
Many zero values lead to wastage of memory
and number of operations.
Therefore we use sparse matrices
Regular sparse matrices- Lower
triangular matrix
A₁₁ 0 0 0 0
A₂₁ A₂₂ 0 0 0
A₃₁ A₃₂ A₃₃ 0 0
A₄₁ A₄₂ A₄₃ A₄₄ 0
A₅₁ A₅₂ A₅₃ A₅₄ A₅₅
C={A₁₁,A₁₂,A₁₃,A₁₄,A₁₅,A₂₂,A₂₃,A₂₄,A₂₅,A₃₃A,A₃₄,A₃₅,A₄₄A₄₅,A₅₅}