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.
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;
}
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...?
#include "arrayUtils.h"
#include "courses.h"
usingnamespace 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 = newint[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];
elsereturn 0;
}
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include "arrayUtils.h"
#include "students.h"
usingnamespace 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 = newint [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