#if 0 /********************************************* This program reads in student/grade data from namedgrades.txt, gets a name from the user and prints out his homework average. **********************************************/ #include #include #include using namespace std; /********************************************* ** PROTOTYPES & STRUCT DEFINITIONS *********************************************/ struct Student { string name; int *grades; // all the homeworks }; int find(string name, Student* students, int size); void sort(Student *A, int N, int); bool before(Student a, Student b, int); int maxindex(Student *A, int N, int); void swap(Student &a, Student &b); /********************************************* ** MAIN FUNCTION *********************************************/ int main() { // Open file and read header info int i,j; int Ns, Nh; string junk; ifstream IN("namedgrades.txt"); IN >> Ns >> junk >> Nh >> junk; // create the students array Student *students; students = new Student[Ns]; // create the grades array for each student for (i=0; i> students[i].name; for (j=0; j> students[i].grades[j]; } } // Ask for what homework to sort by int hwNum; cout << "What homework # to sort by: "; cin >> hwNum; sort (students, Ns, hwNum); // print the students for (i=0; i 1; length--) swap(A[maxindex(A,length,hwNum)],A[length-1]); } // Returns the index of the "max" element in the array, according to before() int maxindex(Student *A, int N, int hwNum) { int imax = 0, i; for(i = 1; i < N; i++) if (before(A[imax],A[i],hwNum)) imax = i; return imax; } void swap(Student &a, Student &b) { Student temp = a; a = b; b = temp; } #endif