Question

In: Computer Science

Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...

Question 2 (Function Template)

Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key.

Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>).

Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>).

Write a main() function that test your function by passing parameters of type int, float, char, Student, and Book. Identify and implement additional suitable operators needed to be overloaded in the Student and Book class that you define above so that it can work well with the template binary search function that you have written. A Student object should be searched using the student id and the Book object should be searched using its title.

Solutions

Expert Solution

Code:

Code as text:

#include <iostream>

using namespace std;

// Template of iterative binary search

template<class T> int binarySearch(T arr[], int size, T target) {

int low = 0;

int high = size - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (target < arr[mid])

high = mid - 1;

else if (target > arr[mid])

low = mid + 1;

else

return mid;

}

return -1;

}

// a class for student

class Student {

private:

int id;

string name;

char grade;

public:

Student(int _id, string _name, char _grade) {

id = _id;

name = _name;

grade = _grade;

}

friend ostream &operator<<(ostream &out, Student &S) {

out << "Student id: " << S.id << "\nName: " << S.name << "\nGrade: " << S.grade;

return out;

}

friend istream &operator>>(istream &input, Student &S) {

input >> S.id >> S.name >> S.grade;

return input;

}

bool operator<(Student &S) {

if (id < S.id)

return true;

else

return false;

}

bool operator>(Student &S) {

if (id > S.id)

return true;

else

return false;

}

bool operator<=(Student &S) {

if (id <= S.id)

return true;

else

return false;

}

};

// a class for book

class Book {

private:

string title;

string author;

float price;

public:

Book(string _title, string _author, float _price) {

title = _title;

author = _author;

price = _price;

}

friend ostream &operator<<(ostream &out, Book &B) {

out << "Book Title: " << B.title << "\nAuthor: " << B.author << "\nPrice: " << B.price;

return out;

}

friend istream &operator>>(istream &input, Book &B) {

input >> B.title >> B.author >> B.price;

return input;

}

bool operator<(Book &B) {

if (title < B.title)

return true;

else

return false;

}

bool operator>(Book &B) {

if (title > B.title)

return true;

else

return false;

}

bool operator<=(Book &B) {

if (title <= B.title)

return true;

else

return false;

}

};

int main() {

int pos; // to store position of search element

int size; // to store size of array

// test int array

cout << "Testing on type: int" << endl;

int intArr[] = {1, 3, 4, 6, 7, 9, 13, 15};

size = sizeof(intArr) / sizeof(*intArr);

int targetInt = 7;

pos = binarySearch(intArr, size, targetInt);

cout << "Target found at position: " << pos << endl;

cout << endl;

// test float array

cout << "Testing on type: float" << endl;

float floatArr[] = {23.0, 56.7, 69.9, 142.8, 299.3};

size = sizeof(floatArr) / sizeof(*floatArr);

float targetfloat = 69.9;

pos = binarySearch(floatArr, size, targetfloat);

cout << "Target found at position: " << pos << endl;

cout << endl;

// test char array

cout << "Testing on type: char" << endl;

char charArr[] = {'A', 'D', 'E', 'H', 'L', 'P', 'S', 'W', 'Z'};

size = sizeof(charArr) / sizeof(*charArr);

char targetchar = 'L';

pos = binarySearch(charArr, size, targetchar);

cout << "Target found at position: " << pos << endl;

cout << endl;

// test student array

cout << "Testing on type: Student" << endl;

Student studArr[] = {{1, "AJ", 'A'}, {2, "DK", 'F'}, {3, "AS", 'C'}};

size = sizeof(studArr) / sizeof(*studArr);

Student targetStud(2, "DK", 'F');

pos = binarySearch(studArr, size, targetStud);

cout << "Target found at position: " << pos << endl;

cout << endl;

// test book array

cout << "Testing on type: Book" << endl;

Book bookArr[] = {{"XYZ", "ABC", 32.0}, {"HSJ", "NMJ", 50.2}};

size = sizeof(bookArr) / sizeof(*bookArr);

Book targetBook("XYZ", "ABC", 32.0);

pos = binarySearch(bookArr, size, targetBook);

cout << "Target found at position: " << pos << endl;

return 0;

}

Sample run:

P.s. Ask any doubts in comments and don't forget to rate the answer.


Related Solutions

Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write test programs to convince someone that you did them right. DIRECTIONS: You have been provided: 1. Function stubs for searching and sorting algorithms: Utility functions: swap, shift right/left, show array, insertion point. Searching: linear seach, binary search Sorting: bubble sort, insertion sort, selection sort. 2. Main program providing a pattern for testing youour functions, including: - sample arrays, each providing a different test case....
In the recursive version of binary search each function call reduces the search size by half....
In the recursive version of binary search each function call reduces the search size by half. Thus in the worst case for a sorted list of length n. The algorithm makes log n calls. Is it possible to make fewer than log n calls? Group of answer choices 1) Yes, depends on when it finds the element it is looking for 2) No, it will always make log n calls.
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT