Dynamic 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
Dynamically create an array of Student structures
Create an array using new. The maximum size needed is 10.
If I were to create an array to contain 10 int's I would write:

 
int int_array[10];


If I wanted an array to contain 10 Foo's I would write:

 
Foo array[10];


Then you can add up to 10 of the type you have declared the array of, i.e. 10 int's or Foo's at index [1..9]

HTH
By "dynamically", the instructions are saying that you need to dynamically allocate memory to store the structures. As kbw says, you need to use new.

If you don't understand the meaning of what I've just written, you need to hit your textbooks.
@MikeyBoy - I know what it means, I am trying to help him by starting with how to declare an array of a certain type and size.
In case you haven't worked it out yet:

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
#include <string>

using namespace std;

#define MAX_GRADES 5
#define MAX_STUDENTS 10

struct Student
{
	string id;
	string name;
	double grades[MAX_GRADES];
	double average;
};

int main()
{
	Student* students = new Student[MAX_STUDENTS];

	// do stuff

	delete[] students;

	return 0;
}


But the struct would be better defined with a default constructor, as follows so that each member of the struct is initialised:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Student
{
	Student()
	{
		for (int n = 0; n < MAX_GRADES; ++n)
			grades[n] = 0.00;
		average = 0.00;
	}

	string id;
	string name;
	double grades[MAX_GRADES];
	double average;
};


HTH
ajh32
Please do not do peoples homework on the forum. It doesn't help anyone.
yea i understand that but what im really confused about is, how would i access each of the 5 grades for 10 students.

This is how the file looks like, with 10 sets of different students:

1020526770
Elizabeth Anderson
68
78
70
66
60


so for example if i wanted to print the 2nd students 4th grade, would it be something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Student
{
	string id;
	string name;
	double grades[MAX_GRADES];
	double average;
};

int main()
{
     Student* studentInfo = new Student[10];
     
     cout <<  studentInfo[1].grades[3];


Last edited on
Yes, that's exactly right. studentInfo[1] will give you the second student in the array, the .grades will give you the grades array for that student, and then the [3] will give you the 4th grade in that array.

Of course, somewhere between those two lines, you'll have to put the values into the Student objects, but I'm sure you've worked that out already!
Last edited on
Topic archived. No new replies allowed.