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

  • In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...

    In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...

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

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

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

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

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