finish the code: // Adds a new course in the courses array of pointers. // If the course…
- User
finish the code: // Adds a new course in the courses array of pointers. // If the course exists (there is a course with the course_id), return false. // Else if the course does not exist and the array has empty space then insert the course and return true. // Else if there is no empty space, double the array size (e.g., if the array has size 16, then increase it to size 32), and then add the course. Finally return true. // The items of stars_count array of a newly added course must be all zero. Moreover, the star_rank_head field should be nullptr because there are no star ranks yet.
// @param: course_array the array of pointers for the Course // @param: an unsigned integer representing the id of the course the student star ranks (course_id) // @param: a characters array for the name of the course // @param: an unsigned integer representing the number of courses until now. bool add_course(Course **&course_array, const unsigned int course_id, const char name[MAX_TITLE], unsigned int &num_courses) { // TODO: Write code to implement add_course cout << "increase course array size to " << num_courses << endl; return false; }
- Assistant
bool add_course(Course **&course_array, const unsigned int course_id, const char name[MAX_TITLE], unsigned int &num_courses) { // Check if the course exists for (unsigned int i = 0; i < num_courses; i++) { if (course_array[i]->get_course_id() == course_id) { return false; // Course already exists } }
// Check if there is empty space in the array if (num_courses < MAX_COURSES) { // Create a new course object Course* new_course = new Course(course_id, name);
// Initialize stars_count with all zeros for (unsigned int i = 0; i < MAX_STARS; i++) { new_course->stars_count[i] = 0; } // Set star_rank_head to nullptr new_course->star_rank_head = nullptr; // Add the new course to the array course_array[num_courses] = new_course; // Increment the number of courses num_courses++; return true; - User
can u check whether the course exists by using the following code and use it as a helper function: bool search_course(Course **&course_array, const unsigned int course_id, const unsigned int num_courses, int &i) { Course *course; if (course_array != nullptr) { for (i = 0; i < num_courses; i++) { course = course_array[i]; if (course == nullptr) { break; }
if (course->course_id == course_id) { // found an existing entry return true; } }} return false; }
- Assistant
Sure! Here's the updated code with the helper function
search_course():bool search_course(Course **&course_array, const unsigned int course_id, const unsigned int num_courses, int &i) { Course *course; if (course_array != nullptr) { for (i = 0; i < num_courses; i++) { course = course_array[i]; if (course == nullptr) { break; } if (course->get_course_id() == course_id) { // found an existing entry return true; } } } return false; } bool add_course(Course **&course_array, const unsigned int course_id, const char name[MAX_TITLE], unsigned int &num_courses) { int index; if (search_course(course_array, course_id, num_courses, index)) { return false; // Course already exists } if (num_courses < MAX_COURSES) { Course* new_course = new Course(course_id, name); // Initialize stars_count with all zeros for (unsigned int i = 0; i < MAX_STARS; i++) { new_course->stars_count[i] = 0; } // Set star_rank_head to nullptr new_course->star_rank_head = nullptr; course_array[num_courses] = new_course; num_courses++; return true; } else { unsigned int new_size = 2 * MAX_COURSES; Course** new_array = new Course*[new_size]; for (unsigned int i = 0; i < num_courses; i++) { new_array[i] = course_array[i]; } Course* new_course = new Course(course_id, name); // Initialize stars_count with all zeros for (unsigned int i = 0; i < MAX_STARS; i++) { new_course->stars_count[i] = 0; } // Set star_rank_head to nullptr new_course->star_rank_head = nullptr; new_array[num_courses] = new_course; delete[] course_array; course_array = new_array; num_courses++; return true; } }