Question

In: Computer Science

Please do not reply to this question by copy pasting the code that someone wrote for...

Please do not reply to this question by copy pasting the code that someone wrote for this question because it is incorrect. The code written before for this messes up two vectors. Please make sure you write a new code without that error.

8.10 Project 4 - Help me Sort My Roster

About

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
    • functions which have /*Provide Implementation*/ are for you to edit.
    • 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
    • Note: If two elements are equal do not swap the elements
  • SortByFirstName(vector &students, vector &grades)
    • Sorts students and grades by the students first name
  • SortByLastName(vector &students, vector &grades)
    • Sorts students and grades by the students last name
  • SortByGrade(vector &students, vector &grades)
    • Sorts students and grades by the students grade
  • 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*/

Unlike past assignments which test matching output this assignment tests output along with testing your functions directly using unit tests. The unit tests will use your functions with hardcoded vectors, if the tests fails the unit tests will print out what it expected compared to what your function did when sorting. If you have any questions please reach out via email, discussion board, or during office hours.

#include <iostream>
#include <vector>
#include <string>
#include <locale>
#include <iomanip>
using namespace std;

void PrintWelcome();
void GetNumberOfStudents(int &numOfStudents);
void PrintMenu();
char GetUserSelection();
void PrintSummary(const vector<string> &students, const vector<double> &grades);
void AddStudent(vector<string> &students, vector<double> &grades);
void RemoveStudent(vector<string> &students, vector<double> &grades);
void SortByFirstName(vector<string> &students, vector<double> &grades);
void SortByLastName(vector<string> &students, vector<double> &grades);
void SortByGrade(vector<string> &students, vector<double> &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<string> students;
vector<double> 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; i++){
AddStudent(students, grades);
}
//Print thank you message
cout << thankYou;
//Print the Roster Menu
PrintMenu();
//Get the users selection
selection = GetUserSelection();

while(selection != QUIT){
if(selection == ADD){
AddStudent(students, grades);
}
else if(selection == REMOVE){
RemoveStudent(students, grades);
}
else if(selection == PRINT){
PrintSummary(students, grades);
}
else if(selection == MENU){
PrintMenu();
}
/*Provide Implementation for other menu options*/
else{
cout << notValid;
}
selection = GetUserSelection();
}

}

void PrintWelcome(){
string welcome = "Welcome to the student roster!\n";
cout << welcome;
}

void GetNumberOfStudents(int &numOfStudents){
string numOfStudentsQuestion = "How many students are in your class?:\n";
cout << numOfStudentsQuestion;
cin >> 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<string> &students, const vector<double> &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<string> &students, vector<double> &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<string> &students, vector<double> &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<string> &students, vector<double> &grades){
/*Provide Implementation*/
}
void SortByLastName(vector<string> &students, vector<double> &grades){
/*Provide Implementation*/
}
void SortByGrade(vector<string> &students, vector<double> &grades){
/*Provide Implementation*/
}

Solutions

Expert Solution

Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code.  Please get back to me if you need any change in code. Else please upvote

CODE:

#include <iostream>

#include <vector>

#include <string>

#include <locale>

#include <iomanip>

using namespace std;

void PrintWelcome();

void GetNumberOfStudents(int &numOfStudents);

void PrintMenu();

char GetUserSelection();

void PrintSummary(const vector<string> &students, const vector<double> &grades);

void AddStudent(vector<string> &students, vector<double> &grades);

void RemoveStudent(vector<string> &students, vector<double> &grades);

void SortByFirstName(vector<string> &students, vector<double> &grades);

void SortByLastName(vector<string> &students, vector<double> &grades);

void SortByGrade(vector<string> &students, vector<double> &grades);

