DSA Lab 05 SPRING 2022 SE
DSA Lab 05 SPRING 2022 SE
Name : _________________________________________
Roll No. : _________________ Section : ________________
Date : _________________________________________
Remarks : _________________________________________
Signature : _________________________________________
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
Experiment No. 05
Lab 05 – Working with Advance applications of Arrays, operations on arrays using
functions in JAVA.
Introduction
In previous lab we have done with the foundations of array and its concepts. In this lab we use the
advance concepts and its applications.
method_name (myarray);
The above call passes the reference to the array myarray to the method ‘method_name’. Thus, the changes
made to myarray inside the method will reflect in the calling method as well.
Unlike in C/C++, you need not pass the length parameter along with array to the method as all Java arrays
have a property ‘length’. However, it might be advisable to pass several elements in case only a few
positions in the array are filled.
Example 5.1: Program demonstrates the passing of an array as a parameter to the function.
In the above program, an array is initialized in the main function. Then the method printArray is called to
which this array is passed as an argument. In the printArray method, the array is traversed and each element
is printed using the enhanced for loop.
Let us take another example of passing arrays to methods. In this example, we have implemented two
classes. One class contains the calling method main while the other class contains the method to find the
maximum element in the array.
So, the main method calls the method in another class by passing the array to this method find_max. The
find_max method calculates the maximum element of the input array and returns it to the calling function.
Example 5.2: Program that has function find_max() which can find the max value from the given array.
class maxClass{
public int find_max(int [] myarray) {
int max_val = 0;
//traverse the array to compare each element with max_val
for(int i=0; i<myarray.length; i++ ) {
if(myarray[i]>max_val) {
max_val = myarray[i];
}
}
//return max_val
return max_val;
}
}
public class Main
{
public static void main(String args[]) {
//input array
int[] myArray = {43,54,23,65,78,85,88,92,10};
System.out.println("Input Array:" + Arrays.toString(myArray));
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 2
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
While returning a reference to an array from a method, you should keep in mind that:
1. The data type that returns value should be specified as the array of the appropriate data type.
2. The returned value from a method is the reference to the array.
3. The array is returned from a method in the cases where you need to return multiple values of the
same type from a method. This approach becomes useful as Java doesn’t allow returning multiple
values.
import java.util.*;
public class Main
{
public static String[] return_Array() {
//define string array
String[] ret_Array = {"Java", "C++", "Python", "Ruby", "C"};
//return string array
return ret_Array;
}
}
}
Output:
The above program is an example of returning an array reference from a method. The ‘return_array’ method
is declared an array of strings ‘ret_Array’ and then simply returns it. In the main method, the return value
from the return_array method is assigned to the string array and then displayed.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 3
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
Example 5.4: Program that has an array having random elements, using a function.
// Create an array
int[] random_numbers;
Output:
The following program provides another example of returning an array from a method. Here, we use an
integer array that is used to store the computed random numbers and then this array is returned to the caller.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 4
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
System.out.println(intArrayString);
// [1, 2, 3, 4, 5]
String[] stringArray = { "a", "b", "c", "d", "e"
};
Create an ArrayList from an ArrayList<String> arrayList = new
3 ArrayList<String>(Arrays.asList(stringArray));
array
System.out.println(arrayList);
// [a, b, c, d, e]
String[] stringArray = { "a", "b", "c", "d", "e"
};
Check if an array contains a boolean b =
4
certain value Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
5 Concatenate two arrays // Apache Commons Lang library
int[] combinedIntArray =
ArrayUtils.addAll(intArray, intArray2);
6 Declare an array inline method(new String[]{"a", "b", "c", "d", "e"});
//containing the provided list of elements
//Apache common lang
Joins the elements of the String j = StringUtils.join(new String[] { "a",
7 provided array into a single "b", "c" }, ", ");
String System.out.println(j);
//a, b, c
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray,
3);//create a new array
System.out.println(Arrays.toString(removed));
One more - convert int to byte array
11 Remove element of an array byte[] bytes =
ByteBuffer.allocate(4).putInt(8).array();
In this case, an array is given and the task is to find the certain element from the array. If the desired element
found in the array it will return the position otherwise it will return that the key has not found.
Example 5.5: Program that can help in searching a key element from an unsorted array.
class Main
{
// Function to implement search operation
static int findElement(int arr[], int n,int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 6
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
return -1;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {12, 34, 10, 6, 40};
int n = arr.length;
if (position == - 1)
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1));
}
}
Output:
class Main
{
// Function to insert a given key in the array. This function returns n+1
// if insertion is successful, else n.
static int insertSorted(int arr[], int n, int key, int capacity)
{
arr[n] = key;
return (n + 1);
}
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 7
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
// Driver Code
public static void main (String[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
// Inserting key
n = insertSorted(arr, n, key, capacity);
Output:
class Main
{
// function to search a key to
// be deleted
static int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 8
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
return -1;
}
if (pos == -1)
{
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Driver Code
public static void main(String args[])
{
int i;
int arr[] = {10, 50, 30, 40, 20};
int n = arr.length;
int key = 30;
n = deleteElement(arr, n, key);
Output:
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 9
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
class Main {
// function to implement
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
/* Driver Code*/
public static void main(String[] args)
{
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = arr.length - 1;
key = 10;
Output:
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 10
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
class Main {
// Inserts a key in arr[] of given capacity. n is current size of arr[].
// This function returns n+1 if insertion is successful, else n.
static int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capacity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
// Inserting key
n = insertSorted(arr, n, key, capacity);
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 11
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
class Main {
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
if (pos == -1) {
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
/* Driver Code */
public static void main(String[] args)
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = arr.length;
int key = 30;
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 12
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
n = deleteElement(arr, n, key);
Output:
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 13
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
What is mask?
Mask is also a signal. It can be represented by a two dimensional matrix. The mask is usually of the order of 1x1,
3x3, 5x5, 7x7. A mask should always be in odd number, because otherwise you cannot find the mid of the mask.
Why do we need to find the mid of the mask? The answer lies below, in topic of, how to perform convolution?
Example of convolution
Let’s perform some convolution. Step 1 is to flip the mask.
Mask
Let’s take our mask to be this.
1 2 3
4 5 6
7 8 9
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 14
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
3 2 1
6 5 4
9 8 7
9 8 7
6 5 4
3 2 1
Image
Let’s consider an image to be like this
2 4 6
8 10 12
14 16 18
Convolution
Convolving mask over image. It is done in this way. Place the center of the mask at each element of an image.
Multiply the corresponding elements and then add them, and paste the result onto the element of the image on which
you place the center of mask.
The box in red color is the mask, and the values in the orange are the values of the mask. The black color box and
values belong to the image. Now for the first pixel of the image, the value will be calculated as
First pixel = (5*2) + (4*4) + (2*8) + (1*10)
= 10 + 16 + 16 + 10 = 52
Place 52 in the original image at the first index and repeat this procedure for each pixel of the image.
Video Tutorial Reference:
https://www.youtube.com/watch?v=2OW2aLFaV8A
https://www.youtube.com/watch?v=1GUgD2SBl9A
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 15
Algorithms
Lab 05 – Working with Advance applications of Arrays, operations on
April 20, 2022 arrays using functions in JAVA.
Now for each of the student the marks will be displayed as histogram
Name: Nida
*
* * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
English Urdu Maths Science ICT
5. A University wants you to design a grade book system for their students. The system will generate the result
of course with at least 10 students. Each student will give 3 tests and their average will be recorded.
Furthermore, it will show the lowest marks and highest marks. At the end it will show that overall grade
distribution of the class. For your help the analyst and given you the following output so you can do the
reverse engineering and find how you can use arrays and get the project solved.
Instructor: Asst. Prof. Syed Faisal Ali CS-211 | Data Structures & 16
Algorithms