Question: In C++ Modify this program so that it prints the names of the 5 students in alphabetical order. Do not write another program. Just modify
In C++
Modify this program so that it prints the names of the 5 students in alphabetical order. Do not write another program. Just modify here
#include
#include
#include
#include
using namespace std;
// Define a structure for each node in the list
struct Node {
int ID;
char name[50];
int year;
};
// Define a comparison function for sorting by year
bool compareByYear(const Node &a, const Node &b) {
return a.year < b.year; ////// MODIFY THIS FUNCTION SO THAT IT SORTS IN ALPHABETICAL ORDER THE 5 NAMES
}
int main() {
// Declare a list of nodes
list
// Insert data into the list
Node el;
el.ID = 1;
strcpy(el.name, "Mary Smith");
el.year = 3;
TheList.push_back(el);
el.ID = 2;
strcpy(el.name, "Sophie Jones");
el.year = 1;
TheList.push_back(el);
el.ID = 3;
strcpy(el.name, "Mary Williams");
el.year = 4;
TheList.push_back(el);
el.ID = 4;
strcpy(el.name, "Anne Brown");
el.year = 2;
TheList.push_back(el);
el.ID = 5;
strcpy(el.name, "Joana Taylor");
el.year = 5;
TheList.push_back(el);
// Sort the list by year using the STL sort() function and the compareByYear() function
TheList.sort(compareByYear);
// Print the sorted list
cout << "ID\tName\t\tYear of Study ";
for (auto it = TheList.begin(); it != TheList.end(); it++) {
cout << it->ID << "\t" << it->name << "\t\t" << it->year << " ";
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