int main() {

    const char QUIT = 'q';

    const char ADD = 'a';

    const char REMOVE = 'r';

    const char PRINT = 'p';

    const char MENU = 'm';

    const char SORT_FNAME = 'f';

    const char SORT_LNAME = 'l';

    const char SORT_GRADE = 'g';

   

    string thankYou = "Thank you for entering your students information!\n";

    string notValid = "Not a valid selection.\n";

    char selection;

   

    int numOfStudents;

   

    vector<string> students;

    vector<double> 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; i++){

        AddStudent(students, grades);

    }

   

    //Print thank you message

    cout << thankYou;

   

    //Print the Roster Menu

    PrintMenu();

   

    //Get the users selection

    selection = GetUserSelection();

   

    while(selection != QUIT){

        if(selection == ADD){

            AddStudent(students, grades);

        }

        else if(selection == REMOVE){

            RemoveStudent(students, grades);

        }

        else if(selection == PRINT){

            PrintSummary(students, grades);

        }

        else if(selection == MENU){

            PrintMenu();

        }

        else if(selection == SORT_FNAME){

            SortByFirstName(students, grades);

        }

        else if(selection == SORT_LNAME){

            SortByLastName(students, grades);

        }

        else if(selection == SORT_GRADE){

            SortByGrade(students, grades);

        }

        else{

            cout << notValid;

        }

        selection = GetUserSelection();

    }

}

void PrintWelcome(){

    string welcome = "Welcome to the student roster!\n";

    cout << welcome;

}

void GetNumberOfStudents(int &numOfStudents){

    string numOfStudentsQuestion = "How many students are in your class?:\n";

    cout << numOfStudentsQuestion;

    cin >> 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<string> &students, const vector<double> &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<string> &students, vector<double> &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<string> &students, vector<double> &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<string> &students, vector<double> &grades){

    int vecsize = students.size();

    for (int j = 0; j < vecsize - 1; ++j) {

   

        int min = j;

        for (int i = j+1; i < vecsize; ++i) {

            if (students.at(min) > students.at(i)) {

                min = i;

            }

   

        }

        if (min != j){

            string temp = students.at(j);

            students.at(j) = students.at(min);

            students.at(min) = temp;

           

            double temp1 = grades.at(j);

            grades.at(j) = grades.at(min);

            grades.at(min) = temp1;

        }

    }

    cout <<"Student data sorted by first name.\n";

}

void SortByLastName(vector<string> &students, vector<double> &grades){

    int vecsize = students.size();

    for (int j = 0; j < vecsize - 1; ++j) {

   

        int min = j;

        for (int i = j+1; i < vecsize; ++i) {

           

            string min_lname = students.at(min).substr(students.at(min).find(' ')+1, students.at(min).length());

            string i_lname = students.at(i).substr(students.at(i).find(' ')+1, students.at(min).length());

           

            if (min_lname > i_lname) {

                min = i;

            }

   

        }

        if (min != j){

            string temp = students.at(j);

            students.at(j) = students.at(min);

            students.at(min) = temp;

           

            double temp1 = grades.at(j);

            grades.at(j) = grades.at(min);

            grades.at(min) = temp1;

        }

    }

     cout <<"Student data sorted by last name.\n";

}

void SortByGrade(vector<string> &students, vector<double> &grades){

    int vecsize = grades.size();

    for (int j = 0; j < vecsize - 1; ++j) {

   

        int min = j;

        for (int i = j+1; i < vecsize; ++i) {

            if (grades.at(min) > grades.at(i)) {

                min = i;

            }

   

        }

        if (min != j){

           

            double temp1 = grades.at(j);

            grades.at(j) = grades.at(min);

            grades.at(min) = temp1;

           

            string temp = students.at(j);

            students.at(j) = students.at(min);

            students.at(min) = temp;

           

           

        }

    }

     cout <<"Student data sorted by grades.\n";

}

OUTPUT:


Related Solutions

