BMC205 DSAA Unit1 Array Notes
BMC205 DSAA Unit1 Array Notes
Definition
Array is a linear data structure that is a collection of data elements of same types. Arrays
are stored in contiguous memory locations. It is a static data structure with a fixed size.
Representation of 1D array:
Syntax
The general form of declaring 1-dimensional arrays is shown below:
type arr_name[size];
type: Type of data to be stored in the array.
arr_name: Name assigned to the array.
size: Size of dimension.
Initialization of 1D Array
We can initialize a 1D array by using a list of values enclosed inside ‘{ }’ and separated by
a comma as shown in the example below:
int arr[4] = {0, 1 ,2 ,3 }
Multidimensional Array:
A multi-dimensional array can be defined as an array that has more than one dimension.
Having more than one dimension means that it can grow in multiple directions. Some
popular multidimensional arrays are 2D arrays and 3D arrays.
Syntax
The general form of declaring N-dimensional arrays is shown below:
type arr_name[size1][size2]….[sizeN];
type: Type of data to be stored in the array.
arr_name: Name assigned to the array.
size1, size2,…, sizeN: Size of each dimension.
Representation of 2D array:
Initialization of 2D Arrays
We can initialize a 2D array by using a list of values enclosed inside ‘{ }’ and separated by
a comma as shown in the example below:
int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
or
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
Three-Dimensional Array:
A Three-Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It
can be visualized as multiple 2D arrays stacked on top of each other.
Initialization of 3D Array in C
Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number
of dimensions increases so the number of nested braces will also increase.
int arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11}
or
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4, 5 } },{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };
1. Row Major Order: The technique stores the 2D array row after row. It means that the
first row is stored first in the memory, then the second row of the array, then the third
row, and so on.
2. Column Major Column: This technique stores the 2D array column after column. It
means that first column is stored first in the memory, then the second column, then the
third column, and so on.
Example: 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 (B) = 1020
Lower bound (LB) = 1300
Size of each element (W) = 2 bytes
Index of element (not value) = 1700
Calculate the address of any element in the 2-D array using row-major and column-
major order:
The 2-dimensional array can be defined as an array of arrays. The 2-Dimensional arrays are
organized as matrices which can be represented as the collection of rows and columns as
array[M][N] where M is the number of rows and N is the number of columns.
Example:
To find the address of any element in a 2-Dimensional array, there are the following two
ways-
1. Row Major Order
2. Column Major Order
To find the address of the element using row-major order uses the following formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Example:
Given an array, arr[1………10][1………15] with base value 100 and the size of each
element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major
order.
Solution:
Given:
Base address B = 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 column given in the matrix N = Upper Bound – Lower Bound + 1 = 15 – 1
+ 1 = 15
To find the address of the element using column-major order use the following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Example:
Given an array arr[1………10][1………15] with a base value of 100 and the size of each
element is 1 Byte in memory find the address of arr[8][6] with the help of column-major
order.
Solution:
Given:
Base address B = 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 Rows given in the matrix M = Upper Bound – Lower Bound + 1 = 10 – 1 +
1 = 10
From the above examples, it can be observed that 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 row, and in column-major order, first move down to the
first column and then next column. So both the answers are right.
So it’s all based on the position of the element whose address is to be found. For some
cases, same answers are obtained with row-major order and column-major order and for
some cases, different answers are obtained.
Calculate the address of any element in the 3-D Array using row-major and column-
major order:
A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using
three subscripts:
1. Index1
2. Index2
3. Index3
More dimensions in an array mean more data can be stored in that array.
Example:
To find the address of any element in 3-Dimensional arrays there are the following two
ways-
Row Major Order
Column Major Order
Find the length Li of each dimension ‘i’ of array by using the formula
Li = UBi – LB i + 1
Now find the effective index Ei of Li for given index Ki by using the formula
Ei = Ki – LB i
Example: Given an array, arr[1:9, -4:1, 5:10] with a base value of 400 and the size of each
element is 2 Bytes in memory, find the address of element arr[5][-1][8] with the help of
row-major order?
Solution:
Given:
Base address B = 400
Storage size of one element store in any array(in Byte) W = 2
L1 = UB1 – LB1 + 1 = 9 – 1 + 1 = 9
L2 = UB2 – LB2 + 1 = 1 – (-4) + 1 = 6
L3 = UB3 – LB3 + 1 = 10 – (5) + 1 = 6
E1 = K1 – LB1 = 5 – 1 = 4
E2 = K2 – LB2 = (-1) – (-4) = 3
E3 = K3 – LB3 = 8 – 5 = 3
Formula used for row-major order:
Address of A[i][j][k] = B + W * ((E1 * L2 + E2) * L3 + E3)
Example: Given an array arr[1:8, -5:5, -10:5] with a base value of 400 and the size of
each element is 4 Bytes in memory find the address of element arr[3][3][3] with the help
of column-major order?
Solution:
Given:
Base address B = 400
Storage size of one element store in any array(in Byte) W = 4
L1 = UB1 – LB1 + 1 = 8 – 1 + 1 = 8
L2 = UB2 – LB2 + 1 = 5 – (-5) + 1 = 11
L3 = UB3 – LB3 + 1 = 5 – (-10) + 1 = 16
E1 = K1 – LB1 = 3 – 1 = 2
E2 = K2 – LB2 = 3 – (-5) = 8
E3 = K3 – LB3 = 3 – (-10) = 13
Time Complexity: O(NM), where N is the number of rows in the sparse matrix, and M is
the number of columns in the sparse matrix.
Auxiliary Space: O(NM), where N is the number of rows in the sparse matrix, and M is the
number of columns in the sparse matrix.
Transpose
Any row in the 1st matrix with a value equal to x and any row in the 2nd matrix (the
transposed one) with a value equal to y will contribute to the result[x][y]. The result [x][y] is
derived by multiplying all elements with col values in both matrices and only adding those
with row values of x in the original matrix and y in the 2nd transposed matrix.
Multiplication
To make our comparisons easier and keep the sorted order, we first calculate the transpose of
the 2nd matrix before multiplying the matrices. In order to obtain the resultant matrix, the
length of matrices must be traversed, and the relevant multiplied values must be added.
For example: The 5 by 5 lower triangular sparse matrix as shown in the above figure is
stored in one-dimensional array B is:
B = { A11, A21, A22, A31, A32, A33, A41, A42, A43, A44, A51, A52, A53, A54, A55}
Here B [1] = A11, B [2] = A21, B [3] = A22, ……………………… B [14] = A54, B [15] = A55
To calculate the total number of non-zero elements, we need to know the number of non-zero
elements in each row and then add them. Since the number of non-zero elements in an ith row
is ‘i’ so that the total number of non-zero elements in the lower triangular sparse matrix
of n rows is:
So B will contain only n * (n+1) / 2 elements which is about half as many elements
as a two dimensional n * n array.
Now we will want the formula that gives us the integer L in terms of J and K where B[L] =
AJK, where L represents the number of elements in the list up to and including AJK
Now there are 1 + 2 + ………… + (J-1) = (J-1) * J / 2 elements in the rows above
AJK and there are K elements in row J up to and including AJK.
( J 1) * J
L K
2
yields the index that accesses the value AJK from the linear array B.
For example, The 5 by 5 upper triangular sparse matrix, as shown in the above figure, is
stored in one-dimensional array B is:
B = {A11, A12, A22, A13, A23, A33, A14, A24, A34, A44, A15, A25, A35, A45, A55}
Here B [1] = A11, B [2] = A12, B [3] = A22, B [4] = A13,B [5] = A23 ,B [6] = A33, B [7] = A14 ,B
[8] = A24 ,B [9] = A34 ,B [10] = A44 ,B [11] = A15 ,B [12] = A25 ,B [13] = A35 , B [14] = A45, B
[15] = A55
In order to calculate the total number of non-zero elements, we need to know the number of
non-zero elements in each column and then add them. Since, the number of non-zero
elements in ith column is ‘i’ so that the total number of non-zero elements in upper triangular
sparse matrix of n columns is:
For example, The 5 by 5 tri-diagonal sparse matrix, as shown in the above figure, is stored in
one-dimensional array D is:
D = {A11, A12, A21, A22, A23, A32, A33, A34, A43, A44, A45, A54, A55}
Here D [1] = A11, D [2] = A12, D [3] = A21, D [4] = A22, D [5] = A23, D [6] = A32, D [7] =
A33, D [8] = A34, D [9] = A43, D [10] = A44, D [11] = A45, D [12] = A54, D [13] = A55
In order to calculate the total number of non-zero elements, we need to know the number of
non-zero elements along the diagonal, above the diagonal and below the diagonal. The
number of elements in a square matrix along the diagonal is 'n', above the diagonal is (n-1)
and below the diagonal is (n-1). So the total number of non-zero elements in n-square matrix
is:
n + (n-1) + (n-1)
the diagonal Above the diagonal Below the diagonal
Therefore, the total number of elements for n-square matrix = n + (n-1) + (n-1) = 3n-2
Now we will want the formula that gives us the integer L in terms of J and K where D[L] =
AJK, where L represents the number of elements in the list up to and including AJK
Now there are 3 * (J - 2) + 2 elements in the rows above AJK and (K – J + 1) elements to the
left of AJK.
L = {3 * (J - 2) + 2} + (K – J + 1) + 1
L 2* J K 2
yields the index that accesses the value AJK from the linear array D.