// Program will student and grade data from a file, and fill two string arrays // and one two dimensional float array. The array will be processed to produce // the course average for each student and the test average for each test. // Input: Text file will student and grade data // Output: Table display with student name, test grades, class average and test // average. #include #include #include #include using namespace std; typedef float grade_array[50]; typedef float table_array[50][4]; typedef float test_array[4]; typedef string string_array[50]; void process_file(ifstream&, string_array, string_array, table_array, int&); void int_grades(table_array, int); void row_avg(table_array, grade_array, int); void col_avg(table_array, test_array, int); void display_results(string_array, string_array, table_array, grade_array, test_array, int); int main() { ifstream textin; string_array f_name; string_array l_name; table_array grades; grade_array course_avg; test_array test_avg; int rowcount = 0; textin.open ("grades.txt"); if (!textin) { cout << endl << endl; cout << "File was not found!" << endl; cout << "Please check for existance of the file and run the program again." << endl; cout << endl; system ("Pause"); } else { int_grades(grades,rowcount); process_file(textin,f_name,l_name,grades,rowcount); row_avg(grades,course_avg,rowcount); col_avg(grades,test_avg,rowcount); display_results(f_name,l_name,grades,course_avg,test_avg,rowcount); } textin.close(); return 0; } //****************************************************************************** void display_results(string_array f_name, string_array l_name, table_array grades, grade_array course_avg, test_array test_avg, int rowcount) { int row; int col; cout << endl; cout << fixed << setprecision(2); cout << setw(20) << left << "Student Name"; cout << setw(10) << right << "Test 1"; cout << setw(10) << right << "Test 2"; cout << setw(10) << right << "Test 3"; cout << setw(10) << right << "Test 4"; cout << setw(15) << right << "Course Avg"; cout << endl; for (row = 0; row < rowcount; row++) { cout << setw(10) << left << f_name[row] << setw(10) << left << l_name[row]; for (col = 0; col < 4; col++) cout << setw(10) << right << grades[row][col]; cout << setw(15) << right << course_avg[row]; cout << endl; } cout << endl; cout << setw(20) << left << "Test Averages"; for (col = 0; col < 4; col++) { cout << setw(10) << right << test_avg[col]; } cout << endl; cout << endl; system("Pause"); } //****************************************************************************** void int_grades(table_array grades, int rowcount) { int row; int col; for (row = 0; row < 50; row++) for (col = 0; col < 4; col++) grades[row][col] = 0.0; } //****************************************************************************** void col_avg(table_array grades, test_array test_avg, int rowcount) { int row; int col; float total; for (col = 0; col < 4; col++) { total = 0.0; for (row = 0; row < rowcount; row++) total = total + grades[row][col]; test_avg[col] = total / row; } } //****************************************************************************** void row_avg(table_array grades, grade_array course_avg, int rowcount) { int row; int col; float total; for (row = 0; row < rowcount; row++) { total = 0.0; for (col = 0; col < 4; col++) total = total + grades[row][col]; course_avg[row] = total / 4; } } //****************************************************************************** void process_file(ifstream& textin, string_array f_name, string_array l_name, table_array grades, int& rowcount) { int col; while (!textin.eof()) { textin >> f_name[rowcount] >> l_name[rowcount]; for(col = 0; col < 4; col++) textin >> grades[rowcount][col]; rowcount++; } }