Homework 26
 

Write a program that will read in the grade information from grades.txt and then enter a loop that asks the user for a student number and an assignment number and prints out stats like this:

 

You may start with the program below, which does many of the above steps, but does not calculate the maximum grade for a student or a maximum grade for an assignment.  To accomplish this, figure out the appropriate prototypes and write two new functions:

// Prints the maximum score obtained by 'student' on any assignment

// For instance, for the first set of sample output above, this prints

//   The max grade for student 0 is 83

//   It was obtained for homework 4

     printMaxOneStudent

 

// Prints the maximum score obtained by any student on ‘assignment’

/ For instance, for the first set of sample output above, this prints

//   The max grade for assignment 4 is 98

//   It was obtained by student 6

     printMaxOneAssignment

 

You may assume the number of students and grades are fixed at 10, and that each row gives the grades for one student.

Turn In a printout of your code and a screen capture showing your program running with the same two sets of inputs shown in the screenshot above.

Starter code:

#include <iostream>

#include <fstream>

using namespace std;

 

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);

 

int main()

{

        // 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

            printOneGrade     (grades, ROWS, COLS, student, assignment);

            printOneStudent   (grades, ROWS, COLS, student);

            printOneAssignment(grades, ROWS, COLS, assignment);

       

            // 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)

{

      cout << "Grade for student " << student << " on assignment " << assignment;

      cout << " is " << grades[student][assignment] << 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 i=0; i<cols; i++)

      {

            cout << grades[student][i] << " ";

 

      }

    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<rows; i++)

      {

            cout << grades[i][assignment] << " ";

 

      }

    cout << endl;

 

}