https://programming-workshop-2.github.io/course-material/labs/lab8-stl-vectors U
ID: 3808988 • Letter: H
Question
https://programming-workshop-2.github.io/course-material/labs/lab8-stl-vectors
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 $7$ students 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
#include <iostream>
#include <vector>
#include <ctime> // time()
#include <cstdlib> // srand(), rand()
#include <algorithm> // min_element(), max_element(), sort()
/**
* Class student for holding a single student's record.
*/
class Student
{
// These will not be accessible from else where
private:
// Holds the name of the student.
std::string name_;
// Roll no. of the student.
int number_;
// A vector to hold grades.
std::vector<int> grades_;
// Number of courses taken by the student.
const int num_courses_;
// Alphabets.
char alpha[63] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Returns a random number
int rand_gen(int min, int max);
// You need to implement the following four methods
std::string gen_name()
{
std::cout << alpha << std::endl;
int size = this->rand_gen(6, 12);
std::string rand_name = "";
for (int i = 0; i < size; ++i)
{
rand_name += this->alpha[this->rand_gen(0, 61)];
std::cout << rand_name;
}
return rand_name;
}
int gen_number()
{
// Produce a random number between 201100000
// and 201600000
return this->rand_gen(201100000, 201600000);
}
int gen_grade()
{
for (int i = 0; i < num_courses_; ++i) {
// Generate some random number and push it into grades vector.
grades_.push_back(this->rand_gen(70, 100));
}
}
double compute_average()
{
int sum = 0;
// Assuming no one will manipulate number of courses
for (int g : grades_)
sum += g;
// Change this to return sum / grades_.size(); incase you are going serious.
return sum / 5;
}
public:
// Constructor function that initiates with name and
// number of a student.
Student();
// << operator overloading.
friend std::ostream& operator<<(
std::ostream& os, const Student& s);
// Prints grades of a student.
void print_grades(std::ostream& os) const;
};
/** CONSTRUCTOR **/
Student::Student() :
name_(gen_name()), number_(gen_number()), num_courses_(5)
{
gen_grade();
}
/** Overloading '<<' operator **/
std::ostream& operator<<(
std::ostream& os, const Student& s)
{
os << "Name = " << s.name_
<< ", Number = " << s.number_;
return os;
}
/** Print Grades **/
void Student::print_grades(std::ostream& os) const
{
for (int i = 0; i < num_courses_; ++i) {
os << grades_[i] << ", ";
}
}
/** Generates random numbers between min and max **/
int Student::rand_gen(int min, int max) {
return std::rand()%(max - min + 1) + min;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.