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

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...
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...
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.
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT