USING C++ Goal: a program to manage student records You are asked to implement a
ID: 3703247 • Letter: U
Question
USING C++
Goal: a program to manage student records
You are asked to implement a program to manage Student records of the following form:
Number of students will be specified at runtime:
The number of students to be stored in the system will be specified at runtime via commandline arguments. The program will take 1 argument whose value will indicate the number of students to stored in the system. The following call, for example, will store 77students in the system.
Student records will be initialized with random values
Names, numbers and grades will be set to random values with the following constraints:
A name is a random string (a-zA-Z0-9) between 6 and 12 characters long;
A number is a random integer between 201100000 and 201600000; and
A grade is a random integer between 70 and 100.
Note that each student stores only 5 grades.
Required functionality
The program will store students in std::vector and will implement the following functionality:
Print student records;
Print student records sorted by name (ascending order);
Print student records sorted by average grade (ascending order); and
Print the student record with the highest average grade (and print average values).
Average values may be floats.
Some example code to get you started
Consider the code shown below, which showcases the use of sort algorithm available in STL.
Explanation / Answer
Please find my implementation:
Please let me know in case of any issue.
File Name: student.cpp
//Include the needed files
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//create class
class Student
{
//Access specifier
private:
//declare variable to hold name
std::string name_;
//Declare variable to hold number
int number_;
//Declare vector to store grades
std::vector<int> grades_;
//Declare variable to store number of courses student has taken
const int num_courses_;
// You need to implement the following four methods
//Define method to generate name randomly
static std::string gen_name()
{
//Create string
char *tp="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Create string
string name = "";
//Generate length for the name
int sz = rand()%6+6;
//Loop to generate name randomly with sz characters
for(int kk=0;kk<sz;kk++)
{
//Generate random index
int rno = rand()%63+0;
//Add the character at the random index in tp
name += tp[rno];
}
//Return the random name
return name;
}
//Define method to generate a number randomly
static int gen_number()
{
//Generate random number
return rand()%500000 + 201100000;
}
//Define method to generate grade randomly
static int gen_grade()
{
//Generate grade randomly
return rand()%30 + 70;
}
//Access specifier
public:
//Constructor
Student() : name_(gen_name()), number_(gen_number()), num_courses_(5)
{
//Loop to fill the grades
for (int i=0; i<num_courses_; ++i)
{
//Call function
grades_.push_back(gen_grade() );
}
}
//Define method to print student name and number
friend std::ostream& operator<<(std::ostream& os, const Student& s) {
//Print name and number
os << "Name = " << s.name_ << ", Number = " << s.number_;
return os;
}
//Define method to print the grades
void print_grades(std::ostream& os) const
{
//Loop to print all the grades
for (int i=0; i<num_courses_; ++i)
{
//Print grade
os << grades_[i] << ", ";
}
}
//Define method to compute the average grade for the student
double compute_average()
{
//Define variable to store the average
double avg = 0;
//define variable to store grades sum
int sum = 0;
//Loop
for(int kk=0;kk<num_courses_;kk++)
{
//Find sum
sum += grades_.at(kk);
}
//Find average
avg = (static_cast<double> (sum) / static_cast<double> (5));
//return average
return avg;
}
//define method to get the name
string getName()
{
//Return name
return name_;
}
//Define method to get the number
int getNumber()
{
//Return the number
return number_;
}
//Define method to copy the values
void operator= (const Student& s1)
{
//Copy the namr
name_ = s1.name_;
//Copy the number
number_=s1.number_;
//clear the grades vector
grades_.clear();
//Loop to copy the grades from s1
for(int kk=0;kk<5;kk++)
{
//Copy the current grade
grades_.push_back(s1.grades_.at(kk));
}
}
};
File Name: main.cpp
//Include the needed files
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include "student.cpp"
using namespace std;
//Define method to check the student name alphabetically
bool sortByName(Student s1, Student s2)
{
//If student 1 name < student 2 name
if(s1.getName().compare(s2.getName())<0)
//return True
return true;
//return False
return false;
}
//Define method to sort the student name by average grade
bool sortByAverageGrade(Student s1, Student s2)
{
//if student1 average grade < student2 average grade
if(s1.compute_average()<s2.compute_average())
//Return True
return true;
//Return False
return false;
}
//main
int main(int argc, char *argv[])
{
//Create vector to store the student records
vector<Student> vecStud;
//check command line arguments
if (argc!=2)
{
//If number of students is not given, print error message
cout<<"No arguments supplied";
//Pause window
system("pause");
//Exit
exit(-1);
}
//Parse number of students
int nStud = atoi(argv[1]);
//Create students
for(int kk=0;kk<nStud; kk++)
{
//Create student
Student s1;
//Add student to the vector
vecStud.push_back(s1);
}
//print message
cout<<" ****************************************************"<<endl;
cout<<"Printing Student records:"<<endl;
cout<<" ****************************************************"<<endl;
//Loop to print all student records
for(int kk=0;kk<nStud; kk++)
{
//Get current student
Student s1 = vecStud.at(kk);
//Print name and number
cout<<s1;
//Print message
cout<<" Grades:"<<endl;
//Call function to print grades
s1.print_grades(cout);
//Print a line
cout<<" ------------------------"<<endl;
}
//Print message
cout<<" ****************************************************";
cout<<" Printing Student records sorted by name:"<<endl;
cout<<" ****************************************************"<<endl;
//Sort records by name
std::sort(vecStud.begin(), vecStud.end(), sortByName);
//Loop to print all records
for(int kk=0;kk<nStud; kk++)
{
//Get current student
Student s1 = vecStud.at(kk);
//Print name and number
cout<<s1;
//Print message
cout<<" Grades:"<<endl;
//Call function to print grades
s1.print_grades(cout);
//Print a line
cout<<" ------------------------"<<endl;
}
//Print message
cout<<" ****************************************************";
cout<<" Printing Student records sorted by average grade:"<<endl;
cout<<" ****************************************************"<<endl;
//Call function to sort by average grade
std::sort(vecStud.begin(), vecStud.end(), sortByAverageGrade);
//Loop
for(int kk=0;kk<nStud; kk++)
{
//Get current student
Student s1 = vecStud.at(kk);
//Print name and number
cout<<s1;
//Print message
cout<<" Grades:"<<endl;
//Call function to print grades
s1.print_grades(cout);
//Print average grade
cout<<" Average Grade:"<<s1.compute_average()<<endl;
//Print a line
cout<<" ------------------------"<<endl;
}
//Print message
cout<<" ****************************************************";
cout<<" Printing Student record with highest average grade:"<<endl;
cout<<" ****************************************************"<<endl;
//Get the student with the highest grade
Student s1 = vecStud.at(nStud - 1);
//Print the name and number
cout<<s1;
//Print grades
cout<<" Grades:"<<endl;
//call function
s1.print_grades(cout);
//Print average grade
cout<<" Average Grade:"<<s1.compute_average()<<endl;
//Print a line
cout<<" ------------------------"<<endl;
//Pause window
system("pause");
//Stop the program
return 0;
}
Please DONT forgot to rate my answer. We are working hard for you Guys!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.