fork download
  1. // Elaine Torrez #12 CS1A Chapter 11 – Course Grade Problem
  2.  
  3.  
  4. /********************************************************************************************
  5.  *
  6.  * COURSE GRADES
  7.  *
  8.  * ------------------------------------------------------------------------------------------
  9.  * This program keeps a list of students and their test scores. It uses a structure to store
  10.  * each student's:
  11.  * - name
  12.  * - ID number
  13.  * - pointer to an array of test scores
  14.  * - average test score
  15.  * - course grade
  16.  *
  17.  * The program:
  18.  * 1) Asks how many students there are and how many test scores per student.
  19.  * 2) Dynamically allocates an array of structures for the students.
  20.  * 3) For each student, dynamically allocates an array of test scores.
  21.  * 4) Prompts the user to enter name, ID number, and ALL test scores.
  22.  * 5) Computes the average test score for each student.
  23.  * 6) Assigns a letter grade using this scale:
  24.  *
  25.  * 91 – 100 : A
  26.  * 81 – 90 : B
  27.  * 71 – 80 : C
  28.  * 61 – 70 : D
  29.  * 60 or less: F
  30.  *
  31.  * 7) Displays a table of each student's name, ID number, average score, and course grade.
  32.  * ------------------------------------------------------------------------------------------
  33.  *
  34.  * INPUT
  35.  * numStudents : Number of students in the class
  36.  * numTests : Number of tests per student
  37.  * For each student:
  38.  * name : Student name
  39.  * idNum : Student ID number
  40.  * tests[] : Each test score (must be >= 0)
  41.  *
  42.  * OUTPUT
  43.  * For each student:
  44.  * name
  45.  * ID number
  46.  * average test score
  47.  * course grade
  48.  *
  49.  * INPUT VALIDATION
  50.  * - Number of students and number of tests must be > 0
  51.  * - Test scores must not be negative
  52.  *
  53.  ********************************************************************************************/
  54.  
  55. #include <iostream>
  56. #include <iomanip>
  57. #include <string>
  58. #include <limits>
  59. using namespace std;
  60.  
  61. //------------------------------------------------------------
  62. // Structure to store one student's grade information
  63. //------------------------------------------------------------
  64. struct CourseGrade {
  65. string name; // Student name
  66. int idNum; // Student ID number
  67. float *tests; // Pointer to array of test scores
  68. float average;// Average test score
  69. char grade; // Course grade
  70. };
  71.  
  72. // Function prototypes
  73. void getStudentData(CourseGrade[], int numStudents, int numTests);
  74. void computeAveragesAndGrades(CourseGrade[], int numStudents, int numTests);
  75. void displayReport(const CourseGrade[], int numStudents);
  76.  
  77. int main()
  78. {
  79. int numStudents;
  80. int numTests;
  81.  
  82. cout << "How many students? ";
  83. cin >> numStudents;
  84. while (numStudents <= 0) {
  85. cout << " **ERROR** Enter a value greater than 0: ";
  86. cin >> numStudents;
  87. }
  88.  
  89. cout << "How many tests per student? ";
  90. cin >> numTests;
  91. while (numTests <= 0) {
  92. cout << " **ERROR** Enter a value greater than 0: ";
  93. cin >> numTests;
  94. }
  95.  
  96. //--------------------------------------------------------
  97. // Dynamically allocate array of CourseGrade structures
  98. //--------------------------------------------------------
  99. CourseGrade *students = new CourseGrade[numStudents];
  100.  
  101. //--------------------------------------------------------
  102. // Dynamically allocate test-score arrays for each student
  103. //--------------------------------------------------------
  104. for (int i = 0; i < numStudents; ++i) {
  105. students[i].tests = new float[numTests];
  106. }
  107.  
  108. //--------------------------------------------------------
  109. // Get all input from the user
  110. //--------------------------------------------------------
  111. getStudentData(students, numStudents, numTests);
  112.  
  113. //--------------------------------------------------------
  114. // Compute averages and course grades, then display report
  115. //--------------------------------------------------------
  116. computeAveragesAndGrades(students, numStudents, numTests);
  117. displayReport(students, numStudents);
  118.  
  119. //--------------------------------------------------------
  120. // Free dynamically allocated memory
  121. //--------------------------------------------------------
  122. for (int i = 0; i < numStudents; ++i) {
  123. delete [] students[i].tests;
  124. }
  125. delete [] students;
  126.  
  127. return 0;
  128. }
  129.  
  130. //------------------------------------------------------------
  131. // Prompts user for each student's name, ID, and test scores
  132. //------------------------------------------------------------
  133. void getStudentData(CourseGrade s[], int numStudents, int numTests)
  134. {
  135. cout << "\nEnter student information:\n";
  136. cout << "--------------------------\n";
  137.  
  138. // Clear leftover newline so getline works after using >>
  139. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  140.  
  141. for (int i = 0; i < numStudents; ++i) {
  142. cout << "Student #" << (i + 1) << " name: ";
  143. getline(cin, s[i].name);
  144.  
  145. cout << "Student #" << (i + 1) << " ID number: ";
  146. cin >> s[i].idNum;
  147.  
  148. for (int j = 0; j < numTests; ++j) {
  149. cout << " Test #" << (j + 1) << " score: ";
  150. cin >> s[i].tests[j];
  151.  
  152. while (s[i].tests[j] < 0.0f) {
  153. cout << " **ERROR** Score cannot be negative. Re-enter: ";
  154. cin >> s[i].tests[j];
  155. }
  156. }
  157.  
  158. // Clear newline before next getline for name
  159. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  160. cout << endl;
  161. }
  162. }
  163.  
  164. //------------------------------------------------------------
  165. // Computes each student's average and assigns a letter grade
  166. //------------------------------------------------------------
  167. void computeAveragesAndGrades(CourseGrade s[], int numStudents, int numTests)
  168. {
  169. for (int i = 0; i < numStudents; ++i) {
  170. float sum = 0.0f;
  171.  
  172. for (int j = 0; j < numTests; ++j) {
  173. sum += s[i].tests[j];
  174. }
  175.  
  176. s[i].average = sum / numTests;
  177.  
  178. // Assign letter grade based on average
  179. if (s[i].average >= 91.0f)
  180. s[i].grade = 'A';
  181. else if (s[i].average >= 81.0f)
  182. s[i].grade = 'B';
  183. else if (s[i].average >= 71.0f)
  184. s[i].grade = 'C';
  185. else if (s[i].average >= 61.0f)
  186. s[i].grade = 'D';
  187. else
  188. s[i].grade = 'F';
  189. }
  190. }
  191.  
  192. //------------------------------------------------------------
  193. // Displays a table of student name, ID, average, and grade
  194. //------------------------------------------------------------
  195. void displayReport(const CourseGrade s[], int numStudents)
  196. {
  197. cout << fixed << setprecision(2);
  198.  
  199. cout << "\nCOURSE GRADE REPORT\n";
  200. cout << "-------------------------------------------------------------\n";
  201. cout << left << setw(20) << "Name"
  202. << setw(10) << "ID"
  203. << setw(12) << "Average"
  204. << "Grade" << endl;
  205. cout << "-------------------------------------------------------------\n";
  206.  
  207. for (int i = 0; i < numStudents; ++i) {
  208. cout << left << setw(20) << s[i].name
  209. << setw(10) << s[i].idNum
  210. << setw(12) << s[i].average
  211. << s[i].grade << endl;
  212. }
  213. }
  214.  
  215.  
Success #stdin #stdout 0.01s 5308KB
stdin
3
4
Alice Smith
1001
95
88
92
90
Brian Lee
1002
78
82
80
76
Carla Diaz
1003
60
65
58
62
stdout
How many students? How many tests per student? 
Enter student information:
--------------------------
Student #1 name: Student #1 ID number:   Test #1 score:   Test #2 score:   Test #3 score:   Test #4 score: 
Student #2 name: Student #2 ID number:   Test #1 score:   Test #2 score:   Test #3 score:   Test #4 score: 
Student #3 name: Student #3 ID number:   Test #1 score:   Test #2 score:   Test #3 score:   Test #4 score: 

COURSE GRADE REPORT
-------------------------------------------------------------
Name                ID        Average     Grade
-------------------------------------------------------------
Alice Smith         1001      91.25       A
Brian Lee           1002      79.00       C
Carla Diaz          1003      61.25       D