#include #include using namespace std; #if 0 int** createArray(int rows, int cols); void readData(int** dataArray, ifstream& file_in, int rows, int cols); void printOneGrade (int **grades, int rows, int cols, int student, int assignment); void printOneStudent (int **grades, int rows, int cols, int student); void printOneAssignment(int **grades, int rows, int cols, int assignment); void printMaxOneStudent (int **grades, int rows, int cols, int student); void printMaxOneAssignment(int **grades, int rows, int cols, int assignment); int main98779879() { // Constants -- to keep simple we assume 10 students and 10 assignments const int ROWS = 10; const int COLS = 10; // Create 2D array and read grades in // grades[i][j] will be grades of i'th student on j'th assignment int **grades = createArray(ROWS, COLS); ifstream fin("grades.txt"); readData(grades, fin, ROWS, COLS); char ch; do { // Ask user what they want to know about int student, assignment; cout << "Enter student number [0..9]: "; cin >> student; cout << "Enter assignment number [0..9]: "; cin >> assignment; // Print out different info about the student and assignment cout << endl; printOneGrade (grades, ROWS, COLS, student, assignment); cout << endl; printOneStudent (grades, ROWS, COLS, student); printMaxOneStudent(grades, ROWS, COLS, student); cout << endl; printOneAssignment (grades, ROWS, COLS, assignment); printMaxOneAssignment(grades, ROWS, COLS, assignment); cout << endl; // Should we continue? cout << "Continue? (y/n) "; cin >> ch; } while (ch == 'y'); return 0; } int** createArray(int rows, int cols) { int** dataArray = new int*[rows]; for (int i = 0; i < rows; i++) { dataArray[i] = new int[cols]; } return dataArray; } void readData(int** dataArray, ifstream& file_in, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { file_in >> dataArray[i][j]; } } } // Prints grade for the given student and assignment // e.g. "Grade for student 1 on assignment 3 is 74"; void printOneGrade (int **grades, int rows, int cols, int student, int assignment) { int theGrade = grades[student][assignment]; cout << "Grade for student " << student << " on assignment " << assignment; cout << " is " << theGrade << endl; } // Prints all grades for the given student // e.g. "Grades for student 1 are: // 96 67 56 74 94 100 98 68 95 65" void printOneStudent (int **grades, int rows, int cols, int student) { cout << "Grades for student " << student << " are: " << endl; for (int j = 0; j < cols; j++) { cout << grades[student][j] << " "; } cout << endl; } // Prints all grades for the given assignment // e.g. "Grades for assignment 3 are: // 72 74 59 69 67 54 71 89 95" void printOneAssignment(int **grades, int rows, int cols, int assignment) { cout << "Grades for assignment " << assignment << " are: " << endl; for (int i = 0; i < cols; i++) { cout << grades[i][assignment] << " "; } cout << endl; } // Prints the maximum score obtained by 'student' on any assignment void printMaxOneStudent (int **grades, int rows, int cols, int student) { // default value is the first grade int maxGradeIndex = 0; int maxGradeValue = grades[student][maxGradeIndex]; // Check all grades for a higher maximum for (int j = 0; j < cols; j++) { int thisGradeValue = grades[student][j]; if (thisGradeValue > maxGradeValue) { // found a new maximum maxGradeValue = thisGradeValue; maxGradeIndex = j; } } // Print results cout << "The max grade for student " << student << " is " << maxGradeValue << "." << endl; cout << "It was obtained for homework " << maxGradeIndex << "." << endl; } // Prints the maximum score obtained by any student on 'assignment' void printMaxOneAssignment (int **grades, int rows, int cols, int assignment) { // default value is for first student int maxGradeIndex = 0; int maxGradeValue = grades[maxGradeIndex][assignment]; // Check all grades for a higher maximum for (int j = 0; j < cols; j++) { int thisGradeValue = grades[j][assignment]; if (thisGradeValue > maxGradeValue) { // found a new maximum maxGradeValue = thisGradeValue; maxGradeIndex = j; } } // Print results cout << "The max grade for assignment " << assignment << " is " << maxGradeValue << "." << endl; cout << "It was obtained by student " << maxGradeIndex << "." << endl; } #endif