0% found this document useful (0 votes)
12 views6 pages

Dsa Lab 02-2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Dsa Lab 02-2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DHA SUFFA UNIVERSITY

Department of Computer Science


CS-201L Data Structures & Algorithms
Fall 2024
LAB 02

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

Algorithm A3: delete(LA , K)


1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J•1] = LA[J]
5. Set J = J+1
6. Set N = N•1
7. Stop

Algorithm A4: search(LA , ITEM)


1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
Stop

Task (to be performed during lab session):


Implement the algorithms A1-A4 in Java with test cases of your own choice.

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.

Initializing and Accessing Two-Dimensional Arrays

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

Algorithm A6: subMatrix(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

Task (to be performed during lab session):


Implement the algorithms A5-A7 in Java with test cases of your own choice.

5
LAB # 02
LAB TASKS

Q1. Write a program for: insert(LA, K, ITEM)


a) K is not out of range
b) Item must be positive integer
c) Check user's entered ITEM at index K is greater or less than in an array at index K, if less than
insert it into the array otherwise not perform any operation, and prints the message 'ITEM’ at
index k is greater than user's entered ‘ITEM'.

Q2. Write a program for: search(LA,ITEM) in Java


a) Search whether the user's entered item is exist or not in the array if exist perform following
b) Print two right neighbors(item/value) in case no right neighbor print message 'no right neighbor'
c) Print two left neighbors(item/value) in case no left neighbor print message 'no left neighbor'

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.

TWO DIMENSIONAL ARRAYS

Q1. Write a program that finds the transpose of a 2D matrix.


Q2. Write functions for square matrix to calculate
1. Left diagonal sum
2. Right diagonal sum

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

You might also like