Question

Must be written in C++ Display results and comments Write a program that sorts a vector...

Must be written in C++

Display results and comments

Write a program that sorts a vector of names alphabetically using the selection sort and then
searches the vector for a specific name using binary search. To do that, you need to write three
functions:


1. void selSort (vector <string> & v): sorts the vector using selection sort
2. void display (const vector <string> & v): displays the vector contents
3. int binSearch (const vector <string> & v, string key): searches the vector for a key,
returns the index of the key if found and -1 if the index is not found


Your main program should instantiate a vector of strings, names, and initialize it to the names
specified below. Then call the functions in the order specified by the comments.


int main()
{
// Vector of string initialized to specific names
vector<string> names {“Joe Garcia”, “Amelia Perez”, “Bob
Haynes”, “Tim Ducrest”, “Kevin Garcia”, “Suzy Walter”, “Fang
Yi”, “Robert James”, “Mary Andres”};


//Call the function display to print the vector contents
//Call the selSort function to sort the vector in ascending order
// Call the function display to print the sorted vector contents
// Call the binSearch function to search for “Kevin Garcia “
// Call the binSearch function to search for “Joe James“
return 0;

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

#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

void selSort(vector<string>& v)
{
for (int pass=0; pass<v.size()-1; pass++) {

int potentialSmallest = pass;
for (int i=pass+1; i<v.size(); i++) {

if (v[i] < v[potentialSmallest]) {

potentialSmallest = i;

}

}

string temp = v[pass];

v[pass] = v[potentialSmallest];

v[potentialSmallest] = temp;

}

}



void display (const vector <string> & v){
    for(int i=0;i<v.size();i++){
        cout<<v[i]<<" ";
   }
   cout<<"\n"<<endl;
}


int binSearch (const vector <string> & arr, string x){
int l = 0,n=arr.size() ;
int r = n - 1;
while (l < r)
{
int m = l + (r - l) / 2;
  
int res;
if (x==(arr[m]))
res = 0;
  
if (res == 0)
return m;
  
if (x>arr[m])
l = m + 1;
  

else
r = m - 1;
}
  
return -1;
}

int main()
{
string arr[]={"Joe Garcia", "Amelia Perez", "Bob Haynes", "Tim Ducrest", "Kevin Garcia", "Suzy Walter", "Fang Yi", "Robert James", "Mary Andres"
};
vector<string> names(arr,arr + sizeof(arr) / sizeof(string));
display(names);

selSort(names);

display(names);

int t=binSearch(names,"Kevin Garcia");
cout<< t <<endl;
t=binSearch(names,"Joe Garcia");
cout<<t<<endl;

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Must be written in C++ Display results and comments Write a program that sorts a vector...
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
  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

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

  • Write a C++ program to fill a vector with 5000 random numbers. Then display: The smallest...

    Write a C++ program to fill a vector with 5000 random numbers. Then display: The smallest number The largest number The number of odd values The number of even values The total of the values The average of the values Ask the user for an integer and display the subscript of the cell where it was found or NOT FOUND! if it isn’t found. There is a file called "Activity 25 Sort functions.zip" under Materials in this week’s module. Download...

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

  • (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection...

    (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection Sort • Rotates the sorted array around a random point, e.g., 1 2 3 4 5 à 3 4 5 1 2 is obtained by rotation around 3. • Searches for a key point in the rotated array in O(logn) time, where the rotation point is known in advance. • Repeats above by first finding the unknown rotation point via sequential search and binary...

  • THIS IS FOR C++ PROGRAMMING USING VISUAL STUDIO THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING #inclu...

    THIS IS FOR C++ PROGRAMMING USING VISUAL STUDIO THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING #include "pch.h" #include #include using namespace std; // Function prototype void displayMessage(void); void totalFees(void); double calculateFees(int); double calculateFees(int bags) {    return bags * 30.0; } void displayMessage(void) {    cout << "This program calculates the total amount of checked bag fees." << endl; } void totalFees() {    double bags = 0;    cout << "Enter the amount of checked bags you have." << endl;    cout <<...

  • Please help!! I am supposed to write a program in C++ about student & grades. Needs...

    Please help!! I am supposed to write a program in C++ about student & grades. Needs to have two functions that sorts students letter grade, and another one to sort Students name in your student’s record project. Remember, to add necessary parameters and declaration in main to call each function properly. Order of functions call are as follow Read Data Find Total Find average Find letter grade Call display function Call sort letter grade function Call display function Call sort...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector...

    Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector with random numbers. Use merge sort to reorder the vector Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the...

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