What am I doing WRONG? Sorting pointer array in class?

This is the pointer that is in the class private members
double *grades;

This is the function that I retrieve to supposedly compare and transfer the data as you will see in the next coding
1
2
double *getGrades()
{ return grades; }


This is the actual sorting function, do not mind the other values as they work just fine, but the grades get a hell of a messup when I try to change them. I have no idea how to do this and it keeps screwing up.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

void sortStudent(Student *students,int size)
{
	int scan,
	    minInd,
	    ID;

	string first,
	       last;

	double *gradeHold,
		min;
        for(scan=0;scan<(size-1);scan++)
	{
		minInd=scan;
		min=students[scan].calcAverage();
		first=students[scan].getFirstN();
		last=students[scan].getLastN();
		ID=students[scan].getStudentID();
		gradeHold=students[scan].grades;

		for(int i=(scan+1);i<size;i++)
		{
			if(students[i].calcAverage()>min)
			{
				minInd=i;
				min=students[i].calcAverage();
				first=students[i].getFirstN();
				last=students[i].getLastN();
				ID=students[i].getStudentID();
				gradeHold=students[i].grades;
			}
		}

		students[minInd].setFirstN(students[scan].getFirstN());
		students[minInd].setLastN(students[scan].getLastN());
		students[minInd].setStudentID(students[scan].getStudentID());
		students[minInd].switchGrades(students[scan].getGrades());
		students[scan].setFirstN(first);
		students[scan].setLastN(last);
		students[scan].setStudentID(ID);
		students[scan].switchGrades(gradeHold);
		
	}
}


The issue is that the "return *double" function is the only way I know of how to access the private pointer. I am pretty sure the problem lies there, does anyone care to enlighten me on how to pass the pointer array of a function correctly?
I think you're overthinking things.

Assuming that operator= is defined correctly for Student, your sort function should probably resemble the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void sortStudent(Student *students,int size)
{
    for ( unsigned i=0; i<size-1; ++i )
    {
        unsigned min_index = i ;

        for ( unsigned j=i+1; j<size; ++j )
        {
            if ( students[j].calcAverage() < students[min_index].calcAverage() )
                min_index = j ;
        }

        if ( min_index != i )
        {
            Student temp = students[i] ;           // uses copy constructor
            students[i] = students[min_index] ;    // uses operator=
            students[min_index] = temp ;           // uses operator=
        }
    }
}

Topic archived. No new replies allowed.