Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS212T: DATA STRUCTURE TERM: 2nd TERM(2022)

LAB: 03

General notes:
● Each exercise is a separate project and should be saved in a
different folder. The name of the folder is the exercise number.
● Write your name, ID, and PC number as a comment on the main
class.
● Zip your folders into one folder and upload your answer to the
blackboard before the end of the class. Late submission is
considered absent.
Recursion
Exercise 1:

Find Minimum Value In Array Using Recursion

Use recursion method to find the minimum value in an array of numbers.

Output:
The given array: [43,54,87,12,6,93]
Minimum value in array is : 6

Exercise 2:
Reverse String
Write a method with name ReversString which reverse a string using recursion.
(This done by starting from the last character in the string then recursively print each
character until all the characters in the string are exhausted).
Example : String "Data" becomes "atad"
Binary Search
Exercise 1:

given a sorted array of n elements, search for given key element using binary
search algorithm.

Output sample:
The given array: [4,6,12,16,22]
Element 16 is found at index 3

Exercise 2:
Write a Java program which finds the number of occurrences of an
element x in a sorted array A using binary search.

1- Ask the user to enter the array elements.


2- Ask the user to enter the target to search for occurrences.
3- Write a function: BinarySearch (int[] nums, int target, boolean searchFirst)
to find the first or last occurrence of a given number in a sorted integer array. If
(searchFirst) is true, return the first occurrence of the number; otherwise,
return its last occurrence.
Hint: First occurrence of key 3 occurs at index_a = (1) and the last occurrence of 3
occurs index_b= (4) ,and the number of occurrence is the difference between
index_a and index_b.
Exercise 3:

A. Write a class Student which contains the following data and methods

Data:
• Name (string)
• ID (integer)
• Level (integer)
• GPA (double)
Methods:
• Get and set for each data item
• Print all students

B. Main class contain :


• BinarySum Method:
Binary recursion method which receives an array of students, and calculate the
sum of the GPA of all students then returns the sum.
• Main Method:
- Create an array of 4 students then assign GPA values for each student (no need
to read information from user).
- print GPA values for each student.
- Compute the GPA sum of all students using method BinarySum method
- Compute the average GPA of all students

You might also like