Populating a Dynamically Allocated (2D?) Array From File of a struct type

Nov 3, 2019 at 11:30pm
In my code, I am dynamically allocating an array from structure Athlete:
1
2
3
4
5
6
7
8
struct Athlete
{
	string fullName;
	int* scores;
	status stStatus;
	int average;
	int* max;
};


In order to find all students with an average of n or higher, then I will need to use a dynamically allocated array of type Athlete. Here is how I dynamically allocated space for it, assuming I would have to use a 2D array.

1
2
3
 Athlete** gradeBook = new Athlete*[numAthletes];
	for (int i = 0; i < numAthletes; i++)
		gradeBook[i] = new Student[numAthletes];


**Note: numAthletes is from user input and represents the amount of rows.
For columns, that would be numScores, which is also from user input.

I will be coding this in a function with a prototype of vector<Athlete> listofHigherThan_N(Athlete*, int, int).

The desired output would be to output the full name of the student along with their averages. I have already calculated their averages as well as populated a 1D array of type integer to store their scores (successfully). However, I am unsure on how to use an array of type Athlete to output names and averages that meet the requirement grade of n or exceed it.

Here is the file I am reading from, the text file is written like first name[space]lastname[tab]1 for undergrad or 0 for grad[tab]score[tab and so forth:

1
2
3
4
5
6
7
Joseph Blake	1	81	90	95	78
Susy Marywhether   0	67	80	81	79
Brandon Cortez	1	90	91	88	93
Paul Johnson	1	93	89	78	90
Joshua Banks	1	98	91	89	97
Angela Robins	0	57	70	63	72
Shawn Martinez	0	89	92	90	92


Any help would be greatly appreciated!
Last edited on Nov 3, 2019 at 11:30pm
Nov 4, 2019 at 12:16am
Here is how I dynamically allocated space for it, assuming I would have to use a 2D array.

Why do you think you need a 2D array? Wouldn't a much simpler single dimensional array be better?

I would also recommend using std::vectors instead of all those arrays.

By the way why is max a pointer instead of a single instance?

Something more like the following, perhaps:

1
2
3
4
5
6
7
8
9
10
11
struct Athlete
{
	string fullName;
	std::vector<int> scores;
	status stStatus;
	int average;
	int max;
};

... // In some function:
  std::vector<Athlete> athletes;



Topic archived. No new replies allowed.