Sort isn't working

My program is supposed to accept the names of students with one test score. Then is supposed to organize the students in descending order based on their scores. It also gives off the average score.


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace std;

//Student class
class student{
    public:
        string firstname;
        string lastname;
        float testscore;
        student operator=(const student &right){
            student opstu;
            opstu.firstname = right.firstname;
            opstu.lastname = right.lastname;
            opstu.testscore = right.testscore;
            return opstu;
        }
};

int main(){
cout << "Enter number of students:";

int numstudent, avgscore = 0;
student *pstudent, temp;
bool swapp = false;

cin >> numstudent;

pstudent = new student[numstudent];

//Accepts info of students
for(int index = 0; index < numstudent; index++){
    cout << "Info for student #" << index+1 << endl;
    cout << "Enter student first name: ";
    cin >> pstudent[index].firstname;
    cout << "Enter student last name: ";
    cin >> pstudent[index].lastname;
    cout << "Enter student test score: ";
    cin >> pstudent[index].testscore;
    avgscore += pstudent[index].testscore;
}

//The sort
do
    {swapp = false;
for(int pass =0; pass < numstudent-1; pass++){
        if(pstudent[pass].testscore > pstudent[pass+1].testscore){
            temp = pstudent[pass];
            pstudent[pass] = pstudent[pass+1];
            pstudent[pass+1] = temp;
        }}}while(swapp);

cout << "Results" << endl;

//Prints results
for(int n = 0; n < numstudent; n++){
    cout << "Name: " << pstudent[n].firstname << " " << pstudent[n].lastname << endl;
    cout << "Score: " << pstudent[n].testscore << endl;
}

cout << "Average score was " << avgscore/numstudent << endl;
return 0;
}


Its works pretty well, but the output does sort. Heres an example of the output:

Enter number of students:3
Info for student #1
Enter student first name: Chris
Enter student last name: Smith
Enter student test score: 25
Info for student #2
Enter student first name: John
Enter student last name: Baker
Enter student test score: 95
Info for student #3
Enter student first name: Bill
Enter student last name: Lopez
Enter student test score: 35
Results
Name: Chris Smith
Score: 25
Name: John Baker
Score: 95
Name: Bill Lopez
Score: 35
Average score was 51




Please and thanks in advance and sorry about the long post.
1
2
3
4
5
6
7
8
do
    {swapp = false;
for(int pass =0; pass < numstudent-1; pass++){
        if(pstudent[pass].testscore > pstudent[pass+1].testscore){
            temp = pstudent[pass];
            pstudent[pass] = pstudent[pass+1];
            pstudent[pass+1] = temp;
        }}}while(swapp);


As swapp is defined as false, this will only execute once, which means lower scores will be left-shifted only once, and won't be sorted properly.
It's not about the swapp variable. The algorithm itself is incorrect, it's not a sorting algorithm.
If you compare index a with index a+1, and swap the order if a > a+1, and do it for the entire array, multiple passes, the array will indeed be sorted low to high, left to right.

I'm not critiquing how he chooses to sort the array, only telling him why it's not sorting with the given code.
Last edited on
took out the do while loop and instead changed to :
1
2
3
4
5
6
7
8
9
10
for(int p = 0; p < numstudent; p++)
  for(int s = 0; s < numstudent-1; s++) 
  {
    if(pstudent[s].testscore > pstudent[s+1].testscore)
    {
      temp = pstudent[s];
      pstudent[s] = pstudent[s+1];
      pstudent[s+1] = temp;
    }
  }


In addition to not sorting enough times the operator= is not working. Try this instead:
1
2
3
4
5
6
student &operator=(const student &right){
    firstname = right.firstname;
    lastname = right.lastname;
    testscore = right.testscore;
    return *this;
}


Topic archived. No new replies allowed.