Advertisement
Spocoman

04. Grades

Sep 8th, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int studentCount;
  7.     cin >> studentCount;
  8.  
  9.     double grade3 = 0, grade4 = 0, grade5 = 0, grade6 = 0, totalGradeSum = 0, currentGrade;
  10.  
  11.     for (int i = 0; i < studentCount; i++) {
  12.         cin >> currentGrade;
  13.  
  14.         if (currentGrade >= 5) {
  15.             grade6++;
  16.         }
  17.         else if (currentGrade >= 4) {
  18.             grade5++;
  19.         }
  20.         else if (currentGrade >= 3) {
  21.             grade4++;
  22.         }
  23.         else if (currentGrade >= 2) {
  24.             grade3++;
  25.         }
  26.  
  27.         totalGradeSum += currentGrade;
  28.     }
  29.     cout.setf(ios::fixed);
  30.     cout.precision(2);
  31.  
  32.     cout << "Top students: " << 100.0 * grade6 / studentCount << "%\n";
  33.     cout << "Between 4.00 and 4.99: " << 100.0 * grade5 / studentCount << "%\n";
  34.     cout << "Between 3.00 and 3.99: " << 100.0 * grade4 / studentCount << "%\n";
  35.     cout << "Fail: " << 100.0 * grade3 / studentCount << "%\n";
  36.     cout << "Average: " << 1.0 * totalGradeSum / studentCount << endl;
  37.  
  38.     return 0;
  39. }
  40.  
  41. Решение с тернарен оператор:
  42.  
  43. #include <iostream>
  44.  
  45. using namespace std;
  46.  
  47. int main() {
  48.     int studentCount;
  49.     cin >> studentCount;
  50.  
  51.     double grade3 = 0, grade4 = 0, grade5 = 0, grade6 = 0, totalGradeSum = 0, currentGrade;
  52.  
  53.     for (int i = 0; i < studentCount; i++) {
  54.         cin >> currentGrade;
  55.  
  56.         currentGrade >= 5 ? grade6++ : currentGrade >= 4 ? grade5++ : currentGrade >= 3 ? grade4++ : grade3++;
  57.  
  58.         totalGradeSum += currentGrade;
  59.     }
  60.  
  61.     cout.setf(ios::fixed);
  62.     cout.precision(2);
  63.  
  64.     cout << "Top students: " << 100.0 * grade6 / studentCount << "%\n";
  65.     cout << "Between 4.00 and 4.99: " << 100.0 * grade5 / studentCount << "%\n";
  66.     cout << "Between 3.00 and 3.99: " << 100.0 * grade4 / studentCount << "%\n";
  67.     cout << "Fail: " << 100.0 * grade3 / studentCount << "%\n";
  68.     cout << "Average: " << 1.0 * totalGradeSum / studentCount << endl;
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement