0% found this document useful (0 votes)
13 views

Lecture 2

Uploaded by

Abir Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 2

Uploaded by

Abir Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Array operations

Basic Operations
• Traverse − print all the array elements one by one.

• Insertion − Adds an element at the given index.

• Deletion − Deletes an element at the given index.

• Search − Searches an element using the given index or by the value.

• Update − Updates an element at the given index.

• Display − Displays the contents of the array.


Search in array int main()
{
int arr[] = { 12, 34, 10, 6, 40 };
int findElement(int arr[], int n, int key) int n = sizeof(arr) / sizeof(arr[0]);
{
int i; // Using a last element as search element
for (i = 0; i < n; i++) int key = 40;
if (arr[i] == key)
return i; int position = findElement(arr, n, key);

// If the key is not found if (position == -1)


return -1; printf("Element not found");
} else
printf("Element Found at Position: %d", position + 1);

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;

// Cannot insert more elements if n is printf("\n Before Insertion: ");


// already more than or equal to capacity for (i = 0; i < n; i++)
if (n >= capacity) printf("%d ", arr[i]);
return n;
// Inserting key at the end
arr[n] = key; n = insertData(arr, n, key, capacity);

return (n + 1); printf("\n After Insertion: ");


} for (i = 0; i < n; i++)
printf("%d ", arr[i]);

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

• Row: Index of row, where non-zero element is located


• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index – (row,column)
2D representation
Example int compactMatrix[3][size];
// Making of new matrix
int k = 0;
#include<stdio.h> for (int i = 0; i < 4; i++)
int main() for (int j = 0; j < 5; j++)
{ // Assume 4x5 sparse matrix if (sparseMatrix[i][j] != 0)
int sparseMatrix[4][5] = {
{ compactMatrix[0][k] = i;
{0 , 0 , 3 , 0 , 4 }, compactMatrix[1][k] = j;
{0 , 0 , 5 , 7 , 0 }, compactMatrix[2][k] = sparseMatrix[i][j];
{0 , 0 , 0 , 0 , 0 }, k++;
{0 , 2 , 6 , 0 , 0 } }
}; for (int i=0; i<3; i++)
int size = 0; {
for (int i = 0; i < 4; i++) for (int j=0; j<size; j++)
for (int j = 0; j < 5; j++) printf("%d ", compactMatrix[i][j]);
if (sparseMatrix[i][j] != 0) printf("\n");
}
size++; return 0;
}
Address of an element in array
• Address of A[I] = B + W * (I – LB)

• I = Subset of element whose address to be found,


• B = Base address,
• W = Storage size of one element store in any array(in byte),
• LB = Lower Limit/Lower Bound of subscript(If not specified assume zero).
• Ex: Given the base address of an array A[1200 ………… 1600] as 1020 and
the size of each element is 2 bytes in the memory, find the address of
A[1500]
• Ans: Address of A[1500] = 1020 + 2 * (1500 – 1300)
Row Major and Column Major
• To find the address of any element in a 2-Dimensional array there are
the following two ways-
• Row Major Order
• Column Major Order
Row Major Ordering
• Row major ordering assigns successive elements, moving across the rows and then down the next row, to
successive memory locations. In simple language, the elements of an array are stored in a Row-Wise fashion

Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))


I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero),
N = Number of column given in the matrix= Upper Bound – Lower Bound + 1

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:

Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))


I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it as zero),
M = Number of rows given in the matrix.

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?

Ans: Address of arr[3][3][3] = 400 + 4 * ((8*11*(3-1)+8*(3-(-5)+(3-(-10))) = 1412


Exercise
Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns
where the sum of elements in the row is equal to the sum of elements in the columns.
Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}
Output: {{1, 1}}
Explanation: The sum of elements of rows and columns of matrix M are:

C=1 C=2 C=3


R=1 1 2 2 5
R=2 1 5 6 12
R=3 3 8 9 20
5 15 17
Steps
• Declare to arrays, arrR[] to store the sums of each row, and arrC[] to
store the sums of each column.
• Using nested loops calculate the sums of each row and store the
values in arrR[].
• Using nested loops calculate the sums of each column and store the
values in arrC[].
• Use the nested loops to check if for any pair of rows and columns the
sums are equal and store them if possible.
Problem2
• Given a 2D array of size M x N. Calculate count of positions in 2D array
where address as per row-major order equals to address as per
column-major order.
Row major address = B + w * (N * (i-1) + j-1)
Column major address = B + w * (M * (j-1) +
i-1)

Iterate for all possible i and find


corresponding j
If j comes out to be an integer in the range 1
to N,
increment the counter.

You might also like