1

This is a college assignment so I would prefer pseudo code, or just a basic idea of the process; however, any help would be appreciated.

The problem:

I have two parallel arrays: array courseNames and array maximumEnrollment. I must use a function provided by faculty that inserts new elements into array courseNames by alphabetical order:

// Assume the elements of the array are already in order
// Find the position where value could be added to keep
//    everything in order, and insert it there.
// Return the position where it was inserted
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger 
//      than the current value of that counter

template <typename T>
int addInOrder (T* array, int& size, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= 0 && value < array[toBeMoved]) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[toBeMoved+1] = value;
  ++size;
  return toBeMoved+1;
}

Since the array courseNames is empty, I don't have to worry about the array being in order when I first use the function. All the input comes from a file separated by lines, every line contains the name of the class(ei. CS150) and the maximum enrollment allowed(ei. 50). The first array, holds the names of the courses, while the second array holds the maximum enrollment.

My code for the function that reads the inputs from the files looks like this:

/*
 * Read the course names and max enrollments. Keep the courses
 * in alphabetic order by course name.
 */
void readCourses (istream& courseFile, int numCourses,
          string* courseNames, int* maxEnrollments)
{
  //!! Insert your code here
    for(int i =0; i < numCourses; ++i){
        string course;
        courseFile >> course;
        int pos = addInOrder(courseNames, numCourses, course);
        courseFile >> maxEnrollments[pos];
    }
}

I'm using the position given to the elements of courseNames as the index to store the maximum enrollment for the given course. The problem arises when a new value is inserted into courseNames and I get the position in which it was added. If I use that position, the old value of the index will be overwritten and everything will fall out of place.

My question is, how can I prevent this from happening? How can I find out what value was replaced to keep the array in order and make the parallel array maximumEnrollment* match the index of courseNames for each course?

Thank you in advance for any help you might be able to provide!

UPDATE

I was also provided this function:

// Add value into array[index], shifting all elements already in positions
//    index..size-1 up one, to make room.
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger 
//      than the current value of that counter

template <typename T>
void addElement (T* array, int& size, int index, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= index) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[index] = value;
  ++size;
}

So I changed my code to this:

/*
 * Read the course names and max enrollments. Keep the courses
 * in alphabetic order by course name.
 */
void readCourses (istream& courseFile, int numCourses,
          string* courseNames, int* maxEnrollments)
{
  //!! Insert your code here
    int size = 1;
    for(int i =0; i < numCourses; i++){
        string course;
        int maxEnroll;
        courseFile >> course;
        courseFile >> maxEnroll;

        int pos = addInOrder(courseNames, size, course);
        addElement(maxEnrollments, size, pos, maxEnroll);

    }
}
4

0 回答 0