EDIT: This was marked as a duplicate question, but the question linked is not what I am asking. I know how to pass an array of a structure into a function. I am seeking help on how to pass member data from elements in that array to a function.
I have a homework assignment in C++ where I was tasked to create a structure for keeping track of a student's information:
struct Student {
string name;
double idNumber;
double currentGrade;
double lastGrade;
double overallGPA;
};
The user is prompted to enter the number of students that they will be entering data for which creates an array of the structure based on this input. The program then loops over each element, prompting the user to enter the member data for each instance of the structure.
Once all of the data is entered, I am tasked with sorting the array based on the member variable the user chooses. I have written the sorting function for sorting by name variable, and I have written a general function for sorting the array based on a double variable, but I am unsure how I can use only one function for all four double variables.
Essentially, is there a way I can use this function:
void doubleSort(Student arr[], int arrSize) {
// bubble sort: high -> low
Student temp;
for(int i = 0; i < arrSize; i++) {
for(int j = 0; j < arrSize; j++) {
if(arr[j] < arr[i]) {
// swap values
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
But pass member data as an arguement, i.e. idNumber, in order to avoid writing a sorting function for each individual member variable?