### Bubble sort function for last names, in alphabetical order For this project I am struggling to create my function that sorts a list of full names by last names. I know that I will need to use another bubble sort as I have done for the previous functions, but I am not too sure how to compare only the last names. Please help and explain.

Here is the code:
Where it says provide implementation is where I am stuck on

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223`` ``````#include #include #include #include #include using namespace std; void PrintWelcome(); void GetNumberOfStudents(int &numOfStudents); void PrintMenu(); char GetUserSelection(); void PrintSummary(const vector &students, const vector &grades); void AddStudent(vector &students, vector &grades); void RemoveStudent(vector &students, vector &grades); void SortByFirstName(vector &students, vector &grades); void SortByLastName(vector &students, vector &grades); void SortByGrade(vector &students, vector &grades); int main() { const char QUIT = 'q'; const char ADD = 'a'; const char REMOVE = 'r'; const char PRINT = 'p'; const char MENU = 'm'; string thankYou = "Thank you for entering your students information!\n"; string notValid = "Not a valid selection.\n"; char selection; int numOfStudents; vector students; vector grades; //Print the Welcome Message PrintWelcome(); //Get Number of Students GetNumberOfStudents(numOfStudents); //Add the total number of students to the student and grades vectors for(int i = 0; i > numOfStudents; } void PrintMenu(){ string menu = "Please choose one of the following options:\n" "a: add a student\n" "r: remove a student\n" "p: print the class summary\n" "m: print menu\n" "f: sort - first name\n" "l: sort - last name\n" "g: sort - grade\n" "q: quit program\n"; cout << menu; } char GetUserSelection(){ string selectionString = "selection:\n"; char selection; cout << selectionString; cin >> selection; return selection; } void PrintSummary(const vector &students, const vector &grades){ string summaryHeading = "Class Summary\n" "------------------------\n"; string nameGradeHeading = "Name Grade\n" "--------- --------\n"; string numOfStudentsString = "Number of Students:\n" "-------------------\n"; string averageGradeString = "Average Grade:\n" "--------------\n"; double sum = 0.0; double average = 0.0; int numOfStudents = students.size(); cout << endl; cout << summaryHeading << nameGradeHeading; for(int i = 0; i < students.size(); i++){ sum += grades.at(i); cout << left << setw(20) << students.at(i) << setprecision(2) << fixed << grades.at(i) << endl; } cout << numOfStudentsString << numOfStudents << endl; cout << averageGradeString << setprecision(2) << fixed << sum/numOfStudents << endl; cout << endl; } void AddStudent(vector &students, vector &grades){ string studentInfo = "Please enter student (First Last Grade) info:\n"; string firstName, lastName; double grade; cout << studentInfo; cin >> firstName >> lastName >> grade; students.push_back(firstName + " " + lastName); grades.push_back(grade); } void RemoveStudent(vector &students, vector &grades){ string removeStudent = "Please enter students first and last name"; string firstName, lastName; cout << removeStudent; cin >> firstName >> lastName; string fullName = firstName + " " + lastName; for(int i = 0; i < students.size(); i++){ if(students.at(i) == fullName) { students.erase(students.begin() + i); grades.erase(grades.begin() + i); cout << "Removing: " << fullName; } } } void SortByFirstName(vector &students, vector &grades){ cout << "Sorting by first name" << endl; int i, j; string temp; int smallestIndex; double temp_grade; for(i = 0; i < students.size(); ++i) { smallestIndex = i; for(j = i + 1; j < students.size()-1; ++j) { if (students.at(j) < students.at(smallestIndex)) { smallestIndex = j; } } temp_grade = grades.at(i); grades.at(i) = grades.at(smallestIndex); grades.at(smallestIndex) = temp_grade; temp = students.at(i); students.at(i) = students.at(smallestIndex); students.at(smallestIndex) = temp; } } void SortByLastName(vector &students, vector &grades){ cout << "Sorting by last name" << endl; /*Provide Implementation*/ } void SortByGrade(vector &students, vector &grades){ cout << "Sorting by grade" << endl; int i, j, smallestIndex; double temp; string temp_student; for(i = 0; i < students.size(); ++i) { smallestIndex = i; for(j = i + 1; j < students.size()-1; ++j) { if(grades.at(j) < grades.at(smallestIndex)) { smallestIndex = j; } } temp = grades.at(i); grades.at(i) = grades.at(smallestIndex); grades.at(smallestIndex) = temp; temp_student = students.at(i); students.at(i) = students.at(smallestIndex); students.at(smallestIndex) = temp_student; } }``````

