Array Manipulation

Hello people of C++ forums. I am a rookie programmer. I hope you can assist me:

Implement the processEnrollments function to complete the desired program.

Consider the development of a program to help manage university course registrations. Input consists of two files, whose names will be specified as command line parameters. The first file describes the courses available and the maximum enrollment that is permitted in each course. The second file contains requests for enrollment by various students. To simplify this assignment, we will assume that no student requests multiple enrollments in the same course.

Enrollment requests are accepted on a first-come, first-served basis, until the desired course is full. The program must therefore track the total number of enrollment requests accepted for each course.

The program will produce two sets of output. The first, written to standard output, is a log of how each enrollment request is handled. The second, written to a file, is a report on the total enrollment of each course. The name of the file to which this report should be written will be specified as a command line parameter.

Hence the program will be invoked with three command line parameters, e.g.,

registration courseList.txt requests.txt outputReport.csv

Here is the source code that I am to make all changes in. I started filling it in but i'm puzzled on the remaining ones:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "arrayutils.h"
#include "enrollment.h"
#include <iostream>
#include <string>


using namespace std;


/*
 * 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)
{
     courseFile>> numCourses;
    for (int i = 0; i < numCourses; ++i)
    {
        courseFile >> courseNames[i] >> maxEnrollments[i];
    }
}

/*
 * Read the enrollment requests, processing each one and tracking
 * the number of students successfully enrolled into each course.
 */
void processEnrollmentRequests (istream& enrollmentRequestsFile, 
				int numCourses, 
				string* courseNames, 
				int* maxEnrollments, 
				int* enrollments)
{
  // Start the enrollment counters at zero
  //!! Insert your code here


  // Read the requests, one at a time, serving each one
  string courseName;
  enrollmentRequestsFile >> courseName;
  while (enrollmentRequestsFile) {
    enrollmentRequestsFile >> ws;
    string studentName;
    getline (enrollmentRequestsFile, studentName);

    //!! Insert your code here

    enrollmentRequestsFile >> courseName;
  }
}


/*
 * Write a CSV report listing each course and its enrollment.
 */
void generateReport (ostream& reportFile,
		     int numCourses,
		     string* courseNames,
		     int* enrollments)
{
  //!! Insert your code here
}





void processEnrollments (istream& courseFile, istream& enrollmentRequestsFile,
			 ostream& reportFile)
{
  int numCourses;
  courseFile >> numCourses;

  // Create the arrays we need
  //!! Insert your code here


  // Process the enrollments
  readCourses (courseFile, numCourses, courseNames, maxEnrollments);
  processEnrollmentRequests (enrollmentRequestsFile, numCourses,
			     courseNames, maxEnrollments, enrollments);
  generateReport (reportFile, numCourses, courseNames, enrollments);
}


Enrollment.cpp header file:
1
2
3
4
5
6
7
8
9
10
11
#ifndef ENROLLMENT_H
#define ENROLLMENT_H

#include <iostream>

void processEnrollments (std::istream& courseFile, 
			 std::istream& enrollmentRequestsFile,
			 std::ostream& reportFile);


#endif 


registration.cpp additional source code to help with main source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


#include "enrollment.h"


using namespace std;





int main (int argc, char** argv)
{
  if (argc != 4) 
    {
      cerr << "Usage: " << argv[0] << " courseFile enrollmentFile reportFile" << endl;
      return -1;
    }

  // Take input and output file names from the command line
  ifstream coursesIn (argv[1]);
  ifstream enrollmentIn (argv[2]);
  ofstream reportOut (argv[3]);

  processEnrollments (coursesIn, enrollmentIn, reportOut);

  coursesIn.close();
  enrollmentIn.close();
  reportOut.close();

  return 0;
}


  


ArrayUtils.h another header file with functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef ARRAYUTILS_H
#define ARRAYUTILS_H



//  Add to the end
//  - 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 addToEnd (T* array, int& size, T value)
{
   array[size] = value;
   ++size;
}



// 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;
}


// 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;
}


// Search an array for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int seqSearch(const T list[], int listLength, T searchItem)
{
    int loc;

    for (loc = 0; loc < listLength; loc++)
        if (list[loc] == searchItem)
            return loc;

    return -1;
}


// Search an ordered array for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int seqOrderedSearch(const T list[], int listLength, T searchItem)
{
    int loc = 0;

    while (loc < listLength && list[loc] < searchItem)
      {
       ++loc;
      }
    if (loc < listLength && list[loc] == searchItem)
       return loc;
    else
       return -1;
}


// Removes an element from the indicated position in the array, moving
// all elements in higher positions down one to fill in the gap.
template <typename T>
void removeElement (T* array, int& size, int index)
{
  int toBeMoved = index + 1;
  while (toBeMoved < size) {
    array[toBeMoved] = array[toBeMoved+1];
    ++toBeMoved;
  }
  --size;
}



// Search an ordered array for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int binarySearch(const T list[], int listLength, T searchItem)
{
    int first = 0;
    int last = listLength - 1;
    int mid;

    bool found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (list[mid] == searchItem)
            found = true;
        else 
            if (searchItem < list[mid])
                last = mid - 1;
            else
                first = mid + 1;
    }

    if (found) 
        return mid;
    else
        return -1;
}





#endif 


Well that's all the info besides some test data which won't upload for some reason. Can anyone help lead me in the right direction? I'm sure if I can get the remaining functions I can finish this program!
Topic archived. No new replies allowed.