Having trouble with the Sort function

Hey, I am working on a program that reads in a file with a student's name,their id number, 5 quizzes,2 midterms ,and a final. After that i have to drop the student's lowest quiz score. That's where I'm having troubles. i want to sort the quizzes from highest to lowest(doesn't matter from lowest to highest) and then drop the lowest score.I need help setting up the sort function to do this. Any help would be appreciated. Thanks

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>
#include <fstream>
#include <algorithm>


using namespace std;

struct students{
    string name;
    int id;
    double    quiz[5];
    double midterm[2];
    double final;
    double average;
    char grade;
};

int main()
{
    students s[30];
    string cst231;
  cout << "Enter an input file: " ;
    cin >> cst231;

    ifstream in_file;
    in_file.open(cst231);
    if(in_file.fail())
    {
        cout << "Error: Output file open fail." ;
        exit(1);
    }
    cout << "--------------------------------" <<endl;
    cout<< "Course Report: Numerical Average Order" <<endl;
    cout << "--------------------------------" <<endl;

    for (int i=0;i<5;i++)
    {
        in_file>>s[i].name>>s[i].id;
        for(int j=0; j<5;j++)
        {
            in_file>>s[i].quiz[j];
             
            cout<<s[i].quiz[j]<<" ";
        }
            for(int k=0; k<2;k++)
            {
                in_file>>s[i].midterm[k];
            }
                for(int l=0;l<1;l++)
                {
                    in_file>>s[i].final;
                }
                cout<<endl;
    }
    cout<<endl;
 




   

return 0;
}
Your sorting algorithm needs rethink. If you are not permitted to use std::sort(), then I recommend you use one of:
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/selection-sort/

You can also use a bubble sort, if you must, but these other two will out-perform it on your list of students, and are easier to understand.



After you sort the student grades, highest to lowest, then you know you can simply ignore the last grade (since it is the lowest). There's the trick. Sort normally -- who cares which grade is what -- so long as they all get sorted properly.

Only after you have sorted do you worry about 'dropping' a quiz score -- the very last one. Hence, when it is time to compute the average, treat the student quiz[] array as if it has only four elements (even though it actually has five) -- that is, ignore the last, lowest score.



Also, your struct is misnamed-- it represents a single student, right? Hence, the struct name should be singular:

1
2
3
4
struct student
{
   ...
};

Now when you have a list of students, you can name it as such:

1
2
3
int main()
{
  student students[ 30 ];

Hope this helps.
I can use std sort and that is where I am having trouble.
1
2
3
bool cmp_tests(const students&t1, const students&2){
return t1.quiz[5]<t2.quiz[5];
}

 
sort(s,s+5,cmp_tests);


This sort function does not sort the quizzes from highest to lowest. Can I get help compiling the right function. Thanks
got it.
1
2
3
4
5
6
7
8
9
10
11

for (int i = 0; i < 5; i++)
	{
		

       sort(s[i].quiz, s[i].quiz + 5);
		
}




thanks for all the help
Right... because you aren't sorting the students, you are sorting each student's quizzes.

Good job!
Topic archived. No new replies allowed.