Code:#include <iostream.h> #include <iomanip.h> #include <string> // function prototypes void SortByGPAs( // Sort arrays by GPAs int NextIndex, // IN: First unused position in arrays int StudIds[], // IN/OUT: array of student nos - to be sorted int SumOfGrades[], // IN/OUT: array of sums of grades - to be sorted int NumSubjs[]); void Swap( // Swap the two values int &Candidate1, int &Candidate2); void SimpleMindedClrScr(void);// Clear the screen bool MoreSubjects(void); // Prompt user for more subjects Y/N? // routines from Study Guide void ReadInteger(int &Integer); // function definition void Clear(void); void ReadChar(char &Char); //my prototypes char *read_course_name(char* course_name); void print_welcome(void); void read_subjects(char* course_name); void read_subject(char* course_name); void print_grade_distribution( char* course_name, char* subject_name, int A, int B, int C, int D); void check_student_id(); const int MIN_STUDENT_ID = 1000; const int MAX_STUDENT_ID = 9999; const int SUBJ_LEN = 7; int StudIds[MAX_STUDENT_ID - MIN_STUDENT_ID]; int NumSubjs[MAX_STUDENT_ID - MIN_STUDENT_ID]; int SumOfGrades[MAX_STUDENT_ID - MIN_STUDENT_ID]; int next_free = 0; //function implementations void main(void) { char course_name[80]; print_welcome(); read_course_name(course_name); read_subjects(course_name); //print_order_of_merit(course_name); } void print_welcome(void) { SimpleMindedClrScr(); cout<<setw(50); cout<<"Course Profiling System\n"; cout<<endl; cout<<endl; cout<<"\t Generates a grade distribution and GPA for each subject and"; cout<<endl; cout<<"\tstudent GPAs with recommendations for commendations & exclusion.\n"; cout<<endl; cout<<endl; } char *read_course_name(char* course_name) { cout<< setw(50); cout<<"Course and Subject Entry"; cout<< endl; cout<< endl; cout<<"\tCourse ==> "; cin.getline(course_name, 80); return course_name; } void read_subjects(char* course_name) { do { read_subject(course_name); } while (MoreSubjects()); } void read_subject(char* course_name) { char* subject_name; int no_of_A = 0; int no_of_B = 0; int no_of_C = 0; int no_of_D = 0; cout<<endl; cout<< "\tSubject ==>"; cin >> setw(SUBJ_LEN) >> subject_name; SimpleMindedClrScr(); cout<<"\t\t"; cout<<course_name; cout<<"\t"; cout<<subject_name; cout<<endl; cout<<endl; cout<<setw(40); cout<<"Student Grade Entry" << endl; cout<< endl; while (1) { int student_id; char grade; cout<<"\t Student Number (0 to exit) ==> "; Clear(); check_student_id(); ReadInteger(student_id); // Break out of the loop when a 0 student id is entered if (student_id == 0) break; cout<<endl; cout<<"\t Grade (A,B,C or D) ==> "; ReadChar(grade); // Search for student id in array int i; bool found = false; for (i=0; i < next_free && !found; i++) { if (StudIds[i] == student_id) found = true; } if (found) { // This student id has been used before NumSubjs[i]++; SumOfGrades[i] += static_cast<int>('C') - static_cast<int>(grade) + 1; } else { // This student id is new StudIds[next_free] = student_id; NumSubjs[next_free] = 1; SumOfGrades[next_free] = static_cast<int>('C') - static_cast<int>(grade) + 1; } // Increment the count of different grades switch (grade) { case 'A': no_of_A++; break; case 'B': no_of_B++; break; case 'C': no_of_C++; break; case 'D': no_of_D++; break; } } // Loop and read another student_id/grade print_grade_distribution(course_name, subject_name, no_of_A, no_of_B, no_of_C, no_of_D); } void check_student_id() { while(StudIds==false) { cout<<"ERROR:Must in range 1000....9999 (0 to exit)"; cout<<endl; cout<<endl; cout<<"\t Student Number (0 to exit) ==> "; } } void print_grade_distribution( char* course_name, char* subject_name, int A, int B, int C, int D) { int total = A + B + C + D; int gpa = ((3 * A) + (2 * B) + (1 * C) + (0 * D)) / total; cout << course_name << " " << subject_name << endl; cout << endl; cout << "Grade Distribution" << endl; cout << endl; cout << "A B C D Total" << endl; cout << A << " " << B << " " << C << " " << D << " "; cout << A + B + C + D << endl; cout << endl; cout << "Subject GPA: " << gpa << endl; cout << endl; } /**************************************************************************** SortByGPAs Process: Sort arrays in descending order by GPA - insertion sort When GPAs are equal, sort in descending order of NumSubjs input: int NextIndex: First unused position in arrays int StudIds[]: array of student nos - to be sorted int SumOfGrades[]: array of sums of grades - to be sorted int NumSubjs[]: array of number of subjects - to be sorted output: int StudIds[]: array of student nos - sorted int SumOfGrades[]: array of sums of grades - sorted int NumSubjs[]: array of number of subjects - sorted ****************************************************************************/ void SortByGPAs( // Sort arrays by GPAs int NextIndex, // IN: First unused position in arrays int StudIds[], // IN/OUT: array of student nos - to be sorted int SumOfGrades[], // IN/OUT: array of sums of grades - to be sorted int NumSubjs[]) { int GPABy100, // GPA multiplied by 100 as an int GPABy100Prev; // " " // NextIndex is first unused position in arrays for (int i = 1; i < NextIndex; i++) { // Sort into descending GPA order - when GPAs are equal sort // into descending number-of-subjects order // To avoid floating-point comparisons, multiply GPAs by 100 and // do integer comparisons for (int j = i; j > 0 && (GPABy100 = SumOfGrades[j] * 100 / NumSubjs[j]) >= (GPABy100Prev = SumOfGrades[j-1] * 100 / NumSubjs[j-1]); j--) { // Swap elements if: 1) GPABy100 is greater // 2) GPABy100s are equal and NumSubjs greater if (GPABy100 > GPABy100Prev || (GPABy100 == GPABy100Prev && NumSubjs[j] > NumSubjs[j-1])) { // swap elements in all arrays Swap(StudIds[j], StudIds[j-1]); Swap(NumSubjs[j], NumSubjs[j-1]); Swap(SumOfGrades[j], SumOfGrades[j-1]); } } } } /**************************************************************************** Swap Process: Swap 2 integer values input: int &Candidate1: value to be swapped int &Candidate2: value to be swapped output: int &Candidate1: value swapped - old Candidate2 int &Candidate2: value swapped - old Candidate1 ****************************************************************************/ void Swap( int &Candidate1, int &Candidate2) { int TmpSwap; TmpSwap = Candidate1; Candidate1 = Candidate2; Candidate2 = TmpSwap; } /**************************************************************************** MoreSubjects Process: Determine if the user wants to continue. Validate respones (Y/y/N/n valid) on entry: on exit: input: output: bool function return value: true to continue, false to exit ****************************************************************************/ bool MoreSubjects(void) { char More = ' '; // loop until user enters Y/y or N/n, printing // error msgs for invalid responses while (More != 'Y' && More != 'N') { cout << "Another Subject? "; ReadChar(More); cout << endl; More = toupper(More); // change to upper case cout << endl; // output error message if response invalid if (More != 'Y' && More != 'N') cout << "ERROR: Valid responses are 'Y' or 'N'" << endl << endl; } // end while // return user response return (More == 'Y'); } /**************************************************************************** SimpleMindedClrScr Process: Clear the screen. input: output: ****************************************************************************/ void SimpleMindedClrScr(void) { const int SCREEN_LINES = 24; for (int i = 0; i < SCREEN_LINES; i++) cout << endl; } /**************************************************************************** Clear process: Clear standard input to end of line input: output: ****************************************************************************/ void Clear(void) { char Char = ' '; while (Char != '\n') { cin.get(Char); } } /**************************************************************************** ReadChar process: Read a character value from standard input input: output: int Integer: the value read ****************************************************************************/ void ReadChar(char &Char) { cin >> Char; Clear(); // clear to end of line } /**************************************************************************** ReadInteger process: Read (and validate) an integer value from standard input input: output: int Integer: the value read ****************************************************************************/ void ReadInteger(int &Integer) // function definition { cin >> Integer; while (cin.fail()) { cin.clear(); // clear input stream flags Clear(); // clear to end of line cout << "ERROR: You must enter an integer ==> "; cin >> Integer; // try again } Clear(); // clear to end of line }
Facemaster: nah, im just a beginner . .