HERE IS THE TASK
Introduce several student records with fields:
Student's name
Record number Scholarship size
Array of structures "Subject"
The "Subject" structure has the following fields:
Teacher's name
Item Name
Evaluation
Write a program that performs the following actions:
- establishes scholarships for all students who study at 4 and 5
- sorts the structures in the order of growth of the average score (the direction of sorting is chosen by the user)
#include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; //ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΡΠ΅Π΄ΠΌΠ΅ΡΠΎΠ² Π² Π·Π°ΡΠ΅ΡΠΊΠ΅ const int subjectsCountInRecordBook = 2; //Π Π°Π·ΠΌΠ΅Ρ ΡΡΠΈΠΏΠ΅Π½Π΄ΠΈΠΈ const int paymentValue = 2000; struct Subject { char SubjName[30]; char SurnameTeacher[20]; int Mark; }; struct RecordBook { int Id; char SurnameStudent[30]; Subject Subjects[subjectsCountInRecordBook]; double Payment; double avgMark; }; int main() { int studentsCount; cout << "Enter count of students: "; cin >> studentsCount; cout << endl; //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΡΡΡΠ΅ Π·Π°ΡΠ΅ΡΠΊΠΈ ΠΏΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ ΡΡΠ΅Π΄Π΅Π½ΡΠΎΠ² RecordBook *recordBooks = new RecordBook[studentsCount]; //ΠΠ°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΠΏΡΠ΅Π΄ΠΌΠ΅ΡΠ°Ρ
Π²ΠΎ Π²ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅Ρ Subject subjectsContainer[subjectsCountInRecordBook]; for (int i = 0; i < subjectsCountInRecordBook; i++) { cout << "Enter" << (i + 1) << " subject name: "; cin >> subjectsContainer[i].SubjName; cout << "Enter" << (i + 1) << "Surname Teacher: "; cin >> subjectsContainer[i].SurnameTeacher; cout << endl; } //ΠΠ°ΡΠΈΠ½Π°Π΅ΠΌ Π·Π°ΠΏΠΎΠ»Π½ΡΡΡ Π·Π°ΡΠ΅ΡΠΊΠΈ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΡΡΠ΄Π΅Π½ΡΠ° for (int i = 0; i < studentsCount; i++) { cout << "Enter" << (i + 1) << " student's lastname: "; cin >> recordBooks[i].SurnameStudent; cout << "Enter" << (i + 1) << " record book id: "; cin >> recordBooks[i].Id; //Π‘ΡΠΌΠΌΠ° ΠΎΡΠ΅Π½ΠΎΠΊ ΠΏΠΎ Π²ΡΠ΅ΠΌ ΠΏΡΠ΅Π΄ΠΌΠ΅ΡΠ°ΠΌ Π² Π·Π°ΡΠ΅ΡΠΊΠ΅ int sumMark = 0; //ΠΡΠΆΠ½ΠΎ Π»ΠΈ ΠΏΠ»Π°ΡΠΈΡΡ ΡΡΠΈΠΏΠ΅Π½Π΄ΠΈΡ ΡΡΠΎΠΌΡ ΡΡΠ΅Π΄ΡΠ½ΡΡ? bool needPayment = true; //Π‘ΠΊΠΎΠΏΠΈΡΡΠ΅ΠΌ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅ΡΠ° Π² Π·Π°ΡΠ΅ΡΠΊΡ ΠΈ Π²ΠΏΠΈΡΠ΅ΠΌ ΠΎΡΠ΅Π½ΠΊΠΈ ΠΏΠΎ ΠΏΡΠ΅Π΄ΠΌΠ΅ΡΠ°ΠΌ for (int j = 0; j < subjectsCountInRecordBook; j++) { strcpy_s(recordBooks[i].Subjects[j].SubjName, subjectsContainer[j].SubjName); strcpy_s(recordBooks[i].Subjects[j].SurnameTeacher, subjectsContainer[j].SurnameTeacher); cout << "Enter" << (i + 1) << " student's mark of " << recordBooks[i].Subjects[j].SubjName << ": "; cin >> recordBooks[i].Subjects[j].Mark; //Π£ΡΡΠ°Π½ΠΎΠ²ΠΈΠΌ ΡΡΠΈΠΏΠ½Π΄ΠΈΠΈ Π΄Π»Ρ ΡΠ΅Ρ
ΠΊΡΠΎ ΡΡΠΈΡΡΡ Π½Π° 4 ΠΈ 5 if (recordBooks[i].Subjects[j].Mark < 4) needPayment = false; //ΡΡΠΈΡΠ°Π΅ΠΌ ΠΈ Π·Π°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΡΡΠ΅Π΄Π½ΠΈΠΉ Π±Π°Π»Π» Π² Π·Π°ΡΠ΅ΡΠΊΠ΅ sumMark += recordBooks[i].Subjects[j].Mark; recordBooks[i].avgMark = (double)sumMark / (double)subjectsCountInRecordBook; } if(needPayment == true) recordBooks[i].Payment = paymentValue; else recordBooks[i].Payment = 0; } //ΠΡΠ±ΠΎΡ ΡΠΈΠΏΠ° ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ int sort_type; while (true) { cout << "Choose sort type(0 for ascending; 1 for descending): "; cin >> sort_type; cout << endl; if (sort_type != 0 && sort_type != 1) { cout << "Unknown sort type." << endl; continue; } break; } //===Π‘ΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ° Π·Π°ΡΠ΅ΡΠΎΠΊ=== for (int i = 0; i < studentsCount - 1; i++) { for (int j = 0; j < studentsCount - i - 1; j++) { RecordBook temp; if ((sort_type == 0 && recordBooks[j].avgMark > recordBooks[j + 1].avgMark) || (sort_type == 1 && recordBooks[j].avgMark < recordBooks[j + 1].avgMark)) { temp = recordBooks[j]; recordBooks[j] = recordBooks[j + 1]; recordBooks[j + 1] = temp; } } } //ΠΡΠ²ΠΎΠ΄ΠΈΠΌ ΠΎΡΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ² Π·Π°ΡΠ΅ΡΠΎΠΊ Π½Π° ΡΠΊΡΠ°Π½ cout << "=========Sorted=record=books=============" << endl; for (int i = 0; i < studentsCount; i++) { cout << "Student Surname: " << recordBooks[i].SurnameStudent << endl; cout << fixed; cout << setprecision(2); cout << "Avg Mark: " << recordBooks[i].avgMark << endl; cout << "Record book Id: " << recordBooks[i].Id << endl; cout << "Payment: " << recordBooks[i].Payment << endl; //ΠΠ½ΡΠ° ΠΏΠΎ ΠΏΡΠ΅Π΄ΠΌΠ΅ΡΠ°ΠΌ cout << "Subjects info:"; cout << endl; for (int j = 0; j < subjectsCountInRecordBook; j++) { cout << "\tSubject:" << recordBooks[i].Subjects[j].SubjName << ", Teacher:" << recordBooks[i].Subjects[j].SurnameTeacher << ", Mark:" << recordBooks[i].Subjects[j].Mark << endl; } cout << "==================" << endl; } delete[] recordBooks; system("pause"); return 0; }