PLEASE DO NOT USE OTHER ALREADY GIVEN ANSWERS TO ANSWER THIS BY COPY PASTING. Suppose you...
PLEASE DO NOT USE OTHER ALREADY GIVEN ANSWERS TO ANSWER THIS BY COPY PASTING. Suppose you are the economic advisor of a fictitious president who lives in a world with only one time period. Consumers and firms in this economy are similar to the consumers and firms studied. Assume that the substitution effect is stronger than the income effect. The government can only collect revenue through lump-sum taxes. Suppose you are in a meeting and the president tells you the...
Please answer to the point and accurate and please do not copy someone else.. Please don't...
Please answer to the point and accurate and please do not copy someone else.. Please don't go for length, 200-300 words will be fine but please very accurate and very careful. I will be very much thankful. I am preperation for exam and it is tough for me. 1. Explain why companies do not consolidate all subsidiaries
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1. List the following functions according to their order of growth from the lowest to the highest: (n−2)!, 5lg(n+100)10, 22n, 0.001n4 +3n3 +1, ln2 n, √3 n, 3n. 2. The range of afinite nonempty set of...
Can someone please show me how to do the work on this please. Question The transactions...
Can someone please show me how to do the work on this please. Question The transactions of the Fury Delivery Service are recorded in the general journal below. Instructions: 1. Post the journal entries to the attached general ledger “T” accounts. 2. Prepare a trial balance on the form provided after the “T” accounts. General Journal Date Account Titles and Explanation Debit Credit 2017 Sept. 1 Cash Common Stock (Stockholders invested cash in business) 25,000 25,000 4 Equipment Cash Notes...
I do not understand this question could someone please answer and give meaning to this question....
I do not understand this question could someone please answer and give meaning to this question. MHC6305 Finanical Management. Compare and contrast cash accounting methodology and accrual accounting methodology in order to illustrate how each best works for different types of companies.
Note: Plagiarism is strictly prohibited please do not copy from internet please . Question 01: Explain...
Note: Plagiarism is strictly prohibited please do not copy from internet please . Question 01: Explain with examples what is the positive and nominative Economics (150 to 200 words) . Note: Plagiarism is strictly prohibited please do not copy from internet please
PLEASE DO NOT COPY AND PASTE PREVIOUS ANSWERED QUESTION. Working effectively and efficiently as part of...
PLEASE DO NOT COPY AND PASTE PREVIOUS ANSWERED QUESTION. Working effectively and efficiently as part of a team can be challenging. But when done properly, teamwork offers many benefits as different people bring their experience, education, knowledge, and skills to resolve a problem. The ability to work with others and collaborate in teams is an important skill in the world today. It is likely that, as a business professional, you will be called upon to work as a valuable member...
Here is the discussion Question. The reply is below the discussion question. I need a reply...
Here is the discussion Question. The reply is below the discussion question. I need a reply to the reply below.(I need an elaborate and comprehensive answer) Do you think markets, in general, are “self-regulating” at least in the US today ? if so why and if not why not? [2] As the prices of good and services begin to increase, shifting to its alternatives is possibly to occur in consumption. However, potential issues may arise from waiting for current prices...
**New code needed! Please do not reference code that has already been answered for this question...
**New code needed! Please do not reference code that has already been answered for this question as that code contains errors*** Write a C++ program to simulate a service desk. This service desk should be able to service customers that can have one of three different priorities (high, medium, and low). The duration for any customer is a random number (between 5 minutes and 8 minutes). You need to write a program that will do the following: Generate random 100...
Please do this question in R and show the code too, please. The alternating current (AC)...
Please do this question in R and show the code too, please. The alternating current (AC) breakdown voltage of an insulating liquid indicates its dielectric strength. The article “Testing Practices for the AC Breakdown Voltage Testing of Insulation Liquids” (IEEE Electrical Insulation Magazine, 1995: 21–26) gave the accompanying sample observations on breakdown voltage (kV) of a particular circuit under certain conditions. 62 50 53 57 41 53 55 61 59 64 50 53 64 62 50 68 54 55 57...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT