Finish the code //Adds the star ranking of a student for a course. //If the student or…
- User
Finish the code //Adds the star ranking of a student for a course. //If the student or the course does not exist, return false. //If the student or the course does not exist, return false. //Else if the student already has a star ranking for that course then return return false. //Else, add a star ranking, update the stars_count of the course, and increase by one the ranks_count of the student and return true.
// IMPORTANT: Always add in a new StarRank at the end of the list
// @param: student_head points to the head of Student list // @param: the id of the student (sid) // @param: course_array the array of pointers for the Course // @param: the id of the course the student ranks (course_id) // @param: the number of courses in the site // @param: the rating which is a between 1 and MAX_RANKING_STARS // @out: a boolean value indicating whether the insertion was successful bool add_star_rank(Student *&student_head, unsigned int sid, Course **&course_array, unsigned int course_id, const unsigned int num_courses, int star) { // TODO: Write code to implement add_star_rank // use error cout carefully
}
- Assistant
bool add_star_rank(Student *&student_head, unsigned int sid, Course **&course_array, unsigned int course_id, const unsigned int num_courses, int star) { // Check if the student exists Student *curr_student = student_head; while (curr_student != nullptr) { if (curr_student->id == sid) { break; } curr_student = curr_student->next; }
if (curr_student == nullptr) { cout << "Student with id " << sid << " does not exist." << endl; return false; }
// Check if the course exists if (course_id < 0 || course_id >= num_courses || course_array[course_id] == nullptr) { cout << "Course with id " << course_id << " does not exist." << endl; return false; }
// Check if the student already has a star ranking for this course StarRank *curr_star_rank = curr_student->star_rank_head; while (curr_star_rank != nullptr) { if (curr_star_rank->course_id == course_id) { cout << "Student with id " << sid << " already has a star ranking for course " << course_id << endl; return false; } curr_star_rank = curr_star_rank->next; }