Question

write a c++ program that conducts a binary search using a template in: a set of...

write a c++ program that conducts a binary search using a template in: a set of values of type int, a set of values of type float, a set of values of type double and a set of values of type char.

Assume that the array may have up to 10 values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Source code of the above program is given below*/

/*NOTE: If you want to change something or want in a different way, please let me know through comments; I will surely revert back to you.*/

#include<iostream>

using namespace std;

template <class T>

int binarySearch(T *a, T val, int n)

{

int start=0,end=n-1;

int mid;

while(start<=end)

{

mid=(start+end)/2;

if(val>a[mid])

start=mid+1;

else if(val<a[mid])

end=mid-1;

else

return mid;

}

return -1;

}

  

int main()

{

//binary search algorithm works in sorted array

int iArr[10] = {20,40,55,62,67,91,123,146,617,826};

double dArr[10]= {21.4, 50.153,64.14, 74.45, 75.7,89.54,91.44, 103.22, 116.33,189.9};

char cArr[10] = {'a','e','s','t','u','v','w','x','y','z'};

int ii;

double dd;

char cc;

//testing for integer array

cout<<"\nElements of Integer Array \n";

for(int i=0;i<10;i++)

{

cout<<" "<<iArr[i]<<" ";

}

cout<<"\n\nEnter an integer to be searched: ";

cin>>ii;

cout<<"\nUsing Binary Search method\n";

int index=binarySearch(iArr,ii,10);

if(index==-1)

cout<<"Item does not found!"<<endl;

else

cout<<"Item found in index: "<<index<<endl;

//testing for double array

cout<<"\nElements of double Array \n";

for(int i=0;i<10;i++)

{

cout<<" "<<dArr[i]<<" ";

}

cout<<"\n\nEnter an double to be searched: ";

cin>>dd;

cout<<"\nUsing Binary Search method\n";

index=binarySearch(dArr,dd,10);

if(index==-1)

cout<<"Item does not found!"<<endl;

else

cout<<"Item found in index: "<<index<<endl;

//testing for character array

cout<<"\nElements of character Array \n";

for(int i=0;i<10;i++)

{

cout<<" "<<cArr[i]<<" ";

}

cout<<"\n\nEnter an character to be searched: ";

cin>>cc;

cout<<"\nUsing Binary Search method\n";

index=binarySearch(cArr,cc,10);

if(index==-1)

cout<<"Item does not found!"<<endl;

else

cout<<"Item found in index: "<<index<<endl;

system("pause");

return 0;

}

/***********************OUTPUT*********************************/

Elements of Integer Array 20 40 55 62 67 91 123 146 617 826 nter an integer to be searched: 62 sing Binary Search method Item

/*Hope this helps. Thanks.*/

Add a comment
Know the answer?
Add Answer to:
write a c++ program that conducts a binary search using a template in: a set of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • 6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search...

    6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search for a target character in the array and return the index location if found or -1 if it is not found. 7 a5 points 17 points cach) Write the code to create a GUI based class Temperature Converter which inherits from JFrame and implements the ActionListerner interface. public static int binary Search(char target, char( theValues, int firstIndex, int lastindex) Example: Clicked "F to C"...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • using c++ Write a C++ program which reads three values of types char, int, double and...

    using c++ Write a C++ program which reads three values of types char, int, double and string from the keyboard and primis, appropriately formatted, assigned values and variable types. For example, if letter, number, and real are variables of type char, int and double respectively, and if the values assigned to them using cin function are: a, 1, and 3.1415, then the output should be: a is a character 1 is an integen 3.1415 is a real number

  • in c++ Class Assignment Nov-1: Write the binary search in template form.

    in c++ Class Assignment Nov-1: Write the binary search in template form.

  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT