Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double GetAverageScore(double** myArray, int numStudents, int examSlot)
- {
- double scoreCollection = 0.0;
- //calculate average
- for (int i = 0; i < numStudents; ++i)
- {
- scoreCollection = scoreCollection + myArray[i][examSlot];
- }
- double averageScore = scoreCollection / numStudents;
- return averageScore;
- }
- string GetLetterGrades(double** myArray, int i, int j)
- {
- string letterGrade;
- double averageScore = 0.0;
- if (myArray[i][j] <= averageScore - 5 || myArray[i][j] >= averageScore + 5)
- {
- letterGrade = "(C)";
- }
- if (myArray[i][j] > averageScore + 5 && myArray[i][j] < averageScore + 15)
- {
- letterGrade = "(B)";
- }
- if (myArray[i][j] < averageScore - 5 && myArray[i][j] > averageScore - 15)
- {
- letterGrade = "(D)";
- }
- if (myArray[i][j] >= averageScore + 15)
- {
- letterGrade = "(A)";
- }
- if (myArray[i][j] <= averageScore - 15)
- {
- letterGrade = "(E)";
- }
- return letterGrade;
- }
- string ReturnNumberGrade(double** myArray, int numStudents, int numExams)
- {
- string letterGrade;
- int numberA = 0;
- int numberB = 0;
- int numberC = 0;
- int numberD = 0;
- int numberE = 0;
- for (int i = 0; i < numStudents; ++i)
- {
- for (int j = 0; j < numExams; ++j) {
- if (GetLetterGrades(myArray, i, j) == "(A)")
- {
- numberA = numberA + 1;
- }
- if (GetLetterGrades(myArray, i, j) == "(B)")
- {
- numberB = numberB + 1;
- }
- if (GetLetterGrades(myArray, i, j) == "(C)")
- {
- numberC = numberC + 1;
- }
- if (GetLetterGrades(myArray, i, j) == "(D)")
- {
- numberD = numberD + 1;
- }
- if (GetLetterGrades(myArray, i, j) == "(E)")
- {
- numberE = numberE + 1;
- }
- }
- }
- ostringstream gradeLine;
- gradeLine << numberA << "(A)" << numberB << "(B)" + numberC << "(C)" << numberD << "(D)" << numberE << "(E)";
- return gradeLine.str();
- }
- double GetTotalAverage(double** myArray, int i, int numExams)
- {
- double totalAverage;
- double totalScore = 0;
- for (int j = 0; j < numExams; ++j)
- {
- totalScore = totalScore + myArray[i][j];
- }
- totalAverage = totalScore / numExams;
- return totalAverage;
- }
- #ifdef _MSC_VER
- #define _CRTDBG_MAP_ALLOC
- #include <crtdbg.h>
- #define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
- #else
- #define VS_MEM_CHECK
- #endif
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- int main(int argc, char* argv[]) {
- if (argc < 3)
- {
- cerr << "Please provide name of input and output files";
- return 1;
- }
- cout << "Input file: " << argv[1] << endl;
- ifstream in(argv[1]);
- if (!in)
- {
- cerr << "Unable to open " << argv[1] << " for input";
- return 2;
- }
- cout << "Output file: " << argv[2] << endl;
- ofstream out(argv[2]);
- if (!out)
- {
- in.close();
- cerr << "Unable to open " << argv[2] << " for output";
- return 3;
- }
- VS_MEM_CHECK // enable memory leak check
- // Your program..
- int numStudents = 0;
- int numExams = 0;
- //read in number of students and number of exams
- in >> numStudents >> numExams;
- // Skip the rest of the line
- in.ignore(std::numeric_limits<int>::max(), '\n');
- //set up array
- double** myArray = new double* [numStudents];
- for (int i = 0; i < numStudents; ++i)
- {
- myArray[i] = new double[numExams];
- }
- string* nameArray = new string[numStudents];
- for (int i = 0; i < numStudents; i++) {
- //fill name array
- string firstName;
- string lastName;
- in >> firstName >> lastName;
- nameArray[i] = firstName + " " + lastName;
- //fill exam score array
- for (int j = 0; j < numExams; j++) {
- int score;
- in >> score;
- myArray[i][j] = score;
- }
- }
- out << "Exam Averages:" << endl;
- //output average
- for (int i = 0; i < numExams; ++i)
- {
- out << "\t" << "Exam " << i << " Average = ";
- out << setprecision(1);
- out << GetAverageScore(myArray, numStudents, i);
- out << endl;
- }
- //output student names and grades with letter grades
- out << "Student Exam Grades:" << endl;
- for (int i = 0; i < numStudents; ++i)
- {
- out << "\t" << nameArray[i];
- for (int j = 0; j < numExams; ++j)
- {
- out << "\t" << myArray[i][j];
- out << GetLetterGrades(myArray, i, j);
- }
- out << endl;
- }
- //output final numbers of grades
- out << "Exam Grades:" << endl;
- for (int i = 0; i < numExams; ++i)
- {
- out << "Exam " << i << " ";
- out << ReturnNumberGrade(myArray, numStudents, numExams);
- out << endl;
- }
- //output each students final grade
- out << "Student Final Grades:" << endl;
- for (int i = 0; i < numStudents; ++i)
- {
- out << nameArray[i] << " ";
- out << GetTotalAverage(myArray, i, numExams);
- }
- //clear up memory
- for (int i = 0; i < numStudents; ++i)
- {
- delete[] myArray[i];
- }
- delete[] myArray;
- delete[] nameArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement