Arrays Records and Pointers
Arrays Records and Pointers
•Linear Search
•Binary Search
Multidimensional Arrays
• The arrays whose elements are accessed by more
than one subscript are termed as
multidimensional arrays.
• Two-Dimensional array
– A two dimensional m × n array A is a collection of
m.n data elements such that each element is
specified by a pair of integers (such as J, K) called
subscripts, with the property that 1 ≤ J ≤ m and 1
≤K≤m
– The element of A with first subscript J and second
subscript K will be denoted by A[J, K]
Representation of Two-Dimensional
Arrays in Memory
• Let A be a two-dimensional m × n array.
• Although A is pictured as a rectangular array
of elements with m rows and n columns, the
array will be represented in memory by a
block of m.n sequential memory locations.
• The programming language stores the array A
either as column by column, or what is called
column-major order, Or row by row, in row
major order.
Representation of Two-Dimensional
Arrays in Memory
Representation of Two-Dimensional
Arrays in Memory
• Here, w is the number of words per memory
cell for the array LA, and 1 is the lower bound
of the index set of LA. M is number of rows in
array and N is number of columns in array.
• For Column-major order
LOC(A[J, K]) = Base(A) + w[M(K-1) + (J-1)]
• For Row-major order
LOC(A[J, K]) = Base(A) + w[N(J-1) + (K-1)]
Example
• FIND OUT THE LOCATION OF A[3][2] of a 3 X 4
INTERGER MATRIX WITH BASE ADDRESS 1000
Base (A) : 1000
w : 2 (because an integer takes 2 bytes in memory)
N:4
J:3
K:2
Now put these values in the given formula as below:
LOC (A [3, 2]) = 1000 + 2 [4 (3-1) + (2-1)]
= 1000 + 2 [4 (2) + 1]
= 1000 + 2 [8 + 1]
= 1000 + 2 [9]
= 1000 + 18 = 1018
Algorithm 4.7 (Matrix Multiplication)
MATMUL(A, B, C, M, P, N)
Let A be an M × P matrix array, and let B be a P × N matrix array.
This algorithm stores the product of A and B in an M × N matrix
array C.
1. Repeat steps 2 to 4 for I = 1 to M:
2. Repeat steps 3 and 4 for J = 1 to N:
3. Set C[I, J] = 0
4. Repeat for K = 1 to P:
C[I, J] = C[I, J] + A[I, K] * B[K, J]
[End of inner loop]
[End of step 2 middle loop]
[End of step 1 outer loop]
5. Exit
MATMUL(A, B, C, M, P, N)