fork download
  1. // Torrez, Elaine CS1A
  2. // Chapter 7, P. 447, #11
  3. // *****************************************************************************************
  4. // * GRADE BOOK *
  5. // *---------------------------------------------------------------------------------------*
  6. // * This program stores student names and their test scores using parallel arrays. *
  7. // * For each student, it validates five test scores (0–100), computes the average, *
  8. // * assigns a letter grade, and prints a formatted report. *
  9. // * *
  10. // * INPUT: *
  11. // * names[] : Student names *
  12. // * scores[][TESTS] : Five test scores per student (0–100) *
  13. // * *
  14. // * OUTPUT: *
  15. // * avg[] : Average score per student *
  16. // * letter[] : Letter grade per student (A/B/C/D/F) *
  17. // *****************************************************************************************
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21. #include <string>
  22. using namespace std;
  23.  
  24. int main()
  25. {
  26. /***** CONSTANTS *****/
  27. const int STUDENTS = 5; // number of students
  28. const int TESTS = 5; // tests per student
  29.  
  30. /***** PARALLEL ARRAYS *****/
  31. string names[STUDENTS];
  32. double scores[STUDENTS][TESTS];
  33. double avg[STUDENTS];
  34. char letter[STUDENTS];
  35.  
  36. /***** INPUT SECTION *****/
  37. cout << "Enter data for " << STUDENTS << " students.\n";
  38. cout << "(Each student has " << TESTS << " test scores between 0 and 100.)\n\n";
  39.  
  40. for (int s = 0; s < STUDENTS; s++)
  41. {
  42. cout << "Student " << (s + 1) << " name: ";
  43. getline(cin, names[s]);
  44. if (names[s].empty()) { // if leftover newline or empty, prompt again
  45. getline(cin, names[s]);
  46. }
  47.  
  48. for (int t = 0; t < TESTS; t++)
  49. {
  50. cout << " Test " << (t + 1) << " score: ";
  51. cin >> scores[s][t];
  52.  
  53. // Validate: 0–100
  54. while (scores[s][t] < 0.0 || scores[s][t] > 100.0)
  55. {
  56. cout << " Invalid. Enter a score between 0 and 100: ";
  57. cin >> scores[s][t];
  58. }
  59. }
  60. cin.ignore(10000, '\n'); // clear newline before next getline
  61. cout << "\n";
  62. }
  63.  
  64. /***** PROCESSING SECTION *****/
  65. for (int s = 0; s < STUDENTS; s++)
  66. {
  67. double sum = 0.0;
  68. for (int t = 0; t < TESTS; t++)
  69. sum += scores[s][t];
  70.  
  71. avg[s] = sum / TESTS;
  72.  
  73. if (avg[s] >= 90.0) letter[s] = 'A';
  74. else if (avg[s] >= 80.0) letter[s] = 'B';
  75. else if (avg[s] >= 70.0) letter[s] = 'C';
  76. else if (avg[s] >= 60.0) letter[s] = 'D';
  77. else letter[s] = 'F';
  78. }
  79.  
  80. /***** OUTPUT SECTION *****/
  81. cout << fixed << setprecision(2);
  82. cout << "====================== GRADE BOOK REPORT ======================\n";
  83. cout << left << setw(20) << "Name"
  84. << right << setw(8) << "Avg"
  85. << setw(8) << "Grade" << "\n";
  86. cout << "---------------------------------------------------------------\n";
  87.  
  88. for (int s = 0; s < STUDENTS; s++)
  89. {
  90. cout << left << setw(20) << names[s]
  91. << right << setw(8) << avg[s]
  92. << setw(8) << letter[s] << "\n";
  93. }
  94. cout << "===============================================================\n";
  95.  
  96. return 0;
  97. }
  98.  
Success #stdin #stdout 0.01s 5308KB
stdin
Alice
95
88
92
90
100

Ben
78
84
79
88
81

Carla
67
72
70
65
60

Diego
50
55
60
45
58

Emma
90
94
88
92
95
stdout
Enter data for 5 students.
(Each student has 5 test scores between 0 and 100.)

Student 1 name:   Test 1 score:   Test 2 score:   Test 3 score:   Test 4 score:   Test 5 score: 
Student 2 name:   Test 1 score:   Test 2 score:   Test 3 score:   Test 4 score:   Test 5 score: 
Student 3 name:   Test 1 score:   Test 2 score:   Test 3 score:   Test 4 score:   Test 5 score: 
Student 4 name:   Test 1 score:   Test 2 score:   Test 3 score:   Test 4 score:   Test 5 score: 
Student 5 name:   Test 1 score:   Test 2 score:   Test 3 score:   Test 4 score:   Test 5 score: 
====================== GRADE BOOK REPORT ======================
Name                     Avg   Grade
---------------------------------------------------------------
Alice                  93.00       A
Ben                    82.00       B
Carla                  66.80       D
Diego                  53.60       F
Emma                   91.80       A
===============================================================