Question

C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to...

C++ Assignment on Search and Sort for Chapter 8
Problem:
Modify the student grade problem to include the following:

•   Write a Search function to search by student score
•   Write a Search function to search by student last name
•   Write a Sort function to sort the list by student score
•   Write a Sort function to sort the list by student last name
•   Write a menu function that lets user to choose any action he/she want to do.

Make sure your program has option to test the program as many times as the user wants.

Student Grade Problem below:

#include<iostream>
#include<iomanip>
#include<stdio.h>

using namespace std;

// Function to calculate grade
char calcGrade(float n)
{
   if (n>=9)
       return 'A';
   if (n>=8 && n<9)
       return 'B';
   if (n>=7 && n<8)
       return 'C';
   if (n>=6 && n<7)
       return 'D';
   else
       return 'F';
}

int main()
{
   // A structure variable
   struct student {
       string name;
       float test1, test2, lab, quiz, final, total;
       char grade;
   };

   // N is the number of students
   int n;
   cout << "Enter the number of students\n";
   cin >> n;
   student s[n];

   // taking the input for all the students
   for (int i = 0;i<n;++i)
   {
       // This is used to get the last enter key pressed while input so that it doesnt mess with the name input
       getchar();
       cout << "\nEnter the name of the stundent\n";
       getline(cin, s[i].name);
       cout << "Enter the grades of the student in order of test1,test2,lab,quiz,final. (Please press ENTER after each grade)\n";
       cin >> s[i].test1 >> s[i].test2 >> s[i].lab >> s[i].quiz >> s[i].final;
       // Mulitply the final result by 0.1 because the grades are out of 10 and not out of 100
       s[i].total = 0.1*(s[i].test1*0.2 + s[i].test2*0.2 + s[i].lab*0.15 + s[i].quiz*0.15 + s[i].final*0.3);
       s[i].grade = calcGrade(s[i].total);
   }

   // printing the output
   cout << fixed << setprecision(2);
   cout << left << setw(30) << "\nStudent Name" << setw(8) << "Test1" << setw(8) << "Test2" << setw(8) << "Lab" << setw(8) << "Quiz" << setw(8) << "Final" << setw(8) << "Total" << setw(8) << "Grade";
   cout << endl << left << setw(30) << "============" << setw(8) << "=====" << setw(8) << "=====" << setw(8) << "===" << setw(8) << "====" << setw(8) << "=====" << setw(8) << "=====" << setw(8) << "=====" << endl;
   for (int i = 0;i<n;++i)
   {
       cout << left << setw(30) << s[i].name << setw(8) << s[i].test1 << setw(8) << s[i].test2 << setw(8) << s[i].lab << setw(8) << s[i].quiz << setw(8) << s[i].final << setw(8) << s[i].total << setw(8) << s[i].grade << endl;
   }

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

/*
Program to get student details and do some operations
*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
// A structure variable for student
struct student {
   string name;
   float test1, test2, lab, quiz, final, total;
   char grade;
};
//Function prototypes
void seachByScore(const student[], const int);
void display(const student[], const int);
void seachByLastName(const student[], const int);
void sortByScore(student[], const int);
void sortByLastName(student[], const int);
int printMenu();
char calcGrade(float);

int main()
{
   // N is the number of students
   int n;
   student *s;
   cout << "Enter the number of students\n";
   cin >> n;
   //Allocate space for array
   s=new student[n];

   // taking the input for all the students
   for (int i = 0; i < n; ++i)
   {
       // This is used to get the last enter key pressed while input so that it doesnt mess with the name input
       getchar();
       cout << "\nEnter the name of the stundent\n";
       getline(cin, s[i].name);
       cout << "Enter the grades of the student in order of test1,test2,lab,quiz,final. (Please press ENTER after each grade)\n";
       cin >> s[i].test1 >> s[i].test2 >> s[i].lab >> s[i].quiz >> s[i].final;
       // Mulitply the final result by 0.1 because the grades are out of 10 and not out of 100
       s[i].total = 0.1*(s[i].test1*0.2 + s[i].test2*0.2 + s[i].lab*0.15 + s[i].quiz*0.15 + s[i].final*0.3);
       s[i].grade = calcGrade(s[i].total);
   }
   //Loop until user choose exit
   int opt = printMenu();
   while (opt != 6) {
       //Search by score
       if (opt == 1) {
           seachByScore(s, n);
       }
       //Search by last name
       else if (opt == 2) {
           seachByLastName(s, n);
       }
       //Sort by score
       else if (opt == 3) {
           sortByScore(s, n);
       }
       //Sort by last name
       else if (opt == 4) {
           sortByLastName(s, n);
       }
       //Display student info
       else if (opt == 5) {
           cout << "\nDisplay student: \n";
           display(s, n);
       }
       cout << endl;
       opt = printMenu();
   }
   cout << "\nGood BYE\n";
   return 0;
}
/*
Prompt for student total and compare it with all students tota
Then display matching scored students
*/
void seachByScore(const student s[], const int sz) {
   float score;
   bool found = false;
   cout << "\nEnter score of the student: ";
   cin >> score;
   for (int i = 0; i < sz; i++) {
       if (s[i].total == score) {
           cout << left << setw(30) << s[i].name << setw(8) << s[i].test1 << setw(8) << s[i].test2 << setw(8) << s[i].lab << setw(8) << s[i].quiz << setw(8) << s[i].final << setw(8) << s[i].total << setw(8) << s[i].grade << endl;
           found = true;
       }
   }
   if (!found) {
       cout << "\nNot found!!!\n";
   }
}
/*
Prompt for student last name and compare it with all student's last name
Then display matching students
*/
void seachByLastName(const student s[], const int sz) {
   string name;
   bool found = false;
   cout << "\nEnter last name of the student: ";
   cin >> name;
   for (int i = 0; i < sz; i++) {
       if (s[i].name.substr(s[i].name.find(' ')+1) == name) {
           cout << left << setw(30) << s[i].name << setw(8) << s[i].test1 << setw(8) << s[i].test2 << setw(8) << s[i].lab << setw(8) << s[i].quiz << setw(8) << s[i].final << setw(8) << s[i].total << setw(8) << s[i].grade << endl;
           found = true;
       }
   }
   if (!found) {
       cout << "\nNot found!!!\n";
   }
}
/*
Using bubble sort to compare each students total and sort in ascending order
*/
void sortByScore(student s[], const int sz) {
   for (int i = 0; i < sz; i++) {
       for (int j = i + 1; j < sz; j++) {
           if (s[i].total > s[j].total) {
               float t = s[i].total;
               s[i].total = s[j].total;
               s[j].total = t;
           }
       }
   }
   cout << "\nStudent after sort by score: \n";
   display(s, sz);
}
/*
Using bubble sort to compare each students last name and sort in ascending order
*/
void sortByLastName(student s[], const int sz) {
   for (int i = 0; i < sz; i++) {
       for (int j = i + 1; j < sz; j++) {
           if (s[i].name.substr(s[i].name.find(' ')+1) > s[j].name.substr(s[j].name.find_first_of(' '))) {
               student t = s[i];
               s[i] = s[j];
               s[j] = t;
           }
       }
   }
   cout << "\nStudent after sort by last name: \n";
   display(s, sz);
}
//Display menu
//Check error in user choice return correct choice
int printMenu() {
   int opt;
   cout << "OPTIONS\n1. Search by score\n2. Search by last name\n"
       << "3. Sort by score\n4. Sort by last name\n5. Display students\n6. Exit\n";
   cout << "Enter choice: ";
   cin >> opt;
   while (opt < 1 || opt > 6) {
       cout << "Error!!!Option must be 1-5.Please try again...\n";
       cout << "Enter choice: ";
       cin >> opt;
   }
   return opt;
}
//Print formatted students details
void display(const student s[], const int sz) {
   // printing the output
   cout << fixed << setprecision(2);
   cout << left << setw(30) << "\nStudent Name" << setw(8) << "Test1" << setw(8) << "Test2" << setw(8) << "Lab" << setw(8) << "Quiz" << setw(8) << "Final" << setw(8) << "Total" << setw(8) << "Grade";
   cout << endl << left << setw(30) << "============" << setw(8) << "=====" << setw(8) << "=====" << setw(8) << "===" << setw(8) << "====" << setw(8) << "=====" << setw(8) << "=====" << setw(8) << "=====" << endl;
   for (int i = 0; i < sz; ++i)
   {
       cout << left << setw(30) << s[i].name << setw(8) << s[i].test1 << setw(8) << s[i].test2 << setw(8) << s[i].lab << setw(8) << s[i].quiz << setw(8) << s[i].final << setw(8) << s[i].total << setw(8) << s[i].grade << endl;
   }
}
/*
Function to calculate grade of a student
Take total score as input and return letter grade
*/
char calcGrade(float n)
{
   if (n >= 9)
       return 'A';
   if (n >= 8 && n < 9)
       return 'B';
   if (n >= 7 && n < 8)
       return 'C';
   if (n >= 6 && n < 7)
       return 'D';
   else
       return 'F';
}

------------------------------------------------

output

Enter the number of students
3

Enter the name of the stundent
Student 1
Enter the grades of the student in order of test1,test2,lab,quiz,final. (Please press ENTER after each grade)
87 85 96 74 72

Enter the name of the stundent
Student 2
Enter the grades of the student in order of test1,test2,lab,quiz,final. (Please press ENTER after each grade)
78 74 95 86 82

Enter the name of the stundent
Student 3
Enter the grades of the student in order of test1,test2,lab,quiz,final. (Please press ENTER after each grade)
96 97 94 92 90
OPTIONS
1. Search by score
2. Search by last name
3. Sort by score
4. Sort by last name
5. Display students
6. Exit
Enter choice: 5

Display student:

Student Name                 Test1   Test2   Lab     Quiz    Final   Total   Grade
============                  =====   =====   ===     ====    =====   =====   =====
Student 1                     87.00   85.00   96.00   74.00   72.00   8.15    B
Student 2                     78.00   74.00   95.00   86.00   82.00   8.22    B
Student 3                     96.00   97.00   94.00   92.00   90.00   9.35    A

OPTIONS
1. Search by score
2. Search by last name
3. Sort by score
4. Sort by last name
5. Display students
6. Exit
Enter choice: 1

Enter score of the student: 8.15
Student 1                     87.00   85.00   96.00   74.00   72.00   8.15    B

OPTIONS
1. Search by score
2. Search by last name
3. Sort by score
4. Sort by last name
5. Display students
6. Exit
Enter choice: 2

Enter last name of the student: 1
Student 1                     87.00   85.00   96.00   74.00   72.00   8.15    B

OPTIONS
1. Search by score
2. Search by last name
3. Sort by score
4. Sort by last name
5. Display students
6. Exit
Enter choice: 3

Student after sort by score:

Student Name                 Test1   Test2   Lab     Quiz    Final   Total   Grade
============                  =====   =====   ===     ====    =====   =====   =====
Student 1                     87.00   85.00   96.00   74.00   72.00   8.15    B
Student 2                     78.00   74.00   95.00   86.00   82.00   8.22    B
Student 3                     96.00   97.00   94.00   92.00   90.00   9.35    A

OPTIONS
1. Search by score
2. Search by last name
3. Sort by score
4. Sort by last name
5. Display students
6. Exit
Enter choice: 4

Student after sort by last name:

Student Name                 Test1   Test2   Lab     Quiz    Final   Total   Grade
============                  =====   =====   ===     ====    =====   =====   =====
Student 3                     96.00   97.00   94.00   92.00   90.00   9.35    A
Student 2                     78.00   74.00   95.00   86.00   82.00   8.22    B
Student 1                     87.00   85.00   96.00   74.00   72.00   8.15    B

OPTIONS
1. Search by score
2. Search by last name
3. Sort by score
4. Sort by last name
5. Display students
6. Exit
Enter choice: 6

Good BYE

Add a comment
Know the answer?
Add Answer to:
C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • This is my code for a final GPA calculator. For this assignment, I'm not supposed to...

    This is my code for a final GPA calculator. For this assignment, I'm not supposed to use global variables. My question is: does my code contain a global variable/ global function, and if so how can I re-write it to not contain one? /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Title: Final GPA Calculator * Course Computational Problem Solving CPET-121 * Developer: Elliot Tindall * Date: Feb 3, 2020 * Description: This code takes grades that are input by the student and displays * their...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able...

    Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able to modify the code so it will accept name such as "john kenny " and the progrom should also ask for the student id number. #include<iostream> #include<stdlib.h> using namespace std; struct st { int roll; char name[50]; char grade; struct...

  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score...

    Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score average and Letter grade. No scores less than 0 or greater than 100 should be accepted #include<iostream> #include<string> using namespace std; int main() { double average[5]; string name[5]; char grade[5]; for(int i=0; i<=5; i++) { cout<<"Enter student name: "; cin.ignore(); getline(cin,name[i]); int sum=0; for(int j=0; j<5; j++) { int grades; cout<<"Enter Grades:"<<j+1<<" : "; cin>>grades; sum+=grades; } average[i]=(float)sum/5; if(average[i]>=90 && average[i]<=100) grade[i]='A'; else if(average[i]>=80...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT