AndreiD
AndreiD

Reputation: 123

C++ Sort class array using qsort

C++: I'm trying to sort some students that are stored in a class by average media.

Only qsort, don't advice me of std::sort, thank you!

Qsort compare function:

int cmp(Student *a, Student *b) {
    return (int)(((Student *)b)->get_media() - ((Student *)a)->get_media());
}

qsort call:

qsort(&tab, (size_t)n, sizeof(tab), (int(*)(const void*, const void*))cmp);

There's no compiler error, but it won't sort.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Student {
private:
    char name[20];
    char surname[20];
    int *marks;
    int group;
    float avg_mark;
public:
    Student()
    {
        char na[20], sur[20];
        int group;
        cout << "\nEnter name: ";
        cin >> na;
        cout << "\nEnter surname: ";
        cin >> sur;
        cout << "\nEnter group: ";
        cin >> group;
        init(na, sur, group);
    }
    ~Student()
    {
        cout << "\ndestructor";
        delete []marks;
    }
    void init(char *n, char *p, int gr)
    {
        strcpy(name, n);
        strcpy(surname, p);
        group = gr;
        marks = new int[6];
        for (int i = 0; i < 6; i++)
        {
            cout << "\nEnter mark " << i + 1 << ": ";
            cin >> *(marks + i);
        }
        avg_mark = media();
    }
    float media()
    {
        int s = 0;
        for (int i = 0; i < 6; i++)
            s += marks[i];
        return ((float)s / 6);
    }
    void set_name(char *n)
    {
        strcpy(name, n);
    }
    char* get_name()
    {
        return name;
    }
    void set_surname(char *p)
    {
        strcpy(name, p);
    }
    char* get_surname()
    {
        return surname;
    }
    int get_group()
    {
        return group;
    }
    float get_media()
    {
        return avg_mark;
    }
};

int cmp(Student *a, Student *b);

int comparator(void *a, void *b) {
    return (int)(((Student *)b)->get_media() - ((Student *)a)->get_media());
}



void main(void)
{
    int n;
    cout << "\nEnter n: ";
    cin >> n;
    Student *tab = new Student[n];
    for (int i = 0; i < n; i++)
        cout << i + 1 << ". " << tab[i].get_name() << " " << tab[i].get_surname() << " Group:" << tab[i].get_group() << " Average mark: " << tab[i].get_media() << endl;
    //qsort(&tab[0], (size_t)n, sizeof(tab), (int*)cmp);
    cout << endl;
    qsort(&tab, (size_t)n, sizeof(tab), (int(*)(const void*, const void*))cmp);
    for (int i = 0; i < n; i++)
        cout << i + 1 << ". " << tab[i].get_name() << " " << tab[i].get_surname() << " Group:" << tab[i].get_group() << " Average mark: " << tab[i].get_media() << endl;
    cin.ignore();
    cin.get();
}

int cmp(Student *a, Student *b) {
    return (int)(((Student *)b)->get_media() - ((Student *)a)->get_media());
}

Upvotes: 2

Views: 2070

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103751

qsort(&tab, (size_t)n, sizeof(tab), (int(*)(const void*, const void*))cmp);

&tab is the address of the pointer tab. You want to pass the address of the first element of your array. That's &tab[0] or simply tab.

Also, you need to pass the size of a Student object, not the size of a pointer. So change sizeof(tab) to sizeof(Student) or sizeof(*tab). So the call should look like this:

qsort(tab, (size_t)n, sizeof(*tab), (int(*)(const void*, const void*))cmp);

Upvotes: 1

Related Questions