Question: (i need code and d i s c u s s i o n), you will analyze examples of search and sort routines in C++
(i need code and d i s c u s s i o n), you will analyze examples of search and sort routines in C++ programs. Describe when, why, and how we use search and sort routines in programming.
Instructions
- To get started, search the Web to find an example of a program that uses a sorting algorithm.
- In your initial post, describe how it works to sort data, when we use it, and why. Which sorting algorithm do you think you will use in your Capstone project? my project sample student management system #include #include #include
using namespace std;
struct Student { string name; int studentNumber; string email; string DOB; double GPA; };
void addStudent(vector& students) { Student newStudent; cout << "Enter student number: "; cin >> newStudent.studentNumber;
cout << "Enter name: "; cin >> newStudent.name;
cout << "Enter email: "; cin >> newStudent.email;
cout << "Enter date of birth: "; cin >> newStudent.DOB;
cout << "Enter GPA: "; cin >> newStudent.GPA; students.push_back(newStudent);
cout << "Student added successfully!" << endl; }
void deleteStudent(vector& students) { int studentNumber; cout << "Enter student number to delete: "; cin >> studentNumber; bool found = false; for (auto it = students.begin(); it != students.end(); it++) { if (it->studentNumber == studentNumber) { students.erase(it); found = true; break; } } if (found) { cout << "Student deleted successfully!" << endl; } else { cout << "Student not found!" << endl; } }
void updateStudent(vector& students) { int studentNumber; cout << "Enter student number to update: "; cin >> studentNumber; bool found = false; for (auto& student : students) { if (student.studentNumber == studentNumber) { cout << "Enter new name: "; getline(cin, student.name); cout << "Enter new email: "; getline(cin, student.email); cout << "Enter new date of birth: "; getline(cin, student.DOB); cout << "Enter new GPA: "; cin >> student.GPA; found = true; break; } } if (found) { cout << "Student updated successfully!" << endl; } else { cout << "Student not found!" << endl; } }
void showAllStudents(const vector& students) { for (const auto& student : students) { cout << "Name: " << student.name << endl; cout << "Student number: " << student.studentNumber << endl; cout << "Email: " << student.email << endl; cout << "Date of birth: " << student.DOB << endl; cout << "GPA: " << student.GPA << endl; } }
int main() { vector students; char input; do { cout << "Menu:" << endl; cout << "A - Add student" << endl; cout << "D - Delete student" << endl; cout << "U - Update student" << endl; cout << "S - Show all students" << endl; cout << "Q - Quit" << endl; cout << "Enter your choice: "; cin >> input; switch (input) { case 'A': addStudent(students); break; case 'D': deleteStudent(students); break; case 'U': updateStudent(students); break; case 'S': showAllStudents(students); break; case 'Q': cout << "Quitting program..." << endl; break; default: cout << "Invalid choice, please try again!" << endl; break; } } while (input != 'Q'); return 0; }