Question with dynamic arrays

I have been racking my brains on this program and im so lost now

this is what im told

# You should use dynamically allocated arrays to store the student names (an array of strings) and the total credits (an array of ints) for which each student has registered.
# For your initial implementation, use ordered insertion to keep the students in order (by name) and ordered sequential search when looking for students.

this is what i have done

1
2
3
4
5
6
7
void initializeStudents(int maxStudents)
{
    studentNames = new string [maxStudents];
    studentCredits = new int [maxStudents];
    
    for(
}

Just to start the stupid thing is this even legit?
Last edited on
Please use the Source Code Format tag on the right to format code.

The start seems ok.

You probably need to keep count of the number of students you've stored to date, which is initially zero. That aside, don't add anything else to your initialiseStudents function, as I'm not sure what you want to do in the for loop.

You need to use a seperate function to do your ordered insert.
Ok thanks that helped but im stuck on the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Add a student, starting with 0 credits. Return position where
//   inserted.
int addStudent (std::string studentsName)
{

}

// Add a number of credits taken by a student
int addCredits (std::string studentName, int credits)
{
    int i;

    for (i = 0; i < numStudents; i++)
        studentName[i] = studentName[i] + studentCredit[i];

    return 0;
}


   "Usage: " << argv[0] << " courseFile enrollmentFile reportFile" << endl;


Im not sure what to do at this point anyway i was trying to just get the information added but it wasnt working well.
Last edited on
studentName[i] = studentName[i] + studentCredit[i];

I don't think this is what you meant to do. You are making the ith element of studentName become the i[supth[/sup] element with the number of credits on the end...?
what i was trying to get it to do was

where the input file courses1.dat contains
4
CS150 4
CS252 1
CS300U 3
ENGL200 3

and requests1.dat contains:
10
CS150 Aiken, Roberto
CS150 Al-douli, Gustavo
CS150 Bach, Ruth
CS150 Blankenship, Kelly
CS150 Cancado, Fahad
CS300U Franklin, Clifford
CS300U Aiken, Roberto
CS252 Aiken, Roberto
CS252 Blankenship, Kelly
ENGL200 Blankenship, Kelly
ENGL200 Beidel, Lance

the output on the screen should be:
Student Credits
Aiken, Roberto 8
Al-douli, Gustavo 4
Bach, Ruth 4
Beidel, Lance 3
Blankenship, Kelly 8
Cancado, Fahad 4
Franklin, Clifford 3


i was trying to add up the inputs like above from the input file but i realize im so far off base its horrible.
Last edited on
For reading the file:
http://www.cplusplus.com/doc/tutorial/files/

Once you have them in an std::vector or an array, you can loop through it and add them up.
Hmm i tried that and was told i was wrong cause they are already being fed in ill show them all to you.

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


#include "courses.h"
#include "students.h"


using namespace std;

/*
 * Read the enrollment requests, processing each one and tracking
 * the number of students successfully enrolled into each course.
 */
void processEnrollments (istream& enrollmentRequestsFile)
{
  int maxStudents;
  enrollmentRequestsFile >> maxStudents;
  initializeStudents (maxStudents);

  string courseName;
  enrollmentRequestsFile >> courseName;
  while (enrollmentRequestsFile)
  {
    enrollmentRequestsFile >> ws;
    string studentName;
    getline (enrollmentRequestsFile, studentName);

    //cerr << studentName << " " << courseName << endl;

    int credits = getCredits (courseName);
    addCredits (studentName, credits);

    enrollmentRequestsFile >> courseName;
  }
}

// Print an alphabetical list of students, with the
// total number of credits taken by each.
void printReport (ostream& out)
{
  string padding (30, ' ');
  string header = "Student" + padding;
  header = header.substr(0,30) + " Credits";
  cout << header << endl;
  int totalCredits = 0;
  for (int i = 0; i < numStudents; ++i)
    {
      string line = studentNames[i] + padding;
      cout << line.substr(0,30) << " " << studentCredits[i] << endl;
      totalCredits += studentCredits[i];
      out << '"' << studentNames[i] << '"' << ","
	  << studentCredits[i] << endl;
    }
  cout << numStudents << " students registered for a total of "
       << totalCredits << " credits." << endl;
}


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]);
  readCourses (coursesIn);
  coursesIn.close();


  ifstream enrollmentIn (argv[2]);
  processEnrollments (enrollmentIn);
  enrollmentIn.close();


  ofstream reportOut (argv[3]);
  printReport (reportOut);
  reportOut.close();

  cleanUpCourses();
  cleanUpStudents();


  return 0;
}


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
#include "arrayUtils.h"
#include "courses.h"

using namespace std;


int numCourses;

string* courseNames; // Array of course names

int* courseCredits;  // Array - number of credits for each courses


// Initialize all course data, allowing for at least the indicated
//   number of courses
void initializeCourses(int maxCourses)
{
  numCourses = 0;
  courseNames = new string[maxCourses];
  courseCredits = new int[maxCourses];
}


// Clean up course data
void cleanUpCourses()
{
  delete [] courseNames;
}


// Read course info from an input stream
void readCourses (std::istream& in)
{
  int maxCourses;
  in >> maxCourses;
  initializeCourses (maxCourses);
  string name;
  while (in >> name)
    {
      int credit;
      in >> credit;
      //cerr << name << " " << credit << endl;
      addCourse (name, credit);
    }
}

// Add a course with the indicated number of credits
void addCourse (std::string courseName, int credits)
{
  courseNames[numCourses] = courseName;
  courseCredits[numCourses] = credits;
  ++numCourses;
}

// Get the number of credits for a course
int getCredits (std::string courseName)
{
  int pos = seqSearch (courseNames, numCourses, courseName);
  if (pos >= 0)
    return courseCredits[pos];
  else
    return 0;
}



THIS IS THE ONLY FILE I CAN TOUCH

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#include "arrayUtils.h"
#include "students.h"


using namespace std;

int numStudents;

int* studentCredits;  // Array - total number of credits for student

std::string* studentNames; // Array of student names

ifstream infile;
ofstream outfile;

// Initialize all student data, allowing for at least the indicated
//   number of students
void initializeStudents(int maxStudents)
{
    numStudents = 0;
    studentNames = new string [maxStudents];
    studentCredits = new int [maxStudents];
}

// Clean up student data
void cleanUpStudents()
{
   delete [] studentNames;
   delete [] studentCredits;

}

// Add a student, starting with 0 credits. Return position where
//   inserted.
int addStudent (std::string studentsName)
{
    for (int i = 0; i < studentsName; i++)
    {
        int loc = binarySearch(studentNames, numStudents, studentsName);

            if (loc != -1)
                studentNames[numStudents]++;
    }
    return studentNames;
}

// Add a number of credits taken by a student
int addCredits (std::string studentName, int credits)
{

}


Im getting 4 errors stilll but heh i think im moving forward
Last edited on
addStudent isn't inserting new name. It simply tacks it on the end after looking up where it should go. You also don't check if your list is full.
Topic archived. No new replies allowed.