Advertisement
Guest User

Untitled

a guest
Dec 13th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6. struct CourseGrade
  7. {
  8. string arrCourseNames;
  9. int arrCreditHours;
  10. char arrGrades;
  11.  
  12. };
  13.  
  14. //****************************************************************************
  15. //*** CONSTANT DEFINITIONS ***
  16. //****************************************************************************
  17. const int MAX_COURSES = 6; // Maximum number of courses
  18.  
  19. const char QUALITY_POINTS[] = {'F', 'D', 'C', 'B', 'A'};
  20. // Assignms quality points to letter grade
  21. const int TOTAL_LETER_GRADES = 5; // Total number of leter grades used
  22.  
  23. const int NOT_FOUND = -1; // Search indicator that's value not found
  24. //****************************************************************************
  25.  
  26.  
  27. //****************************************************************************
  28. //*** PROTOTYPE DEFINITIONS ***
  29. //****************************************************************************
  30.  
  31. void promptCourse(string& sName, int& nCreditHours, char& cGrade);
  32. void displayCourse(string sName, int nCreditHours, char cGrade,
  33. const char arrQltyPts[], int nTotLetters);
  34. double calcGPA(const string arrNames[], const int arrCreditHours[],
  35. const char arrGrades[], int nNumCourses,
  36. const char arrQltyPts[], int nTotLetters);
  37. void dispCourses(CourseGrade course[], int nNumCourses,
  38. const char arrQltyPts[], int nTotLetters);
  39. int calcQualityPoints(char cLetterGrade, const char arrQltyPts[], int nTotLetters);
  40. //****************************************************************************
  41.  
  42.  
  43. //main
  44.  
  45. int main()
  46. {
  47. //***
  48. //*** Declare arrays to hold the grade records
  49. //***
  50. string arrCourseNames[MAX_COURSES]; // Array that stores course names
  51. int arrCreditHours[MAX_COURSES]; // Array that stores credit hours
  52. char arrGrades[MAX_COURSES]; // Array that stores the grades.
  53. int nGradesToProcess; // Stores the nubmer of grades to proces
  54.  
  55. //***
  56. //*** Get the number of grades to process
  57. //***
  58. cout << "How many grades would you like to process? ";
  59. cin >> nGradesToProcess;
  60. cin.ignore(1000, '\n');
  61. while ((nGradesToProcess < 1) || (nGradesToProcess > MAX_COURSES))
  62. {
  63. cout << "*** PLEASE ENTER A VALUE BETWEEN " << 1 << " AND " << MAX_COURSES
  64. << " INCLUSIVE: ";
  65. cin >> nGradesToProcess;
  66. cin.ignore(1000, '\n');
  67. }
  68.  
  69. //***
  70. //*** Now that we know how many grades we have, read them in
  71. //***
  72. for (int nInd = 0; nInd < nGradesToProcess; nInd++)
  73. {
  74. promptCourse(arrCourseNames[nInd], arrCreditHours[nInd], arrGrades[nInd]);
  75. }
  76.  
  77. //***
  78. //*** Display grade report!
  79. //***
  80. cout << endl << endl;
  81. dispCourses(arrCourseNames, arrCreditHours, arrGrades, nGradesToProcess,QUALITY_POINTS, TOTAL_LETER_GRADES);
  82.  
  83. return 0;
  84. }
  85.  
  86.  
  87. //FUNCTIONS
  88.  
  89. //****************************************************************************
  90. //*** FUNCTION: calcQualityPoints ***
  91. //*** ***
  92. //*** PARAMETERS: cLetterGrade [in] - letter grade ***
  93. //*** ***
  94. //*** arrQltyPts - array contains letter grades***
  95. //*** where indexes represent the ***
  96. //*** quality points. ***
  97. //*** ***
  98. //*** nTotLetters - total number of letter ***
  99. //*** grades. ***
  100. //*** ***
  101. //*** RETURNS: int - Quality points for a grade.***
  102. //*** ***
  103. //*** DESCRIPTION: This function converts a letter grade to ***
  104. //*** the number of quality points the grade ***
  105. //*** represents. If the function returns ***
  106. //*** NOT_FOUND, the letter grade is not ***
  107. //*** recognized. ***
  108. //****************************************************************************
  109. int calcQualityPoints(char cLetterGrade, const char arrQltyPts[], int nTotLetters)
  110. {
  111. //***
  112. //*** Convert the letter grade to upper case for search
  113. //***
  114. cLetterGrade = static_cast<char>(toupper(static_cast<int>(cLetterGrade)));
  115.  
  116. //***
  117. //*** Search for the letter grade in the array of grades. If found, the
  118. //*** index is the number of quality points. If not, return NOT_FOUND to
  119. //*** tell the caller of the function that a bad grade was supplied.
  120. //***
  121. for (int nInd = 0; nInd < nTotLetters; nInd++)
  122. {
  123. if (cLetterGrade == arrQltyPts[nInd])
  124. {
  125. //***
  126. //*** FOUND IT!!! Index is Quality Points!
  127. //***
  128. return nInd;
  129. }
  130. }
  131.  
  132. //***
  133. //*** Got this far! Never found it!
  134. //***
  135. return NOT_FOUND;
  136. }
  137. //****************************************************************************
  138.  
  139.  
  140.  
  141. //****************************************************************************
  142. //*** FUNCTION: promptCourse ***
  143. //*** ***
  144. //*** PARAMETERS: sName [out] - Course name ***
  145. //*** ***
  146. //*** nCreditHours [out] - credit hours for ***
  147. //*** the course earns. ***
  148. //*** ***
  149. //*** cGrade [out]- Letter grade earned for the***
  150. //*** course. ***
  151. //*** ***
  152. //*** RETURNS: NOTHING ***
  153. //*** ***
  154. //*** DESCRIPTION: This function prompts the user for the ***
  155. //*** course information which includes the ***
  156. //*** name of the course, the credit hours, ***
  157. //*** and the letter grade received. ***
  158. //****************************************************************************
  159. void promptCourse(string& sName, int& nCreditHours, char& cGrade)
  160. {
  161. cout << endl;
  162. cout << "Enter the course name: ";
  163. getline(cin, sName);
  164. cout << "Enter the credit hours: ";
  165.  
  166. cin >> nCreditHours;
  167. cin.ignore(1000, '\n');
  168. cout << "Enter the letter grade: ";
  169. cin >> cGrade;
  170. cin.ignore(1000, '\n');
  171. cout << endl;
  172. }
  173. //****************************************************************************
  174.  
  175.  
  176.  
  177. //****************************************************************************
  178. //*** FUNCTION: displayCourse ***
  179. //*** ***
  180. //*** PARAMETERS: sName [in] - Course name ***
  181. //*** ***
  182. //*** nCreditHours [in] - credit hours for ***
  183. //*** the course earns. ***
  184. //*** ***
  185. //*** cGrade [in]- Letter grade earned for the ***
  186. //*** course. ***
  187. //*** ***
  188. //*** arrQltyPts [in] - array that stores QP ***
  189. //*** conversion. ***
  190. //*** ***
  191. //*** nTotLetters [in] - total letter grades ***
  192. //*** ***
  193. //*** RETURNS: NOTHING ***
  194. //*** ***
  195. //*** DESCRIPTION: This function displays the letter course ***
  196. //*** information, including the letter grade ***
  197. //*** and the quality points earned. ***
  198. //*** The course is displayed in tabular form. ***
  199. //****************************************************************************
  200. void displayCourse(string sName, int nCreditHours, char cGrade,
  201. const char arrQltyPts[], int nTotLetters)
  202. {
  203. //***
  204. //*** Convert the letter grade to upper case for search
  205. //***
  206. cGrade = toupper(cGrade);
  207.  
  208. cout << setw(35) << sName << setw(15) << nCreditHours
  209. << setw(15) << calcQualityPoints(cGrade, arrQltyPts, nTotLetters)
  210. << setw(15) << cGrade << endl;
  211. }
  212. //****************************************************************************
  213.  
  214.  
  215.  
  216. //****************************************************************************
  217. //*** FUNCTION: calcGPA ***
  218. //*** ***
  219. //*** PARAMETERS: arrNames [in] - array of course names ***
  220. //*** ***
  221. //*** arrCreditHours [in] - array of credit ***
  222. //*** hours ***
  223. //*** ***
  224. //*** cGrades [in]- array of letter grades. ***
  225. //*** ***
  226. //*** nNumCourses - number of courses entered. ***
  227. //*** ***
  228. //*** arrQltyPts [in] - array that stores QP ***
  229. //*** conversion. ***
  230. //*** ***
  231. //*** nTotLetters [in] - total letter grades ***
  232. //*** ***
  233. //*** RETURNS: double - GPA ***
  234. //*** ***
  235. //*** DESCRIPTION: This function iterates over the arrays ***
  236. //*** that store grade records and calculates ***
  237. //*** the GPA. ***
  238. //****************************************************************************
  239. double calcGPA(string arrNames[], int arrCreditHours[],
  240. char arrGrades[], int nNumCourses,
  241. const char arrQltyPts[], int nTotLetters)
  242. {
  243. double dfTotalQualityPoints = 0.0; // Total quality points earned.
  244. double dfTotalCredits = 0.0; // Total credits earned.
  245.  
  246. //***
  247. //*** Iterate over the arrays of grade records and accumulate
  248. //*** the total quality points and total credits.
  249. //***
  250. for (int nInd = 0; nInd < nNumCourses; nInd++)
  251. {
  252. dfTotalQualityPoints +=
  253. arrCreditHours[nInd] * calcQualityPoints(arrGrades[nInd], arrQltyPts, nTotLetters);
  254. dfTotalCredits += arrCreditHours[nInd];
  255. }
  256.  
  257. //***
  258. //*** Return the average
  259. //***
  260. return dfTotalQualityPoints / dfTotalCredits;
  261. }
  262. //****************************************************************************
  263.  
  264.  
  265.  
  266. //****************************************************************************
  267. //*** FUNCTION: calcGPA ***
  268. //*** ***
  269. //*** PARAMETERS: arrNames [in] - array of course names ***
  270. //*** ***
  271. //*** arrCreditHours [in] - array of credit ***
  272. //*** hours ***
  273. //*** ***
  274. //*** cGrades [in]- array of letter grades. ***
  275. //*** ***
  276. //*** nNumCourses - number of courses entered. ***
  277. //*** ***
  278. //*** arrQltyPts [in] - array that stores QP ***
  279. //*** conversion. ***
  280. //*** ***
  281. //*** nTotLetters [in] - total letter grades ***
  282. //*** ***
  283. //*** RETURNS: double - GPA ***
  284. //*** ***
  285. //*** DESCRIPTION: This function iterates over the arrays ***
  286. //*** that store grade records and calculates ***
  287. //*** the GPA. ***
  288. //****************************************************************************
  289. void dispCourses(CourseGrade course[], int nNumCourses,
  290. const char arrQltyPts[], int nTotLetters)
  291. {
  292. //***
  293. //*** Print header first
  294. //***
  295. cout << "============================================================================" << endl;
  296. cout << "=== GRADE REPORT ===" << endl;
  297. cout << "============================================================================" << endl;
  298. cout << endl;
  299. cout << left << setw(35) << "COURSE NAME" << setw(15) << "CREDIT HRS"
  300. << setw(15) << "QLTY PTS" << setw(15) << "GRADE" << endl;
  301. cout << left << setw(35) << "====================" << setw(15) << "=========="
  302. << setw(15) << "==========" << setw(15) << "==========" << endl;
  303.  
  304. //***
  305. //*** Iterate over the courses and display them
  306. //***
  307. for (int nInd = 0; nInd < nNumCourses; nInd++)
  308. {
  309. displayCourse(course[nInd].arrCourseNames,
  310. course[nInd].arrCreditHours,
  311. course[nInd].arrGrades,
  312. arrQltyPts, nTotLetters);
  313. }
  314. cout << "============================================================================" << endl;
  315. cout << "GPA: " << right << fixed << showpoint << setprecision(2) << setw(30)<<
  316. calcGPA(arrNames, arrCreditHours, arrGrades, nNumCourses, arrQltyPts, nTotLetters) << endl;
  317. cout << "============================================================================" << endl;
  318. }
  319. //****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement