(1) sorts an array of strings in ascending order using a sort algorithm (sortLis
ID: 3741384 • Letter: #
Question
(1) sorts an array of strings in ascending order using a sort algorithm (sortList function) (2) Displays the list, one name per line (displayList function) (3) Prompts the user for a first name. Then ask the user for a last name. Return the concatenation of the lastname and first name separated by a comma (format: lastName, firstName). (getName function) (4) searches an array of strings for the name entered using a binary search algorithm. If the name is in the array then display the message "Found". Otherwise, display the message "Not found" (searchList function)
The driver program has been provided. Your task is to add the needed code, as per the comments in the program, and correctly declare and define the required functions to make the program work.
Do not make any changes to the main function definition for any reason.
// Include needed header files here.
// Include needed function prototypes here
int main() //Do NOT modify the contents of the main driver
{
const int SIZE = 20;
string name[SIZE] = {"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"
};
sortList(name, SIZE);
displayList(name, SIZE);
string userInput = getName();
searchList(name, userInput, SIZE);
return 0;
}
//Include needed function definitions here
Explanation / Answer
#include <iostream>
using namespace std;
// Include needed function prototypes here
void sortList(string name[], int SIZE);
void displayList(string name[],int SIZE);
string getName();
int searchList(string name[],string userInput,int SIZE);
int main() //Do NOT modify the contents of the main driver
{
const int SIZE = 20;
string name[SIZE] = {"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"
};
sortList(name, SIZE);
displayList(name, SIZE);
string userInput = getName();
int index = searchList(name, userInput, SIZE);
if(index!=-1) {
cout<<"Found"<<endl;
} else {
cout<<"Not found"<<endl;
}
return 0;
}
int searchList(string name[], string userInput,int SIZE) {
int start = 0;
int end = SIZE- 1;
while (start <= end) {
int mid = (start + end) / 2;
if (userInput == name[mid]) {
return mid;
}
if (userInput < name[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
string getName() {
string f,l;
cout<<"Enter the first name: "<<endl;
cin >> f;
cout<<"Enter the last name: "<<endl;
cin >> l;
return l+", "+f;
}
void displayList(string name[],int SIZE) {
for(int i=0;i<SIZE;i++) {
cout<<name[i]<<endl;
}
}
void sortList(string name[],int SIZE) {
string temp;
for (int i = 0; i < SIZE; i++) {
int j = 1;
while (j < (SIZE - i)) {
if (name[j - 1] > name[j]) {
// swap the elements!
temp = name[j - 1];
name[j - 1] = name[j];
name[j] = temp;
}
j++;
}
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.