Vla_DOS

Untitled

Sep 21st, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>  // SetConsoleCP, SetConsoleOutputCP
  3. #include <locale.h>
  4. #include <conio.h>
  5. #include <algorithm>
  6. #include <array>
  7.  
  8. using namespace std;
  9.  
  10. class Student {
  11. private:
  12.     array<int, 3> score;
  13.     int numberScorebook;
  14.     string lastName;
  15.    // int scoreAverage;
  16. public:
  17.     Student() {}
  18.  
  19.     Student(string lastName, int numberScorebook, array<int, 3> score) {
  20.         this->lastName = lastName;
  21.         this->numberScorebook = numberScorebook;
  22.         this->score = score;
  23.     }
  24.  
  25.     void AddItemArray(Student* students, int size, int scoreSize)
  26.     {
  27.         array<int, 3> myarray1 = { 8, 6, 4 };
  28.         array<int, 3> myarray2 = { 2, 1, 4 };
  29.         array<int, 3> myarray3 = { 1, 2, 3 };
  30.         array<int, 3> myarray4 = { 10, 6, 7 };
  31.         array<int, 3> myarray5 = { 4, 2, 2 };
  32.         array<int, 3> myarray6 = { 3, 4, 5 };
  33.  
  34.         students[0] = Student("Якимчук", 1, myarray1);
  35.         students[1] = Student("Богданець", 2, myarray2);
  36.         students[2] = Student("Мельник", 3, myarray3);
  37.         students[3] = Student("Ступка", 4, myarray4);
  38.         students[4] = Student("Макарець", 5, myarray5);
  39.         students[5] = Student("Власик", 6, myarray6);
  40.     }
  41.  
  42.     void GetArray(Student* students, int size, int scoreSize) {
  43.  
  44.         int temp[3];// = new int[scoreSize];
  45.         int index = 0;
  46.         for (int i = 0; i < size; i++)
  47.         {
  48.             cout << "Прізвище - " << students[i].lastName << ". Номер залікової книжки - " << students[i].numberScorebook << ". Оцінки - ";
  49.             for (int j = 0; j < scoreSize; j++)
  50.             {
  51.  
  52.                 cout << students[i].score[j] << " ";
  53.             }
  54.             cout << endl;
  55.         }
  56.     }
  57.  
  58.     void AverageScore(Student* students, int size, int scoreSize) {
  59.         //Double for loop
  60.         //Bubble sort
  61.         for (int i = 0; i < size - 1; i++) {
  62.             for (int j = 0; j < size - 1; j++) {
  63.                 double averageScore1 = 0;
  64.                 double averageScore2 = 0;
  65.                 double sum1 = 0;
  66.                 double sum2 = 0;
  67.                 //averageScore for student[j]
  68.                 for (int k = 0; k < scoreSize; k++) {
  69.                     sum1 += students[j].score[k];
  70.                 }
  71.                 averageScore1 = sum1 / scoreSize;
  72.                 //averageScore for student[j+1]
  73.                 for (int k = 0; k < scoreSize; k++) {
  74.                     sum2 += students[j + 1].score[k];
  75.                 }
  76.                 averageScore2 = sum2 / scoreSize;
  77.  
  78.                 if (averageScore2 > averageScore1) {
  79.                     Student temp = students[j + 1];
  80.                     students[j + 1] = students[j];
  81.                     students[j] = temp;
  82.                 }
  83.             }
  84.         }
  85.  
  86.         if (size > 5) size = 5;
  87.         cout << endl << size << " студентів з найбільшим середнім балом:" << endl;
  88.         GetArray(students, size, 3);
  89.     }
  90.  
  91. };
  92.  
  93. int main() {
  94.     SetConsoleCP(1251);
  95.     SetConsoleOutputCP(1251);
  96.     const int size = 6;
  97.     Student c[size];
  98.  
  99.  
  100.     c->AddItemArray(c, size, 3);
  101.  
  102.     c->GetArray(c, size, 3);
  103.  
  104.     c->AverageScore(c, size, 3);
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment