Dynamically creating an array of structures

Dynamically create an array of Student structures as declared below to store the student data from the file. There are 10 students and each student has five grades.

What do these instructions mean. Do I have to make 10 different structures like the one below or is it asking me to do something else. Can somebody please explain this to me?

1
2
3
4
5
6
7
struct Student
{
  string id;
  string name;
  double grades[MAX_GRADES];
  double average;
};
Last edited on
What do these instructions mean. Do I have to make 10 different structures like the one below or is it asking me to do something else

I think it means that you have to use dynamic memory allocation.
http://www.cplusplus.com/doc/tutorial/dynamic/

It wants you to allocate memory for 10 separate students
Last edited on
im having problem understanding how would i make the grades work cause each student has five grades and there are 10 different students, so would the MAX_GRADES be 5 or is it gonna be like 2D array.... grades[10][5] ? ........ any hints, clues?
Last edited on
so would the MAX_GRADES be 5
Yes. If it helps you can name it MAX_GRADES_PER_STUDENT

What you want to do is
- Define a pointer to Student struct (Student*)
- Use 'new[]' to allocate memory for 10 Student objects
- Set the data for each of the students. The syntax is the same as when using arrays
You don't need to modify the MAX_GRADES or anything else in the student struct.
Treat the struct just like any other variable.
You declare the an array of structs just like you do for a array of chars or ints.

int* a = new int[10];
or for your case
Student* students_array = new Students[10];
so how would i access each of the fives grades for each student, thats what im really confused about.... so for example if i wanted to print the the 2nd student's 4th grade
how would i do that?

This is how the input file looks, with 10 sets for each student

1020526770
Elizabeth Anderson
68
78
70
66
60

Topic archived. No new replies allowed.