I'm having trouble with one of my assignments (or maybe I'm overthinking it?) I need to create
- a function to take integer parameters for number of students and tests.
- Allocate the memory needed for the array of students and the array of test scores for each student.
Return a pointer to the array of Student structures. No display output is done in this function.
int main() { int studentNum; int testNum; cout << "How many students are there?\n"; cin >> studentNum; cout << "How many tests are there?"; cin >> testNum; system("pause"); return 0; }
my function
GradeBook *initArrays(int s, int t)
{
GradeBook *ptr;
// Allocate the array of Student structures.
ptr = new GradeBook[s];
// Allocate an array of ints (to hold test scores)
// for each element of the array of Student structures.
for (int count = 0; count < s; count++)
{
ptr[count].tests = new int[t];
}
// Return a pointer to the array of structures.
return ptr;
}
edit: I have edited my function, could i get some opinions on that?
{})? This isn't even approximately valid c++ code.std::vectorand writing a simple matrix class.int*be "a pointer to the array of Student structures"? Your function needs to allocate memory for those structures (you should not use a global variable).