// Assignment 9 solution // Use arrays #include #include #include #include #include using namespace std; // function prototypes bool readStudentData(ifstream& fin,int []); void writeStudentReport(ofstream& fout,int []); int totalAssignmentPoints(int []); int getGrade(int pct); int getPlusMinus(int pct); const int IdIndex = 0; const int MidtermIndex = 12; const int FinalIndex = 13; const int LabExIndex = 14; const int AssTotIndex = 15; const int TotPtsIndex = 16; const int PercentIndex = 17; const int GradeIndex = 18; const int PlusMinusIndex = 19; const int NumberAssignments = 11; int main() { int student[20]; const string DataFile = "C:/temp/ass7data.txt"; const string ReportFile = "C:/temp/student.rpt"; const int MaxPoints = 400; ifstream fin(DataFile.c_str()); if (!fin) { cerr << "Unable to open input file: " << DataFile << endl; exit(-2); } ofstream fout(ReportFile.c_str()); if (!fout) { cerr << "Unable to open output file: " << ReportFile << endl; exit(-3); } // Write report file heading fout << "Student ----- Assignment Grades ----- Ass Mid Fin LEx Total Pct Gr" << endl; fout << "-------- -- -- -- -- -- -- -- -- -- -- -- --- --- --- --- ----- --- --" << endl; while (readStudentData(fin,student)) { student[AssTotIndex] = totalAssignmentPoints(student); student[TotPtsIndex] = student[AssTotIndex] + student[MidtermIndex] + student[FinalIndex]+student[LabExIndex]; student[PercentIndex] = static_cast(100.*student[TotPtsIndex]/MaxPoints+.5); student[GradeIndex] = getGrade(student[PercentIndex]); student[PlusMinusIndex] = getPlusMinus(student[PercentIndex]); writeStudentReport(fout,student); } fin.close(); fout.close(); } // Read in: student id, 11 assignment's points, midterm points, final points and lab exercise points bool readStudentData(ifstream& fin,int array[]) { for (int i = 0; i <= LabExIndex; i++) fin >> array[i]; if (fin.fail()) return false; return true; } void writeStudentReport(ofstream& fout,int array[]) { fout << setfill('0'); fout << setw(8) << array[IdIndex] << ' '; fout << setfill(' '); for (int i = 1; i <= NumberAssignments; i++) fout << setw(3) << array[i]; fout << setw(5) << array[AssTotIndex] << setw(5) << array[MidtermIndex] << setw(5) << array[FinalIndex] << setw(4) << array[LabExIndex] << setw(6) << array[TotPtsIndex] << setw(5) << array[PercentIndex] << setw(2) << static_cast(array[GradeIndex]) << static_cast(array[PlusMinusIndex]) <= 98) return '+'; else if (pct < 60) return ' '; else if (pct%10 < 2) return '-'; else if (pct%10 > 7) return '+'; else return ' '; }