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; 

}

  • HERE'S TASK Enter several structures "Student Records" with fields: Student's Name Record Number Scholarships Array of Structures "Subject Structure" Subjects "has fields: Teacher's name Name of subject Assessment Write a program that performs the following actions: sets scholarships for all students who study on 4 and 5 sorts structures in the order of growth of the average score (the direction of sorting is selected by the user) - Alina Gordeeva
  • What's the problem? In the fact that you can not alter it? Or have you tried and failed? If you tried, what problems did you encounter? - vegorov
  • I just can not redo it. It seems that you need to use the list here, but I do not quite understand how, could you help? - Alina Gordeeva

1 answer 1

Yes, there is nothing special and nothing to redo.

You add the input of the number of subjects (by analogy with the input of the number of students), and do not forget to select and clean up the memory for yourself (by analogy with the array of credits). Everything.

The example introduces the number of subjects for all students. If you need the number of subjects for students to be different (one has 2, another has 4) - add a field with the number of subjects to the structure of the class, for each student I enter the number of his subjects and the subjects themselves.

Another option is when your subjects are fixed - 10 pieces, but each student has his own subjects (some subset of the set of original subjects). Then start the id field for the subject structure, and when entering a student, ask for the number of subjects, and then to enter each subject, ask for the subject's id and grade for the subject.

If possible - all this can be rewritten using classes and std :: string, then you can use lists and vectors from STL: std::list and std::vector , which simplifies the tasks of working with memory.

 #include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; //ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΏΡ€Π΅Π΄ΠΌΠ΅Ρ‚ΠΎΠ² Π² Π·Π°Ρ‡Π΅Ρ‚ΠΊΠ΅ int subjectsCountInRecordBook = 0; //Π Π°Π·ΠΌΠ΅Ρ€ стипСндии const int paymentValue = 2000; struct Subject { char SubjName[30]; char SurnameTeacher[20]; int Mark; }; struct RecordBook { int Id; char SurnameStudent[30]; Subject* Subjects; double Payment; double avgMark; }; int main() { int studentsCount; cout << "Enter count of students: "; cin >> studentsCount; cout << "Enter count of subject:"; cin >> subjectsCountInRecordBook; cout << endl; //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ пустыС Π·Π°Ρ‡Π΅Ρ‚ΠΊΠΈ ΠΏΠΎ количСству стСдСнтов RecordBook *recordBooks = new RecordBook[studentsCount]; //ВыдСляСм ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΏΠΎΠ΄ ΠΏΡ€Π΅Π΄ΠΌΠ΅Ρ‚Ρ‹ Π² Π·Π°Ρ‡Ρ‘Ρ‚ΠΊΠ΅ for (int i = 0; i < studentsCount; i++){ recordBooks[i].Subjects = new Subject[subjectsCountInRecordBook]; } //ЗаполняСм ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ ΠΏΡ€Π΅Π΄ΠΌΠ΅Ρ‚Π°Ρ… Π²ΠΎ Π²Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ Subject* subjectsContainer = new Subject[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; } for (int i = 0; i < studentsCount; i++){ delete[] recordBooks[i].Subjects; } delete[] recordBooks; delete[] subjectsContainer; system("pause"); return 0; } 

Conclusion:

 Enter count of students: 2 Enter count of subject:3 Enter1 subject name: OOP Enter1Surname Teacher: Egorov Enter2 subject name: AIG Enter2Surname Teacher: Ivanova Enter3 subject name: TOE Enter3Surname Teacher: Karlova Enter1 student's lastname: Petrov Enter1 record book id: 1 Enter1 student's mark of OOP: 4 Enter1 student's mark of AIG: 5 Enter1 student's mark of TOE: 4 Enter2 student's lastname: Sidorov Enter2 record book id: 2 Enter2 student's mark of OOP: 3 Enter2 student's mark of AIG: 5 Enter2 student's mark of TOE: 5 Choose sort type(0 for ascending; 1 for descending): 1 =========Sorted=record=books============= Student Surname: Petrov Avg Mark: 4.33 Record book Id: 1 Payment: 2000.00 Subjects info: Subject:OOP, Teacher:Egorov, Mark:4 Subject:AIG, Teacher:Ivanova, Mark:5 Subject:TOE, Teacher:Karlova, Mark:4 ================== Student Surname: Sidorov Avg Mark: 4.33 Record book Id: 2 Payment: 0.00 Subjects info: Subject:OOP, Teacher:Egorov, Mark:3 Subject:AIG, Teacher:Ivanova, Mark:5 Subject:TOE, Teacher:Karlova, Mark:5 ================== 
  • The example introduces the number of subjects for all students. "If you need the number of subjects for students to be different (one has 2, another has 4), add a field with a number of subjects to the structure of the class, for each student I enter the number of his subjects and the subjects themselves." I need it to be this way, but something doesn't work - Alina Gordeeva
  • @ AlinaGordeeva Well, yes, as long as you can only ask questions =) Write down what exactly does not work. Give an example of the code - your code. The author of the code attached to the question would easily solve this problem. Even if he had problems with the allocation of dynamic arrays - in my answer he would have spied how to do it. - vegorov