Lecture 2
Lecture 2
Basic Operations
• Traverse − print all the array elements one by one.
return 0;
}
Insertion (unsorted array)
• At end
• At Beginning
• At middle
Insert at ith Position
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
Example-Sample int main()
{
code int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int insertData(int arr[], int n, int key, int capacity) int n = 6; //insert 6th element
{ int i, key = 26;
return 0;
}
Deletion
• At end
• At Beginning
• At middle
Pseudocode :
funcDelete(K,N) //Delete from end of the array
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Sparse Matrix
• If most of the elements of the matrix have 0 value, then it is called a sparse matrix
Use of Sparse Matrix
• Storage: There are lesser non-zero elements than zeros and thus lesser memory
can be used to store only those elements.
• Computing time: Computing time can be saved by logically designing a data
structure traversing only non-zero elements
00304
00570
00000
02600
Representation
• Array representation
• Linked list representation
• Test whether a matrix is sparse matrix or not
(no. of 0s>(total no. of element)/2)
Using Arrays:
• 2D array is used to represent a sparse matrix in which there are three rows named as
Ex: 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
Ans: Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))=210
Column Major
• If elements of an array are stored in a column-major fashion means moving across the column and then to the next
column then it’s in column-major order. To find the address of the element using column-major order use the following
formula:
Ex: 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
Ans: Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1)) = 157
3D array: Row major & Column Major
• A 3-Dimensional array is a collection of 2-Dimensional arrays. It is
specified by using three subscripts:
• Block size
• Row size
• Column size
Row Major-3D
• Address of A[i][j][k] = B + W *(M * N(i-x) + N *(j-y) + (k-z))
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
M = Row (total number of rows)
N = Column (total number of columns)
P = Width (total number of cells depth-wise)
x = Lower Bound of Row
y = Lower Bound of Column
z = Lower Bound of Width
Ex: 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?
Ans: Address of arr[5][-1][8] = 400 + 2 * {[9 * 6 * (5 – 1)] + 6 * [(-1 + 4)] + [8 – 5]}= 874
Column Major: 3D
Address of A[i][j][k]= B + W(M * N(i – x) + M *(k – z) + (j – y))
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
M = Row (total number of rows)=Upper Bound – Lower Bound + 1
N = Column (total number of columns)=Upper Bound – Lower Bound + 1
P = Width (total number of cells depth-wise)
x = Lower Bound of Row
y = Lower Bound of Column
z = Lower Bound of Width
Ex: 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?