98.6k views
3 votes
Header

#include
#include
#include
using namespace std;
enum Gender {
MALE,
FEMALE
};
class Student{
private:
long id; // Unique ID
string name; // Name of student
Gender gender; // Gender of student
string courseCode; // Course code (CIIC or ICOM)
double gpa; // GPA of student
public:
Student(long id, const string &name, Gender gender, double gpa){
this->id = id;
this->name = name;
this->gender = gender;
this->courseCode = "";
this->gpa = gpa;
}
Student(long id, const string &name, Gender gender, string courseCode, double gpa){
this->id = id;
this->name = name;
this->gender = gender;
this->courseCode = courseCode;
this->gpa = gpa;
}
Student(){}
static string toString(Student& s){
string genderLetter = (s.gender == MALE ? "M" : "F");
return string("{" + to_string(s.id) + "," + s.name + "," + genderLetter + "," + to_string(s.gpa) + "}");
}
static string toString(vector& v){
string result = "{";
for (Student s : v) {result += toString(s) + ",";
}
result += "}";
return result;
}
static string toString(vector& v){
string result = "{";
for (long id : v) {
result += to_string(id) + ",";}
result += "}";
return result;
}
// Getters
long getID() const {return id;}
string getName() const {return name;}
Gender getGender() const {return gender;}
double getGPA() const {return gpa;}
string getCourseCode() const {return courseCode;}
//Setters
void setName(string name){this->name = name;}
void setGender(Gender gender){this->gender = gender;}
void setGPA(double gpa){this->gpa = gpa;}
void setCourseCode(string code){this->courseCode = code;}
// EXERCISES
static double maxStudentGPA(vector& v);
static double minStudentGPA(vector& v);
static double averageGPA(vector &v, int N);
static vector countStudents(vector& v, string code);
static void removeByID(vector &v, long ID);
static void updateStudent(vector &v, const Student &s);
static vector findStudents(vector& v, float gpa);
static vector repeatedStudentNames(vector& v);
};
Cpp file
#include "Student.h"
using namespace std;
/*
* EXERCISE: #1A
*
* IMPLEMENT USING AN ENHANCED FOR LOOP (ForEach).
*
* Returns the highest GPA value possessed by any Student in the given list.
*
*/
double Student::maxStudentGPA(vector& v)
{
//YOUR CODE HERE
return -99.9; //DUMMY RETURN
}
/*
* EXERCISE: #1B
*
* IMPLEMENT USING A REGULAR FOR LOOP.
*
* Returns the lowest GPA value possessed by any Student in the given list.
*
*/
double Student::minStudentGPA(vector& v)
{
//YOUR CODE HERE
return -99.9; //DUMMY RETURN
}
/*
* Exercise #1C
*
* IMPLEMENT USING A WHILE LOOP
*
* For the first N students, calculate the average gpa
*
* Formula: average = sum / N
* Assume N is greater than 0
*/
double Student::averageGPA(vector &v, int N){//YOUR CODE HERE
return -99.9; //DUMMY RETURN
}
/**
*
* EXERCISE #2
*
* IMPLEMENT IT USING AN ENHANCED FOR LOOP (ForEach)
*
* Given a course code, you must return a vector that contains
* only the unique ID of the Students that have that particular course code.
*/
vector Student::countStudents(vector& v, string code){
vector result;
//YOUR CODE HERE
return result;
}
/*
* EXERCISE #3
*
* IMPLEMENT USING A DO...WHILE LOOP
*
* Return a vector that contains all the Students that have a GPA greater
* or equal to the GPA passed as the parameter
*
* Assume the list contains at least one element
*/
vector Student::findStudents(vector& v, float gpa){
//YOU CODE HERE
return v;
}
/*
* EXERCISE: #4
*
* IMPLEMENT WITH ANY LOOP
*
* Removes the first occurrence of the specified Student ID,
* if it is present. If not present, then list is unchanged.
*
* HINT: Verify the methods erase() and begin() of the vector
*/
void Student::removeByID(vector &v, long ID){
//YOUR CODE HERE
}
/*
* EXERCISE #5
*
* DO NOT USE WHILE LOOPS
*
* Find the Student record that matches the given Student
* and update its data. If the Student is not present, add it to the list.
*
* Remember that each Student has an unique identifier
*/
void Student::updateStudent(vector &v, const Student &s){
//YOUR CODE HERE
}
/*
* BONUS
*
* IMPLEMENT WITH NESTED LOOPS USING ANY LOOP.
*
* Returns a vector cointaining two Students that has the same name.
* If there is no repeated names, the vector stays empty.
*
* HINT: Use the compare method of the string library
*/
vector Student::repeatedStudentNames(vector& v){
//YOUR CODE HERE
return v;
}

1 Answer

5 votes

Final Answer:

  1. The C++ program provided defines a Student class with attributes like ID, name, gender, course code, and GPA. It includes exercise functions such as maxStudentGPA, minStudentGPA, averageGPA, countStudents, findStudents, removeByID, updateStudent, and repeatedStudentNames.
  2. These exercises involve tasks like finding the maximum and minimum GPA, calculating average GPA, counting students with a specific course code, finding students above a certain GPA threshold, removing students by ID, updating student data, and identifying repeated names.

Step-by-step explanation:

1. In the C++ code, the `Student` class encapsulates student information and provides methods for various operations on a vector of students. The exercises demonstrate core programming concepts like loops, conditional statements, and vector manipulation. For example, the maxStudentGPA and minStudentGPA functions aim to find the maximum and minimum GPA, respectively, employing iterative constructs.

2.The subsequent exercises, such as countStudents, findStudents, removeByID, updateStudent, and repeatedStudentNames , present specific programming challenges, including counting students with a particular course code, finding students above a certain GPA, removing students by ID, updating student data, and identifying repeated names. The comments within the code provide guidance for the implementation, enhancing code readability and modularity. Overall, these exercises offer a comprehensive set of tasks to reinforce understanding and application of C++ programming concepts in practical scenarios.

User Destructor
by
7.4k points