Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When adding a new student to the course, if the array capacity is exceeded, incr

ID: 3841356 • Letter: W

Question

    When adding a new student to the course, if the array capacity is exceeded, increase the array size by creating a new larger array and copying the contents of the current array to it.

    Implement the dropStudent function.

    Add a new function named clear() that removes all students from the course.

    Implement the destructor and copy constructor to perform a deep copy in the class.

Write a test program that creates a course, adds three students, removes one, and displays the students in the course.

#1 nclude O Stream 2 #include "Course.h" 3 using namespace std; 5 Course: Course Cconst string& courseName, int capacity) 6 7 numbe rofStudents 0 8 this courseName course Name 9 this capacity capacity 10 students new string capacity] 11 12 13 Course: Course C) 14 15 delete D] students 16 17 18 string Course: agetCounseNameo const 19 200 return course Name 21 22 23 void Course addstudent const string& name) 24 25 students Inumber0fStudents] name 26 number 0fStudents 27 28 29 void Course dropStudentCconst string& name) 30 1 31 Left as an exercise 32 33 34 string* Course: getStudents O const 35 1 36 return students 38 39 int Course getNumberofStudentsC) const 40 41 return numberofStudents 42

Explanation / Answer

Answer

/*function modified so that if the array capacity is exceeded, the array size is increased by creating a new larger array and copying the contents of the array to it*/
void Course::addStudent(const string& name)
{
   if(numberOfStudents > this->capacity)
   {
       /*increasing the size of the capacity by 10, you can choose more if you want*/
       this->capacity = tis->capacity + 10;
       /*creating a new array*/
       new_students = new string[capacity];
       /*copying the old array content to new array*/
       for(int i=0; i<numberOfStudents; i++)
       {
           new_students[i]   = students[i];          
       }
       /*storing the fresh name in the newly created space*/
       new_students[i] = name;
       /*the count of students name will be 1 more than i value*/
       numberOfStudents = i + 1;
   }
   else
   {
       /*This module will run when there are enough space in the array*/
       students[numberOfStudents] = name;
       numberOfStudents++;
   }
}

/*Implementing the dropStudent(): removing the given name from the array and adjusting the array*/
void Course::dropStudent(const string& name)
{
   for(int i=0; i<numberOfStudents; i++)
   {
       /*looking for the given name in the array*/
       if(students[i] == name)
       {
           /*if the given name is found, shift all the other names one place left in the array, this will automatically remove the name from the array*/
           for(int j = i; j<numberOfStudents-1; j++)
           {
               students[j] = students[j+1];
           }
           students[numberOfStudents] = "";
           break;
       }
   }
}


/*new clear() that removes all the students from the course*/
void Course::clear()
{
   for(int i=0; i<numberOfStudents; i++)
   {
       students[i]=""; /*nullifying each content of the array*/
   }
   numberOfStudents = 0; /*setting the numberOfStudents back to 0*/
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote