Data Structures & Algorithm Lab Manual
Data Structures & Algorithm Lab Manual
UNIVERSITY OF SINDH
KBSAS CAMPUS NAUSHAHRO FEOZE
DEPARTMENT OF INFORMATION TECHNOLOGY
PRACTICAL NOTEBOOK
CERTIF
ICATE
This is to certify that Mr. SHOAIB ALI of 2k23 BS (IT) class bearing Roll No. 19 has
carried out the necessary Project work as per course of studies Data Structures and
Algorithms for the 2nd Year (3rd Semester) as above in practical schedule of his
journal.
Table of Contents
Lab No.5……………………………………………………………………………………………………………………………….
Objective: Build 1
Objective: To become familiar with the downloading and installation of JDK and Net beans IDE.
Step 1: Open Google and type Java jdk in the search bar. Click the very first link then the new tab will
open.
Step 2: Click on the highlighted option and setup will start downloading.
Step 4: Here you can see that the backup of Java JDK has been downloaded, now you just have to
click on open and move to the next step.
Step 1: After opening backup, Click On Next. Step 3: The installation process begins.
Step 2: Set the destination where you want to Step 4: JDK has been installed. Now click on Close.
install the program. Click Next to continue the
process.
Step 1: Open the destination where JDK was Step 4: Go to Path Click on Edit. A dialogue
installed. box appears.
Step 1: Open Google and type Netbeans IDE in the search bar. Click the very first link then the new
tab will open.
Step 2: Click on the highlighted option and setup will start downloading.
Step 4: Click on the highlighted link the backup will start downloading.
Step 2: Agree to the terms and conditions of the Step 4: Click on Next to continue the process.
software.
Objective: Array
Definition:
Normally, an array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, the elements of
an array are stored in a contiguous memory location. It is a data structure where we store similar
elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is
stored on 1st index and so on.
Figure 1: Array
An array in Java works by allocating a contiguous block of memory to hold a fixed number of
elements of the same data type. Here is a brief overview of how it works:
i. Array Declaration: You declare an array by specifying its type and name, along with the size
in square brackets.
Syntax:
Here, the array can store 10 elements. We can also say that the size or length of the array is
10.
In Java, we can declare and allocate the memory of an array in one single statement. For
example,
ii. Array Initialization: In Java, we can initialize arrays during declaration. For example,
Syntax: at the time of declaration,
DataType[ ] arrayName = {value1, value2, value3, …};
Figure 1: Syntax
Here, we have created an array named age and initialized it with the values inside the curly
brackets.
In the Java array, each memory location is associated with a number. The number is known
as an array index. We can also initialize arrays in Java, using the index number.
Syntax (after declaration):
arrayName [index] = value;
For example,
Output:
In the above example, notice that we are using the index number to access each element of
the array.
We can use loops to access all the elements of the array at once.
iv. Array Length: You can find the length of an array using the length property:
Syntax:
int length =
numbers.length;
v. Iterating Through Array: In Java, we can also loop through each element of the array. For
example,
Example: Using For-Loop
Figure
Output:
In the above example, we are using the for Loop in Java to iterate through each element of
the array. Notice the expression inside the loop,
age.length
Here, we are using the length property of the array to get the size of the array.
We can also use the for-each loop to iterate through the elements of an array. For example,
Definition:
Task 1:
Output:
Task 2:
Output:
Output:
Task 4:
Output:
Definition:
A two-dimensional array, also known as 2D array, is a collection of data elements arranged in a grid-
like structure with rows and columns. Each element in the array is referred to as a cell and can be
accessed by its row and column indices/indexes. A multidimensional array is an array of arrays. Each
element of a multidimensional array is an array itself.
For Example,
int [ ] [ ] a = new int [3] [4];
Here we have a created a multidimensional array named a. It is a 2D array that can hold a maximum
of 12 elements,
Syntax:
Initialization:
Syntax:
Example:
Syntax:
// Declare an empty 2D array
int[][] array2D = new int[rows][cols]; // rows and cols represent the number
of rows and columns in the array
// Assign values to each element using nested loops
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array2D[i][j] =value ;
}
}
Example in Java:
Figure 1:
Output:
Figure 2: Output
In Java, we can access elements of a 2D array using row and column indices.
Example in Java:
Figure 1
Output:
Figure 2
Figure 1:
Output:
Figure 2:
Example in Java:
Figure 1
Figure 2
Here is a simple java program that represents a 2D Array and accesses all its elements using
nested loops:
Example:
Figure 1:
Output:
Figure 2:
Output:
Output:
Figure 1:
Output:
Figure 2:
Example:
Figure 1:
Output:
Figure 2:
Example:
Figure 1:
Output:
Figure 2: