1

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?

Austin
  • 11
  • 2
  • 1
    This doesn't seem to be a duplicate, but maybe the question could be worded differently. I think the OP here wants to access some fields of the structure based on a parameter in the function. Array passing is already solved. – MayeulC Sep 21 '16 at 18:04
  • look at https://stackoverflow.com/questions/14418595/c-method-for-iterating-through-a-structs-members-like-an-array – MayeulC Sep 21 '16 at 18:08
  • Correct. My wording was not great, but I am trying to access fields of the structure within the sorting function and am not sure how to do so. – Austin Sep 21 '16 at 18:10
  • You don't pass the member data as an argument, you pass a pointer to a function that compares two students, or you pass a pointer to an implementation of an interface with a virtual ->compareStudents method. The sort function will use this instead of the less-than operator. – Matt Timmermans Sep 22 '16 at 12:43
  • @MattTimmermans: Well, I don't see the problem in using an union with an array here. – MayeulC Sep 23 '16 at 06:09

1 Answers1

0

No You can't write a single generic function for sorting, You need to have compare function for each field or You need to keep track of member by which you want to sort by sending one more argument and inside sorting do check for which tag which field to be compared and then swap.

The best way to sort to use compare function per field.

bool compareCurrentGrade (const Student & a, const Student & b) {
   return a.currentGrade < b.currentGrade;
}

And then use sort function defined in #include <algorithm>

sort(studentarr, startoffset, endoffset, compareCurrentGrade);

to know more about sort See

If you don't want to use external sort libray modules then you have to write sorting functions per field.