Dsa Lab 02-2024
Dsa Lab 02-2024
Objective(s)
Implementation of the following array processing algorithms:
• Traversing an array
• insert
• delete
• search
Implementation of matrix operations using two dimensional arrays
Theory
Traversing
Our basic operation starts with visiting each element of the array exactly once and in order.
Insert
Given an array of length N, we have to insert an element in array at index i (0 <= i <= N-1). After
inserting an element, the number of elements in array will increase by one.
Delete
Given an array of length N, we have to delete an element at index i (0 <= i <= N-1) from an
array. After deleting an element, the number of elements in array will decrease by one.
Search
Given an unsorted array of N integers (N > 0) and an integer value v, write the function that
returns zero-based index in the array at which the value v is located. Function should return
negative value if array does not contain v.
Algorithms
Algorithm A1: travers(arr[])
1. Start
2. for i = 0 to N repeat step 3
3. PRINT arr[i]
1
LAB # 02
Algorithm A2: insert(LA , K , ITEM)
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J•1
7. Set LA[K] = ITEM
8. Stop
2
LAB # 02
Two dimensional Arrays
We have seen a one-dimensional array
The array elements are selected using a one index. A two-dimensional array is an array where
its elements are selected (identified) using two indices.
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is
the name of the array, and i and j are the subscripts that uniquely identify each element in a.
3
LAB # 02
Matrix Addition
We consider two input matrices A and B of order n x n. We need to computer C = A + B,
where C is of order n x n.
To add two matrixes sufficient and necessary condition is "dimensions of matrix A =
dimensions of matrix B".
Matrix Subtraction
We consider two input matrices A and B of order n x n. We need to computer C = A-B,
where C is of order n x n.
To add two matrixes sufficient and necessary condition is "dimensions of matrix A =
dimensions of matrix B".
Matrix Multiplication
We consider two input matrices A and B of order n x n. We need to computer C = A x B,
where, C is of order n 0x n.
To multiply two matrixes sufficient and necessary condition is “the number of columns in
A must equal the number of rows in B.”
Algorithm:
Algorithm A5: addMatrix(a[][], b[][], c[][]))
1. Start
2. for I = 0 to n-1 Repeat 3 to 4
3. for j = 0 to n-1 Repeat 4
4. c[i][j] = a[i][j] + b[i][j]
5. Stop
4
LAB # 02
Algorithm A7: mulMatrix(a[][], b[][], c[][]))
1. Start
2. For i = 0 to n-1 Repeat 3 to 6
3. For j = 0 to n-1 Repeat 4 to 6
4. C[i, j] == 0
5. for k = 0 to n-1 Repeat 6
6. c[i][j] = c[i][j] + a[i][k]*b[k][j]
Stop
5
LAB # 02
LAB TASKS
Q3.
a) Let A be an array of size n≥2 containing integers from 1 to n−1 inclusive, one of which is
repeated. Write a program for finding the integer in A that is repeated.
b) Let B be an array of size n≥6 containing integers from 1 to n−5 inclusive, five of which are
repeated. Write a program for finding the five integers in B that are repeated.
Submission Guidelines
• Write your codes in different notepad (.java) files.
• Place all files in a folder named your roll number e.g. cs162001.
• Compress all files into one (.zip) file named same as your roll number.
• Upload it on LMS
• -100 policies for plagiarism
6
LAB # 02