fork download
  1. //Maxwell Brewer CS1A Chapter 11, p. 648, #12
  2. //
  3. /*******************************************************************************
  4.  * STUDENT GRADE CALCULATOR
  5.  * _____________________________________________________________________________
  6.  * This program collects information about students, including their names,
  7.  * ID numbers, and test scores. It calculates the average test score and assigns
  8.  * a letter grade based on the average.
  9.  *
  10.  * INPUT:
  11.  * - Number of students and number of tests per student.
  12.  * - Each student’s name, ID number, and test scores.
  13.  *
  14.  * OUTPUT:
  15.  * - A summary table displaying each student’s name, ID, average score,
  16.  * and corresponding letter grade.
  17.  *******************************************************************************/
  18.  
  19. #include <iostream>
  20. #include <string>
  21.  
  22. using namespace std;
  23.  
  24. // Structure to store student information
  25. struct Student {
  26. string name;
  27. int ID;
  28. int* testScores;
  29. double average;
  30. char grade;
  31. };
  32.  
  33. // Function prototypes
  34. double calculateAverage(const Student &student, int numTests);
  35. char determineLetterGrade(double average);
  36.  
  37. int main() {
  38. // Variables to hold the number of students and tests
  39. int numStudents, numTests;
  40.  
  41. // Input number of students and tests
  42. cout << "Enter the number of students: ";
  43. cin >> numStudents;
  44.  
  45. cout << "Enter the number of tests per student: ";
  46. cin >> numTests;
  47.  
  48. // Dynamic allocation of student array
  49. Student* students = new Student[numStudents];
  50.  
  51. // Input data for each student
  52. cout << "\nEnter data for each student:\n";
  53. for (int i = 0; i < numStudents; i++) {
  54. cout << "\nStudent " << (i + 1) << ":\n";
  55.  
  56. // Input student name
  57. cout << "Name: ";
  58. cin.ignore(); // Clear input buffer
  59. getline(cin, students[i].name);
  60.  
  61. // Validate name input
  62. while (students[i].name.empty()) {
  63. cout << "Name cannot be empty. Please enter the name: ";
  64. getline(cin, students[i].name);
  65. }
  66.  
  67. // Input student ID
  68. cout << "ID Number: ";
  69. cin >> students[i].ID;
  70.  
  71. // Allocate memory for test scores
  72. students[i].testScores = new int[numTests];
  73.  
  74. // Input test scores
  75. for (int j = 0; j < numTests; j++) {
  76. cout << "Test " << (j + 1) << ": ";
  77. cin >> students[i].testScores[j];
  78.  
  79. // Validate nonnegative test scores
  80. while (students[i].testScores[j] < 0) {
  81. cout << "Test scores must be nonnegative. Enter again: ";
  82. cin >> students[i].testScores[j];
  83. }
  84. }
  85.  
  86. // Calculate average and grade
  87. students[i].average = calculateAverage(students[i], numTests);
  88. students[i].grade = determineLetterGrade(students[i].average);
  89. }
  90.  
  91. // Display results
  92. cout << "\n\nStudent Grade Report:\n";
  93. cout << "-------------------------------------------\n";
  94. cout << "Name\t\tID\t\tAverage\t\tGrade\n";
  95. cout << "-------------------------------------------\n";
  96.  
  97. for (int i = 0; i < numStudents; i++) {
  98. cout << students[i].name << "\t\t"
  99. << students[i].ID << "\t\t"
  100. << students[i].average << "\t\t"
  101. << students[i].grade << "\n";
  102. }
  103.  
  104. // Free allocated memory
  105. for (int i = 0; i < numStudents; i++) {
  106. delete[] students[i].testScores;
  107. }
  108. delete[] students;
  109.  
  110. return 0;
  111. }
  112.  
  113. /**
  114.  * Calculates the average test score for a student.
  115.  *
  116.  * @param student The student whose average is to be calculated.
  117.  * @param numTests The number of tests the student has taken.
  118.  * @return The calculated average score as a double.
  119.  */
  120. double calculateAverage(const Student &student, int numTests) {
  121. double total = 0;
  122.  
  123. // Sum all test scores
  124. for (int i = 0; i < numTests; i++) {
  125. total += student.testScores[i];
  126. }
  127.  
  128. return total / numTests; // Calculate and return average
  129. }
  130.  
  131. /**
  132.  * Determines the letter grade based on the average score.
  133.  *
  134.  * @param average The average score.
  135.  * @return The corresponding letter grade.
  136.  */
  137. char determineLetterGrade(double average) {
  138. if (average >= 91) {
  139. return 'A';
  140. } else if (average >= 81) {
  141. return 'B';
  142. } else if (average >= 71) {
  143. return 'C';
  144. } else if (average >= 61) {
  145. return 'D';
  146. } else {
  147. return 'F';
  148. }
  149. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Standard output is empty