Question

Write and test a function in C++ that uses the binary search algorithm to search an...

Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array:

string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",

        "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",

        "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",

        "James, Jean", "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",

        "Javens, Renee", "Harrison, Rose", "Setzer, Cathy",

        "Pike, Gordon", "Holland, Beth" };

/* Sample output:

To search the 20-element array, enter a name: Jones, January

Name "Jones, January" not found in array

Search again? Y/N: y

To search the 20-element array, enter a name: Pore, Bob

Name "Pore, Bob" found at index 11

Search again? Y/N: y

To search the 20-element array, enter a name: Looney, Joe

Name "Looney, Joe" found at index 9

Search again? Y/N: y

To search the 20-element array, enter a name: Weaver, Jim

Name "Weaver, Jim" found at index 18

Search again? Y/N: n                                   */

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

#include<bits/stdc++.h>
using namespace std;

int binSearch(string names[], string name, int n)
{
   int l = 0, e = n-1, mid;
   while(l<=e)
   {
       mid = (l+e)/2;
       if(names[mid] == name)
           return mid;
       else if(names[mid] > name)
           e = mid-1;
       else
           l = mid+1;
   }
   return -1;
}

int main()
{
   string names[] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",

"Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",

"Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",

"James, Jean", "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",

"Javens, Renee", "Harrison, Rose", "Setzer, Cathy",

"Pike, Gordon", "Holland, Beth" };
int n = sizeof(names)/sizeof(names[0]);
sort(names, names+n);
  
string s;
char c;
int ind;
  
do{
   cout<<"To search the 20-element array, enter a name: ";
   fflush(stdin);
   getline(cin, s);
   ind = binSearch(names, s, n);
   if(ind == -1)
       cout<<"Name "<<'"'<<s<<'"'<<" not found in array"<<endl;
   else
       cout<<"Name "<<'"'<<s<<'"'<<" found at index "<<ind<<endl;
   cout<<"Search again? Y/N: ";
   cin>>c;
   }while(c == 'y' || c == 'Y');
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write and test a function in C++ that uses the binary search algorithm to search an...
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
  • Write a main function that declares the names, marks, number of elements as well as the...

    Write a main function that declares the names, marks, number of elements as well as the value to be searched for and the index of the returned function calls. Read and display the contents of names and marks. Ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student. Sort the arrays...

  • C++ with Pseudocode in the beginning This assignment will require you to write a program that...

    C++ with Pseudocode in the beginning This assignment will require you to write a program that will create an array of 10 string objects. The array will be initialized with the strings which contain the person’s name and phone number in one string. The following is an example of test data: “Renee Javens, 678-1223”, “Joe Looney, 586-0097”, “Geri Palmer, 223-8787”, “Lynn Presnell, 887-1212”, “Bill Wolfe, 223-8878”, “Sam Wiggins, 486-0998”, “Bob Kain, 586-8712”, “Tim Haynes, 586-7676”, “John Johnson, 223-9037”, “Jean James,...

  • SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names...

    SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names as follows: {"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",                         "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",                         "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",                         "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",                   "Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"}; Write a program to sort and display the names in alphabet order (use selection sort). The program prompts...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • Write a program that has an array of at most 50 strings that hold people’s names...

    Write a program that has an array of at most 50 strings that hold people’s names and phone numbers. You can assume each string’s length is no more than 40. You may make up your own strings, or use the following: "Becky Warren, 555-1223" "Joe Looney, 555-0097" "Geri Palmer, 555-8787" "Lynn Presnell, 555-1212" "Holly Gaddis, 555-8878" "Sam Wiggins, 555-0998" "Bob Kain, 555-8712" "Tim Haynes, 555-7676" "Warren Gaddis, 555-9037" "Jean James, 555-4939" "Ron Palmer, 555-2783" The program should ask the user...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Show the steps of the binary search algorithm (pseudocode is given below); low = index of...

    Show the steps of the binary search algorithm (pseudocode is given below); low = index of first item in list high = index of last item in list while low is less than or equal to high mid = index halfway between low and high if item is less than list[mid] high = mid - 1 else if item is greater than list[mid] low = mid + 1 else return mid end while return not found For each step of...

  • 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...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this...

    Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this linear search algorithm returns the indices of the longest sorted subset of numbers in an array of integers of size n. The longest sorted subset of numbers must satisfy three conditions. First, the subset consists of unique numbers (no duplicate); second, all the numbers in this subset is divisible by m, the minimum size of the subset is two elements. In the main method...

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