Advertisement
Kamoss2

Code lab 01

Jan 25th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. double GetAverageScore(double** myArray, int numStudents, int examSlot)
  2. {
  3. double scoreCollection = 0.0;
  4. //calculate average
  5. for (int i = 0; i < numStudents; ++i)
  6. {
  7. scoreCollection = scoreCollection + myArray[i][examSlot];
  8. }
  9. double averageScore = scoreCollection / numStudents;
  10. return averageScore;
  11. }
  12.  
  13. string GetLetterGrades(double** myArray, int i, int j)
  14. {
  15. string letterGrade;
  16. double averageScore = 0.0;
  17.  
  18. if (myArray[i][j] <= averageScore - 5 || myArray[i][j] >= averageScore + 5)
  19. {
  20. letterGrade = "(C)";
  21. }
  22. if (myArray[i][j] > averageScore + 5 && myArray[i][j] < averageScore + 15)
  23. {
  24. letterGrade = "(B)";
  25. }
  26. if (myArray[i][j] < averageScore - 5 && myArray[i][j] > averageScore - 15)
  27. {
  28. letterGrade = "(D)";
  29. }
  30. if (myArray[i][j] >= averageScore + 15)
  31. {
  32. letterGrade = "(A)";
  33. }
  34. if (myArray[i][j] <= averageScore - 15)
  35. {
  36. letterGrade = "(E)";
  37. }
  38. return letterGrade;
  39. }
  40. string ReturnNumberGrade(double** myArray, int numStudents, int numExams)
  41. {
  42. string letterGrade;
  43. int numberA = 0;
  44. int numberB = 0;
  45. int numberC = 0;
  46. int numberD = 0;
  47. int numberE = 0;
  48. for (int i = 0; i < numStudents; ++i)
  49. {
  50. for (int j = 0; j < numExams; ++j) {
  51. if (GetLetterGrades(myArray, i, j) == "(A)")
  52. {
  53. numberA = numberA + 1;
  54. }
  55. if (GetLetterGrades(myArray, i, j) == "(B)")
  56. {
  57. numberB = numberB + 1;
  58. }
  59. if (GetLetterGrades(myArray, i, j) == "(C)")
  60. {
  61. numberC = numberC + 1;
  62. }
  63. if (GetLetterGrades(myArray, i, j) == "(D)")
  64. {
  65. numberD = numberD + 1;
  66. }
  67. if (GetLetterGrades(myArray, i, j) == "(E)")
  68. {
  69. numberE = numberE + 1;
  70. }
  71. }
  72. }
  73. ostringstream gradeLine;
  74. gradeLine << numberA << "(A)" << numberB << "(B)" + numberC << "(C)" << numberD << "(D)" << numberE << "(E)";
  75. return gradeLine.str();
  76. }
  77. double GetTotalAverage(double** myArray, int i, int numExams)
  78. {
  79. double totalAverage;
  80. double totalScore = 0;
  81. for (int j = 0; j < numExams; ++j)
  82. {
  83. totalScore = totalScore + myArray[i][j];
  84. }
  85. totalAverage = totalScore / numExams;
  86. return totalAverage;
  87. }
  88.  
  89. #ifdef _MSC_VER
  90. #define _CRTDBG_MAP_ALLOC
  91. #include <crtdbg.h>
  92. #define VS_MEM_CHECK _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  93. #else
  94. #define VS_MEM_CHECK
  95. #endif
  96. #include <iostream>
  97. #include <iomanip>
  98. #include <fstream>
  99. #include <string>
  100. #include <sstream>
  101. using namespace std;
  102.  
  103.  
  104. int main(int argc, char* argv[]) {
  105. if (argc < 3)
  106. {
  107. cerr << "Please provide name of input and output files";
  108. return 1;
  109. }
  110. cout << "Input file: " << argv[1] << endl;
  111. ifstream in(argv[1]);
  112. if (!in)
  113. {
  114. cerr << "Unable to open " << argv[1] << " for input";
  115. return 2;
  116. }
  117. cout << "Output file: " << argv[2] << endl;
  118. ofstream out(argv[2]);
  119. if (!out)
  120. {
  121. in.close();
  122. cerr << "Unable to open " << argv[2] << " for output";
  123. return 3;
  124. }
  125. VS_MEM_CHECK // enable memory leak check
  126. // Your program..
  127. int numStudents = 0;
  128. int numExams = 0;
  129. //read in number of students and number of exams
  130. in >> numStudents >> numExams;
  131. // Skip the rest of the line
  132. in.ignore(std::numeric_limits<int>::max(), '\n');
  133. //set up array
  134. double** myArray = new double* [numStudents];
  135. for (int i = 0; i < numStudents; ++i)
  136. {
  137. myArray[i] = new double[numExams];
  138. }
  139. string* nameArray = new string[numStudents];
  140. for (int i = 0; i < numStudents; i++) {
  141. //fill name array
  142. string firstName;
  143. string lastName;
  144. in >> firstName >> lastName;
  145. nameArray[i] = firstName + " " + lastName;
  146. //fill exam score array
  147. for (int j = 0; j < numExams; j++) {
  148. int score;
  149. in >> score;
  150. myArray[i][j] = score;
  151. }
  152. }
  153. out << "Exam Averages:" << endl;
  154. //output average
  155. for (int i = 0; i < numExams; ++i)
  156. {
  157. out << "\t" << "Exam " << i << " Average = ";
  158. out << setprecision(1);
  159. out << GetAverageScore(myArray, numStudents, i);
  160. out << endl;
  161. }
  162. //output student names and grades with letter grades
  163. out << "Student Exam Grades:" << endl;
  164. for (int i = 0; i < numStudents; ++i)
  165. {
  166. out << "\t" << nameArray[i];
  167. for (int j = 0; j < numExams; ++j)
  168. {
  169. out << "\t" << myArray[i][j];
  170. out << GetLetterGrades(myArray, i, j);
  171. }
  172. out << endl;
  173. }
  174. //output final numbers of grades
  175. out << "Exam Grades:" << endl;
  176. for (int i = 0; i < numExams; ++i)
  177. {
  178. out << "Exam " << i << " ";
  179. out << ReturnNumberGrade(myArray, numStudents, numExams);
  180. out << endl;
  181. }
  182. //output each students final grade
  183. out << "Student Final Grades:" << endl;
  184. for (int i = 0; i < numStudents; ++i)
  185. {
  186. out << nameArray[i] << " ";
  187. out << GetTotalAverage(myArray, i, numExams);
  188. }
  189. //clear up memory
  190. for (int i = 0; i < numStudents; ++i)
  191. {
  192. delete[] myArray[i];
  193. }
  194. delete[] myArray;
  195. delete[] nameArray;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement