This assignment utilizes a class in an application that might be useful in the “real world.” It requires the sorting of, and computation with, data stored in a vector inside of a class. In this assignment, you will design an Object-Oriented program and manipulate data using a vector within the class.
Instructions
You are working for a university to maintain a list of grades and some related statistics for a student.
Class and Data members:
Create a class called Student that stores a student’s grades (integers) in a vector (do not use an array). The class should have data members that store a student’s name and course for which the grades are earned.
Constructor(s):
The class should have a 2-argument constructor that receives the student’s name and course as parameters and sets the appropriate data members to these values.
Member Functions:
The class should have functions as follows:
- Member functions to set and get the student’s name and course variables.
- A member function that adds a single grade to the vector. Only positive grades are allowed. Call this function AddGrade .
- A member function to sort the vector in ascending order.
Feel free to use the sort function that is available in the algorithm library for sorting vectors. Here is a web page that may help:
Or, if you would prefer to write your own sort code, you may find this site to be helpful:
http://www.cplusplus.com/articles/NhA0RXSz/
- A member function to compute the average (x̄) of the grades in the vector. The formula for calculating an average is
x̄ = ∑xi / n
where xi is the value of each grade and
n is the total number of grades in the vector.
When using a vector, you can manipulate it just like an array. For a review on how to loop over an array to calculate a sum, you may find this video to be helpful:
- A member function to determine the lowest grade in the vector and a separate member function to determine the highest grade in the vector. [Note that to receive credit for these functions, they must contain an algorithm to search through the vector to determine the minimum or maximum value. You cannot simply sort the vector and return the first or last data member or use the *min_element or *max_element functions.]
Hint: The following web page provides an excellent example of algorithms to find the min and max values in an array: [Look at Technique 2]
Sample Solution