Programming Workshop 2 (CSCI 1061U)
Winter 2021
Faculty of Science
Ontario Tech University
You are asked to implement a program to manage Student
records of the following form:
class Student
{private:
std::string name_;
int number_;
std::vector<int> grades_;
const int num_courses_;
// You need to implement the following four methods
static std::string gen_name() { } // To do
static int gen_number() { } // To do
static int gen_grade() { } // To do
double compute_average() { } // To do
public:
const std::string& name, int number) : name_(name), number_(number), num_courses_(5)
Student(
{for (int i=0; i<num_courses_; ++i) {
grades_.push_back(std::rand());
}
}
friend std::ostream& operator<<(std::ostream& os, const Student& s) {
"Name = " << s.name_ << ", Number = " << s.number_;
os << return os;
}
void print_grades(std::ostream& os) const
{for (int i=0; i<num_courses_; ++i) {
grades_[i] << ", ";
os <<
}
} };
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-record 7
...
Names, numbers and grades will be set to random values with the following constraints:
Note that each student stores only 5 grades.
The program will store students in std::vector
and will implement the following functionality:
Average values may be floats.
Consider the code shown below, which showcases the use of sort
algorithm available in STL.
#include <iostream>
#include <vector>
#include <ctime> // time()
#include <cstdlib> // srand(), rand()
#include <algorithm> // min_element(), max_element(), sort()
bool sort(int i, int j) { return (i<j); }
int main()
{std::srand(std::time(0));
std::vector<int> x;
int n = 10;
for (int i=0; i<n; ++i) {
std::rand());
x.push_back(
}
for (std::vector<int>::iterator xi = x.begin(); xi != x.end(); xi++) {
std::cout << *xi << std::endl;
}
std::cout << "min value = " << *std::min_element(x.begin(), x.end()) << std::endl;
std::cout << "index of min value = " << std::min_element(x.begin(), x.end()) - x.begin() << std::endl;
std::cout << "max value = " << *std::max_element(x.begin(), x.end()) << std::endl;
std::cout << "index of max value = " << std::max_element(x.begin(), x.end()) - x.begin() << std::endl;
std::sort(x.begin(), x.end());
for (std::vector<int>::iterator xi = x.begin(); xi != x.end(); xi++) {
std::cout << *xi << std::endl;
}
std::sort(x.begin(), x.end(), sort);
for (std::vector<int>::iterator xi = x.begin(); xi != x.end(); xi++) {
std::cout << *xi << std::endl;
}
return 0;
}
Please submit your source code via Canvas. It is suggested that you implement the entire functionality in a single cpp file.