this is the project info:

The class roster program is working really well. However, it would be nice to be able to sort students based on first name, last name, or their grade. I would like a program where I can choose how my roster is sorted and then print out the class summary

Specification

I have provided a template main.cpp for you to edit
You only have to add code to the areas that say /*Provide Implementation*/. You will find this in three functions, and in the selection handling portion of main
Note: You are not allowed to change any of the function signature (ie. add or edit parameter names and types, or change the function name or its return type)
Note: For sorting you must implement a Selection Sort
Video Explanation Selection Sort
Note: If two elements are equal do not swap the elements
SortByFirstName(vector &students, vector &grades) - option f
Sorts students and grades by the students first name
SortByLastName(vector &students, vector &grades) - option l (lowercase L)
Sorts students and grades by the students last name
You will also need to add code which calls the functions if the user selects to sort using that sort by function
You can find it in the code by searching for /*Provide Implementation for other menu options*/ > Note: You are not allowed to change any of the function signature
Your teacher seems to be teaching you frustration programming.
Showing you how to do it badly before hopefully showing you how to do it well.
C++ has much to offer without these silly blind alley diversions.

> I know that I will need to use another bubble sort
But you're told to (and already provided with an example and a video) of selection sort.
So use the same algorithm given in SortByFirstName.

> students.push_back(firstName + " " + lastName);

Because where you do this to compare first names
if (students.at(j) < students.at(smallestIndex))

Every time you want to compare a last name, you have to do something like
string lastname = students.at(j).substr(students.at(j).find(" ")+1);

That is, you have to get each lastname out of the whole name before you can compare last names. So I tried something like this however its still not working, do you possibly spot any errors?

 ``12345678910111213141516171819202122232425262728293031`` ``````void SortByLastName(vector &students, vector &grades) { cout << "Sorting by last name" << endl; int i, j; string temp; string lastNamej; string lastNamei; double temp_grade; for(i = 0; i < students.size(); ++i) { lastNamei = students.at(i).substr(students.at(i).find(' ')+1); for(j = i + 1; j < students.size()-1; ++j) { lastNamej = students.at(j).substr(students.at(j).find(' ')+1); if (lastNamej < lastNamei) { temp_grade = grades.at(i); grades.at(i) = grades.at(j); grades.at(j) = temp_grade; temp = lastNamei; lastNamei = lastNamej; lastNamej = temp; } } } }`````` ``1234567891011121314151617181920212223`` ``````void SortByLastName(vector &students, vector &grades) { for (size_t i = 0; i < students.size(); ++i) { string lastNamei = students.at(i).substr(students.at(i).find(' ')+1); for (size_t j = i + 1; j < students.size(); ++j) { string lastNamej = students.at(j).substr(students.at(j).find(' ')+1); if (lastNamej < lastNamei) { double temp_grade = grades.at(i); grades.at(i) = grades.at(j); grades.at(j) = temp_grade; string temp = students.at(i); students.at(i) = students.at(j); students.at(j) = temp; lastNamei = lastNamej; } } } }`````` Thank you both for your help it worked @dutch, and @salem c, your explanation was helpful. I figured there are much simpler ways to sort through c++ What's with all the .at() ? As the loops are from 0 to < .size(), then using [] is OK as the subscript won't be out of bounds.

Also, there's std::swap() to swap 2 items.
Topic archived. No new replies allowed.