Question

In: Computer Science

Please use C++ for Mac (XCode) In this program, a user can search for a name...

Please use C++ for Mac (XCode)

In this program, a user can search for a name within a list of names. Here are the steps the program will implement:

1. Reads the names from the file names.txt into a vector that stores strings.

2. Sorts the names in alphabetical order using selection sort.

3. The program then prompts the user to enter a name.

4. The program then uses binary search to see if the name is in the list.

5. If the name is in the list, the program prints: "The name you entered is in the list!"

6. If the name entered was not in the list, the program prints "The name you entered was not in the list."

Notes: You will need to change your swap function to swap strings, like this:

void swap(string& x, string& y)

{

string temp = x;

x = y;

y = temp;

}

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)

CODE-

//c++ code
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
//function to sort the vector of stirng usign selection sort
void selection_sort(vector<string> &names)
{
   int n = names.size();
   //iterate over the vector
   for (int j = 0; j < n - 1; ++j)
   {
       int min = j;
       //find the smallest element
       for (int i = j+1; i < n; ++i)
       {
            if (names.at(min) > names.at(i))
           {
            min = i;
            }

        }
       //swap it with current element
       if (min != j)
       swap(names.at(j), names.at(min));
   }
}
//function to swap two strings
void swap(string &x, string &y)
{
   string temp = x;
   x = y;
   y = temp;
}
//function to search a string using binary search
bool binary_search(vector<string> names, string name)
{
   int left = 0;
   int right = names.size() - 1;
   //iterate while left less than or equal to right
   while(left <= right)
   {
       //find the mid
       int mid = (left + right)/2;
       //if the name is equal to name at mid index
       if(names.at(mid) == name)
       {
           //string found
           return true;
       }
       //if the name at mid index less than give name search in the right side of mid
       else if(names.at(mid) < name)
       {
           left = mid + 1;
       }
       //otherwise search in the left of the mid
       else
       {
           right = mid - 1;
       }

   }
   //string not found
   return false;
}
int main()
{
   //open the file to read
   ifstream infile ("names.txt");
   vector<string> names;
   string line;
   //read the file line by line
   while(getline(infile, line))
   {
       names.push_back(line);
   }
   //call the function to sort the names
   selection_sort(names);
   string name;
   //prompt the user to enter a name
   cout<<"Enter a name:\t";
   cin>>name;
   //search for the name using binary search
   if(binary_search(names, name))
   {
       cout<<"The name you entered is in the list"<<endl;
   }
   else
   {
       cout<<"The name you entered is not in the list"<<endl;
   }

}

names.txt contains

james
marry
smith
arena
bob
alice
john
danyi


SCREENSHOT-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

C++ for Mac (Xcode) For this exercise, you will write a program that includes four function...
C++ for Mac (Xcode) For this exercise, you will write a program that includes four function definitions. You will also write a main() function that calls these four functions to demonstrate that they work as expected. The four functions are: 1. printExitMessage() : This function prints a message to the screen saying something like "Thanks for using this software. Goodbye." 2. getMin(): This function takes two float inputs and returns the lesser of the two float values. For example, if...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
C++ Change the program to take user input for first name and last name for five...
C++ Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name. // EmployeeStatic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> #include <string> using namespace std; class Employee { public:    Employee(const std::string&, const std::string&); // constructor    ~Employee(); // destructor    std::string getFirstName() const; // return first name    std::string getLastName() const; // return...
How do I run a lex program written with C on xcode?
How do I run a lex program written with C on xcode?
Write a program in c# that declare a variable and store user name jjohn in. Then,...
Write a program in c# that declare a variable and store user name jjohn in. Then, write a statement that will make your program display at the screen the content of that variable, followed by " I would like to know your height in centimeter. Please enter it:". Then, write a statement that will store the value entered by the user, allowing decimal numbers ie some precision, a fraction part. Finally, write statements (more than one can be needed) so...
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT