Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- using namespace std;
- struct CourseGrade
- {
- string arrCourseNames;
- int arrCreditHours;
- char arrGrades;
- };
- //****************************************************************************
- //*** CONSTANT DEFINITIONS ***
- //****************************************************************************
- const int MAX_COURSES = 6; // Maximum number of courses
- const char QUALITY_POINTS[] = {'F', 'D', 'C', 'B', 'A'};
- // Assignms quality points to letter grade
- const int TOTAL_LETER_GRADES = 5; // Total number of leter grades used
- const int NOT_FOUND = -1; // Search indicator that's value not found
- //****************************************************************************
- //****************************************************************************
- //*** PROTOTYPE DEFINITIONS ***
- //****************************************************************************
- void promptCourse(string& sName, int& nCreditHours, char& cGrade);
- void displayCourse(string sName, int nCreditHours, char cGrade,
- const char arrQltyPts[], int nTotLetters);
- double calcGPA(const string arrNames[], const int arrCreditHours[],
- const char arrGrades[], int nNumCourses,
- const char arrQltyPts[], int nTotLetters);
- void dispCourses(CourseGrade course[], int nNumCourses,
- const char arrQltyPts[], int nTotLetters);
- int calcQualityPoints(char cLetterGrade, const char arrQltyPts[], int nTotLetters);
- //****************************************************************************
- //main
- int main()
- {
- //***
- //*** Declare arrays to hold the grade records
- //***
- string arrCourseNames[MAX_COURSES]; // Array that stores course names
- int arrCreditHours[MAX_COURSES]; // Array that stores credit hours
- char arrGrades[MAX_COURSES]; // Array that stores the grades.
- int nGradesToProcess; // Stores the nubmer of grades to proces
- //***
- //*** Get the number of grades to process
- //***
- cout << "How many grades would you like to process? ";
- cin >> nGradesToProcess;
- cin.ignore(1000, '\n');
- while ((nGradesToProcess < 1) || (nGradesToProcess > MAX_COURSES))
- {
- cout << "*** PLEASE ENTER A VALUE BETWEEN " << 1 << " AND " << MAX_COURSES
- << " INCLUSIVE: ";
- cin >> nGradesToProcess;
- cin.ignore(1000, '\n');
- }
- //***
- //*** Now that we know how many grades we have, read them in
- //***
- for (int nInd = 0; nInd < nGradesToProcess; nInd++)
- {
- promptCourse(arrCourseNames[nInd], arrCreditHours[nInd], arrGrades[nInd]);
- }
- //***
- //*** Display grade report!
- //***
- cout << endl << endl;
- dispCourses(arrCourseNames, arrCreditHours, arrGrades, nGradesToProcess,QUALITY_POINTS, TOTAL_LETER_GRADES);
- return 0;
- }
- //FUNCTIONS
- //****************************************************************************
- //*** FUNCTION: calcQualityPoints ***
- //*** ***
- //*** PARAMETERS: cLetterGrade [in] - letter grade ***
- //*** ***
- //*** arrQltyPts - array contains letter grades***
- //*** where indexes represent the ***
- //*** quality points. ***
- //*** ***
- //*** nTotLetters - total number of letter ***
- //*** grades. ***
- //*** ***
- //*** RETURNS: int - Quality points for a grade.***
- //*** ***
- //*** DESCRIPTION: This function converts a letter grade to ***
- //*** the number of quality points the grade ***
- //*** represents. If the function returns ***
- //*** NOT_FOUND, the letter grade is not ***
- //*** recognized. ***
- //****************************************************************************
- int calcQualityPoints(char cLetterGrade, const char arrQltyPts[], int nTotLetters)
- {
- //***
- //*** Convert the letter grade to upper case for search
- //***
- cLetterGrade = static_cast<char>(toupper(static_cast<int>(cLetterGrade)));
- //***
- //*** Search for the letter grade in the array of grades. If found, the
- //*** index is the number of quality points. If not, return NOT_FOUND to
- //*** tell the caller of the function that a bad grade was supplied.
- //***
- for (int nInd = 0; nInd < nTotLetters; nInd++)
- {
- if (cLetterGrade == arrQltyPts[nInd])
- {
- //***
- //*** FOUND IT!!! Index is Quality Points!
- //***
- return nInd;
- }
- }
- //***
- //*** Got this far! Never found it!
- //***
- return NOT_FOUND;
- }
- //****************************************************************************
- //****************************************************************************
- //*** FUNCTION: promptCourse ***
- //*** ***
- //*** PARAMETERS: sName [out] - Course name ***
- //*** ***
- //*** nCreditHours [out] - credit hours for ***
- //*** the course earns. ***
- //*** ***
- //*** cGrade [out]- Letter grade earned for the***
- //*** course. ***
- //*** ***
- //*** RETURNS: NOTHING ***
- //*** ***
- //*** DESCRIPTION: This function prompts the user for the ***
- //*** course information which includes the ***
- //*** name of the course, the credit hours, ***
- //*** and the letter grade received. ***
- //****************************************************************************
- void promptCourse(string& sName, int& nCreditHours, char& cGrade)
- {
- cout << endl;
- cout << "Enter the course name: ";
- getline(cin, sName);
- cout << "Enter the credit hours: ";
- cin >> nCreditHours;
- cin.ignore(1000, '\n');
- cout << "Enter the letter grade: ";
- cin >> cGrade;
- cin.ignore(1000, '\n');
- cout << endl;
- }
- //****************************************************************************
- //****************************************************************************
- //*** FUNCTION: displayCourse ***
- //*** ***
- //*** PARAMETERS: sName [in] - Course name ***
- //*** ***
- //*** nCreditHours [in] - credit hours for ***
- //*** the course earns. ***
- //*** ***
- //*** cGrade [in]- Letter grade earned for the ***
- //*** course. ***
- //*** ***
- //*** arrQltyPts [in] - array that stores QP ***
- //*** conversion. ***
- //*** ***
- //*** nTotLetters [in] - total letter grades ***
- //*** ***
- //*** RETURNS: NOTHING ***
- //*** ***
- //*** DESCRIPTION: This function displays the letter course ***
- //*** information, including the letter grade ***
- //*** and the quality points earned. ***
- //*** The course is displayed in tabular form. ***
- //****************************************************************************
- void displayCourse(string sName, int nCreditHours, char cGrade,
- const char arrQltyPts[], int nTotLetters)
- {
- //***
- //*** Convert the letter grade to upper case for search
- //***
- cGrade = toupper(cGrade);
- cout << setw(35) << sName << setw(15) << nCreditHours
- << setw(15) << calcQualityPoints(cGrade, arrQltyPts, nTotLetters)
- << setw(15) << cGrade << endl;
- }
- //****************************************************************************
- //****************************************************************************
- //*** FUNCTION: calcGPA ***
- //*** ***
- //*** PARAMETERS: arrNames [in] - array of course names ***
- //*** ***
- //*** arrCreditHours [in] - array of credit ***
- //*** hours ***
- //*** ***
- //*** cGrades [in]- array of letter grades. ***
- //*** ***
- //*** nNumCourses - number of courses entered. ***
- //*** ***
- //*** arrQltyPts [in] - array that stores QP ***
- //*** conversion. ***
- //*** ***
- //*** nTotLetters [in] - total letter grades ***
- //*** ***
- //*** RETURNS: double - GPA ***
- //*** ***
- //*** DESCRIPTION: This function iterates over the arrays ***
- //*** that store grade records and calculates ***
- //*** the GPA. ***
- //****************************************************************************
- double calcGPA(string arrNames[], int arrCreditHours[],
- char arrGrades[], int nNumCourses,
- const char arrQltyPts[], int nTotLetters)
- {
- double dfTotalQualityPoints = 0.0; // Total quality points earned.
- double dfTotalCredits = 0.0; // Total credits earned.
- //***
- //*** Iterate over the arrays of grade records and accumulate
- //*** the total quality points and total credits.
- //***
- for (int nInd = 0; nInd < nNumCourses; nInd++)
- {
- dfTotalQualityPoints +=
- arrCreditHours[nInd] * calcQualityPoints(arrGrades[nInd], arrQltyPts, nTotLetters);
- dfTotalCredits += arrCreditHours[nInd];
- }
- //***
- //*** Return the average
- //***
- return dfTotalQualityPoints / dfTotalCredits;
- }
- //****************************************************************************
- //****************************************************************************
- //*** FUNCTION: calcGPA ***
- //*** ***
- //*** PARAMETERS: arrNames [in] - array of course names ***
- //*** ***
- //*** arrCreditHours [in] - array of credit ***
- //*** hours ***
- //*** ***
- //*** cGrades [in]- array of letter grades. ***
- //*** ***
- //*** nNumCourses - number of courses entered. ***
- //*** ***
- //*** arrQltyPts [in] - array that stores QP ***
- //*** conversion. ***
- //*** ***
- //*** nTotLetters [in] - total letter grades ***
- //*** ***
- //*** RETURNS: double - GPA ***
- //*** ***
- //*** DESCRIPTION: This function iterates over the arrays ***
- //*** that store grade records and calculates ***
- //*** the GPA. ***
- //****************************************************************************
- void dispCourses(CourseGrade course[], int nNumCourses,
- const char arrQltyPts[], int nTotLetters)
- {
- //***
- //*** Print header first
- //***
- cout << "============================================================================" << endl;
- cout << "=== GRADE REPORT ===" << endl;
- cout << "============================================================================" << endl;
- cout << endl;
- cout << left << setw(35) << "COURSE NAME" << setw(15) << "CREDIT HRS"
- << setw(15) << "QLTY PTS" << setw(15) << "GRADE" << endl;
- cout << left << setw(35) << "====================" << setw(15) << "=========="
- << setw(15) << "==========" << setw(15) << "==========" << endl;
- //***
- //*** Iterate over the courses and display them
- //***
- for (int nInd = 0; nInd < nNumCourses; nInd++)
- {
- displayCourse(course[nInd].arrCourseNames,
- course[nInd].arrCreditHours,
- course[nInd].arrGrades,
- arrQltyPts, nTotLetters);
- }
- cout << "============================================================================" << endl;
- cout << "GPA: " << right << fixed << showpoint << setprecision(2) << setw(30)<<
- calcGPA(arrNames, arrCreditHours, arrGrades, nNumCourses, arrQltyPts, nTotLetters) << endl;
- cout << "============================================================================" << endl;
- }
- //****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement